]>
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 | ||
1790 | static int link_handle_bound_to_list(Link *link) { | |
1791 | Link *l; | |
1792 | Iterator i; | |
1793 | int r; | |
1794 | bool required_up = false; | |
1795 | bool link_is_up = false; | |
1796 | ||
1797 | assert(link); | |
1798 | ||
1799 | if (hashmap_isempty(link->bound_to_links)) | |
1800 | return 0; | |
1801 | ||
1802 | if (link->flags & IFF_UP) | |
1803 | link_is_up = true; | |
1804 | ||
1805 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
1806 | if (link_has_carrier(l)) { | |
1807 | required_up = true; | |
1808 | break; | |
1809 | } | |
1810 | ||
1811 | if (!required_up && link_is_up) { | |
1812 | r = link_down(link); | |
1813 | if (r < 0) | |
1814 | return r; | |
1815 | } else if (required_up && !link_is_up) { | |
1816 | r = link_up(link); | |
1817 | if (r < 0) | |
1818 | return r; | |
1819 | } | |
1820 | ||
1821 | return 0; | |
1822 | } | |
1823 | ||
1824 | static int link_handle_bound_by_list(Link *link) { | |
1825 | Iterator i; | |
1826 | Link *l; | |
1827 | int r; | |
1828 | ||
1829 | assert(link); | |
1830 | ||
1831 | if (hashmap_isempty(link->bound_by_links)) | |
1832 | return 0; | |
1833 | ||
1834 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
1835 | r = link_handle_bound_to_list(l); | |
1836 | if (r < 0) | |
1837 | return r; | |
1838 | } | |
1839 | ||
1840 | return 0; | |
1841 | } | |
1842 | ||
1843 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
1844 | int r; | |
1845 | ||
1846 | assert(link); | |
1847 | assert(carrier); | |
1848 | ||
1849 | if (link == carrier) | |
1850 | return 0; | |
1851 | ||
1852 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
1853 | return 0; | |
1854 | ||
1855 | r = hashmap_ensure_allocated(h, NULL); | |
1856 | if (r < 0) | |
1857 | return r; | |
1858 | ||
1859 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
1860 | if (r < 0) | |
1861 | return r; | |
1862 | ||
1863 | return 0; | |
1864 | } | |
1865 | ||
1866 | static int link_new_bound_by_list(Link *link) { | |
1867 | Manager *m; | |
1868 | Link *carrier; | |
1869 | Iterator i; | |
1870 | int r; | |
1871 | bool list_updated = false; | |
1872 | ||
1873 | assert(link); | |
1874 | assert(link->manager); | |
1875 | ||
1876 | m = link->manager; | |
1877 | ||
b295beea | 1878 | HASHMAP_FOREACH(carrier, m->links, i) { |
0d4ad91d AR |
1879 | if (!carrier->network) |
1880 | continue; | |
1881 | ||
1882 | if (strv_isempty(carrier->network->bind_carrier)) | |
1883 | continue; | |
1884 | ||
1885 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
1886 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
1887 | if (r < 0) | |
1888 | return r; | |
1889 | ||
1890 | list_updated = true; | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | if (list_updated) | |
84de38c5 | 1895 | link_dirty(link); |
0d4ad91d | 1896 | |
b295beea | 1897 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
0d4ad91d AR |
1898 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
1899 | if (r < 0) | |
1900 | return r; | |
1901 | ||
84de38c5 | 1902 | link_dirty(carrier); |
0d4ad91d AR |
1903 | } |
1904 | ||
1905 | return 0; | |
1906 | } | |
1907 | ||
1908 | static int link_new_bound_to_list(Link *link) { | |
1909 | Manager *m; | |
1910 | Link *carrier; | |
1911 | Iterator i; | |
1912 | int r; | |
1913 | bool list_updated = false; | |
1914 | ||
1915 | assert(link); | |
1916 | assert(link->manager); | |
1917 | ||
1918 | if (!link->network) | |
1919 | return 0; | |
1920 | ||
1921 | if (strv_isempty(link->network->bind_carrier)) | |
1922 | return 0; | |
1923 | ||
1924 | m = link->manager; | |
1925 | ||
1926 | HASHMAP_FOREACH (carrier, m->links, i) { | |
1927 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
1928 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
1929 | if (r < 0) | |
1930 | return r; | |
1931 | ||
1932 | list_updated = true; | |
1933 | } | |
1934 | } | |
1935 | ||
1936 | if (list_updated) | |
84de38c5 | 1937 | link_dirty(link); |
0d4ad91d AR |
1938 | |
1939 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
1940 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
1941 | if (r < 0) | |
1942 | return r; | |
1943 | ||
84de38c5 | 1944 | link_dirty(carrier); |
0d4ad91d AR |
1945 | } |
1946 | ||
1947 | return 0; | |
1948 | } | |
1949 | ||
1950 | static int link_new_carrier_maps(Link *link) { | |
1951 | int r; | |
1952 | ||
1953 | r = link_new_bound_by_list(link); | |
1954 | if (r < 0) | |
1955 | return r; | |
1956 | ||
1957 | r = link_handle_bound_by_list(link); | |
1958 | if (r < 0) | |
1959 | return r; | |
1960 | ||
1961 | r = link_new_bound_to_list(link); | |
1962 | if (r < 0) | |
1963 | return r; | |
1964 | ||
1965 | r = link_handle_bound_to_list(link); | |
1966 | if (r < 0) | |
1967 | return r; | |
1968 | ||
1969 | return 0; | |
1970 | } | |
1971 | ||
1972 | static void link_free_bound_to_list(Link *link) { | |
1973 | Link *bound_to; | |
1974 | Iterator i; | |
1975 | ||
1976 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
1977 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
1978 | ||
1979 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 1980 | link_dirty(bound_to); |
0d4ad91d AR |
1981 | } |
1982 | ||
1983 | return; | |
1984 | } | |
1985 | ||
1986 | static void link_free_bound_by_list(Link *link) { | |
1987 | Link *bound_by; | |
1988 | Iterator i; | |
1989 | ||
1990 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
1991 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
1992 | ||
1993 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 1994 | link_dirty(bound_by); |
0d4ad91d AR |
1995 | link_handle_bound_to_list(bound_by); |
1996 | } | |
1997 | } | |
1998 | ||
1999 | return; | |
2000 | } | |
2001 | ||
2002 | static void link_free_carrier_maps(Link *link) { | |
2003 | bool list_updated = false; | |
2004 | ||
2005 | assert(link); | |
2006 | ||
2007 | if (!hashmap_isempty(link->bound_to_links)) { | |
2008 | link_free_bound_to_list(link); | |
2009 | list_updated = true; | |
2010 | } | |
2011 | ||
2012 | if (!hashmap_isempty(link->bound_by_links)) { | |
2013 | link_free_bound_by_list(link); | |
2014 | list_updated = true; | |
2015 | } | |
2016 | ||
2017 | if (list_updated) | |
84de38c5 | 2018 | link_dirty(link); |
0d4ad91d AR |
2019 | |
2020 | return; | |
2021 | } | |
2022 | ||
2023 | void link_drop(Link *link) { | |
2024 | if (!link || link->state == LINK_STATE_LINGER) | |
2025 | return; | |
2026 | ||
2027 | link_set_state(link, LINK_STATE_LINGER); | |
2028 | ||
2029 | link_free_carrier_maps(link); | |
2030 | ||
6a7a4e4d | 2031 | log_link_debug(link, "Link removed"); |
0d4ad91d | 2032 | |
84de38c5 | 2033 | (void)unlink(link->state_file); |
0d4ad91d AR |
2034 | link_unref(link); |
2035 | ||
2036 | return; | |
2037 | } | |
2038 | ||
3f265037 | 2039 | static int link_joined(Link *link) { |
f882c247 TG |
2040 | int r; |
2041 | ||
ef1ba606 | 2042 | assert(link); |
f5be5601 | 2043 | assert(link->network); |
dd3efc09 | 2044 | |
0d4ad91d AR |
2045 | if (!hashmap_isempty(link->bound_to_links)) { |
2046 | r = link_handle_bound_to_list(link); | |
2047 | if (r < 0) | |
2048 | return r; | |
2049 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
2050 | r = link_up(link); |
2051 | if (r < 0) { | |
2052 | link_enter_failed(link); | |
2053 | return r; | |
2054 | } | |
ef1ba606 | 2055 | } |
f882c247 | 2056 | |
9ed794a3 | 2057 | if (link->network->bridge) { |
e1853b00 | 2058 | r = link_set_bridge(link); |
6a7a4e4d LP |
2059 | if (r < 0) |
2060 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
2061 | } |
2062 | ||
ffff9abe TJ |
2063 | if (link->network->use_br_vlan && |
2064 | (link->network->bridge || streq_ptr("bridge", link->kind))) { | |
13b498f9 TJ |
2065 | r = link_set_bridge_vlan(link); |
2066 | if (r < 0) | |
2067 | log_link_error_errno(link, r, "Could not set bridge vlan: %m"); | |
2068 | } | |
2069 | ||
fb6730c4 | 2070 | return link_enter_set_addresses(link); |
02b59d57 TG |
2071 | } |
2072 | ||
62e2d5bb | 2073 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 2074 | _cleanup_link_unref_ Link *link = userdata; |
02b59d57 TG |
2075 | int r; |
2076 | ||
1746cf2a | 2077 | assert(link); |
ef1ba606 | 2078 | assert(link->network); |
02b59d57 | 2079 | |
313cefa1 | 2080 | link->enslaving--; |
52433f6b | 2081 | |
5da8149f | 2082 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
2083 | return 1; |
2084 | ||
1c4baffc | 2085 | r = sd_netlink_message_get_errno(m); |
856f962c | 2086 | if (r < 0 && r != -EEXIST) { |
a2fae7bb | 2087 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
ef1ba606 TG |
2088 | link_enter_failed(link); |
2089 | return 1; | |
ba179154 | 2090 | } else |
6a7a4e4d | 2091 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 2092 | |
856f962c | 2093 | if (link->enslaving <= 0) |
3f265037 | 2094 | link_joined(link); |
02b59d57 TG |
2095 | |
2096 | return 1; | |
2097 | } | |
2098 | ||
3f265037 | 2099 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 2100 | NetDev *netdev; |
672682a6 | 2101 | Iterator i; |
02b59d57 TG |
2102 | int r; |
2103 | ||
2104 | assert(link); | |
2105 | assert(link->network); | |
8434fd5c | 2106 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 2107 | |
e331e246 | 2108 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 2109 | |
84de38c5 | 2110 | link_dirty(link); |
fe8db0c5 | 2111 | |
7951dea2 SS |
2112 | if (!link->network->bridge && |
2113 | !link->network->bond && | |
6cb955c6 | 2114 | !link->network->vrf && |
6a0a2f86 | 2115 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 2116 | return link_joined(link); |
02b59d57 | 2117 | |
d9c67ea1 | 2118 | if (link->network->bond) { |
f2341e0a LP |
2119 | log_struct(LOG_DEBUG, |
2120 | LOG_LINK_INTERFACE(link), | |
2121 | LOG_NETDEV_INTERFACE(link->network->bond), | |
2122 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname), | |
2123 | NULL); | |
2124 | ||
2125 | r = netdev_join(link->network->bond, link, netdev_join_handler); | |
52433f6b | 2126 | if (r < 0) { |
f2341e0a LP |
2127 | log_struct_errno(LOG_WARNING, r, |
2128 | LOG_LINK_INTERFACE(link), | |
2129 | LOG_NETDEV_INTERFACE(link->network->bond), | |
2130 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname), | |
2131 | NULL); | |
2132 | ||
52433f6b TG |
2133 | link_enter_failed(link); |
2134 | return r; | |
2135 | } | |
2136 | ||
313cefa1 | 2137 | link->enslaving++; |
0ad6148e MO |
2138 | } |
2139 | ||
d9c67ea1 | 2140 | if (link->network->bridge) { |
f2341e0a LP |
2141 | log_struct(LOG_DEBUG, |
2142 | LOG_LINK_INTERFACE(link), | |
2143 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
2144 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname), | |
2145 | NULL); | |
2146 | ||
2147 | r = netdev_join(link->network->bridge, link, netdev_join_handler); | |
0ad6148e | 2148 | if (r < 0) { |
f2341e0a LP |
2149 | log_struct_errno(LOG_WARNING, r, |
2150 | LOG_LINK_INTERFACE(link), | |
2151 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
2152 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname), | |
2153 | NULL), | |
0ad6148e MO |
2154 | link_enter_failed(link); |
2155 | return r; | |
2156 | } | |
2157 | ||
313cefa1 | 2158 | link->enslaving++; |
52433f6b TG |
2159 | } |
2160 | ||
6cb955c6 AR |
2161 | if (link->network->vrf) { |
2162 | log_struct(LOG_DEBUG, | |
2163 | LOG_LINK_INTERFACE(link), | |
2164 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
2165 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname), | |
2166 | NULL); | |
2167 | r = netdev_join(link->network->vrf, link, netdev_join_handler); | |
2168 | if (r < 0) { | |
2169 | log_struct_errno(LOG_WARNING, r, | |
2170 | LOG_LINK_INTERFACE(link), | |
2171 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
2172 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname), | |
2173 | NULL); | |
2174 | link_enter_failed(link); | |
2175 | return r; | |
2176 | } | |
2177 | ||
2178 | link->enslaving++; | |
2179 | } | |
2180 | ||
6a0a2f86 | 2181 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 2182 | |
f2341e0a LP |
2183 | log_struct(LOG_DEBUG, |
2184 | LOG_LINK_INTERFACE(link), | |
2185 | LOG_NETDEV_INTERFACE(netdev), | |
2186 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname), | |
2187 | NULL); | |
2188 | ||
2189 | r = netdev_join(netdev, link, netdev_join_handler); | |
7951dea2 | 2190 | if (r < 0) { |
f2341e0a LP |
2191 | log_struct_errno(LOG_WARNING, r, |
2192 | LOG_LINK_INTERFACE(link), | |
2193 | LOG_NETDEV_INTERFACE(netdev), | |
2194 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname), | |
2195 | NULL); | |
326cb406 SS |
2196 | link_enter_failed(link); |
2197 | return r; | |
2198 | } | |
2199 | ||
313cefa1 | 2200 | link->enslaving++; |
326cb406 SS |
2201 | } |
2202 | ||
ef1ba606 TG |
2203 | return 0; |
2204 | } | |
2205 | ||
769d324c | 2206 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
2207 | int r; |
2208 | ||
765afd5c | 2209 | if (!link_ipv4_forward_enabled(link)) |
15dee3f0 LP |
2210 | return 0; |
2211 | ||
765afd5c LP |
2212 | /* We propagate the forwarding flag from one interface to the |
2213 | * global setting one way. This means: as long as at least one | |
2214 | * interface was configured at any time that had IP forwarding | |
2215 | * enabled the setting will stay on for good. We do this | |
2216 | * primarily to keep IPv4 and IPv6 packet forwarding behaviour | |
2217 | * somewhat in sync (see below). */ | |
15dee3f0 | 2218 | |
765afd5c | 2219 | r = write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2220 | if (r < 0) |
765afd5c | 2221 | log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m"); |
43c6d5ab | 2222 | |
769d324c LP |
2223 | return 0; |
2224 | } | |
2225 | ||
2226 | static int link_set_ipv6_forward(Link *link) { | |
769d324c LP |
2227 | int r; |
2228 | ||
765afd5c | 2229 | if (!link_ipv6_forward_enabled(link)) |
8add5f79 NO |
2230 | return 0; |
2231 | ||
61233823 | 2232 | /* On Linux, the IPv6 stack does not know a per-interface |
765afd5c LP |
2233 | * packet forwarding setting: either packet forwarding is on |
2234 | * for all, or off for all. We hence don't bother with a | |
2235 | * per-interface setting, but simply propagate the interface | |
2236 | * flag, if it is set, to the global flag, one-way. Note that | |
2237 | * while IPv4 would allow a per-interface flag, we expose the | |
2238 | * same behaviour there and also propagate the setting from | |
2239 | * one to all, to keep things simple (see above). */ | |
15dee3f0 | 2240 | |
765afd5c | 2241 | r = write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2242 | if (r < 0) |
765afd5c | 2243 | log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m"); |
5a8bcb67 LP |
2244 | |
2245 | return 0; | |
2246 | } | |
2247 | ||
49092e22 | 2248 | static int link_set_ipv6_privacy_extensions(Link *link) { |
1f0d9695 LP |
2249 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; |
2250 | IPv6PrivacyExtensions s; | |
49092e22 SS |
2251 | const char *p = NULL; |
2252 | int r; | |
2253 | ||
1f0d9695 | 2254 | s = link_ipv6_privacy_extensions(link); |
66a6bd68 | 2255 | if (s < 0) |
49092e22 SS |
2256 | return 0; |
2257 | ||
2258 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/use_tempaddr"); | |
66a6bd68 | 2259 | xsprintf(buf, "%u", (unsigned) link->network->ipv6_privacy_extensions); |
49092e22 | 2260 | |
eb3da901 LP |
2261 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2262 | if (r < 0) | |
49092e22 SS |
2263 | log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m"); |
2264 | ||
2265 | return 0; | |
2266 | } | |
2267 | ||
4f2e437a | 2268 | static int link_set_ipv6_accept_ra(Link *link) { |
3b015d40 | 2269 | const char *p = NULL; |
4f2e437a SS |
2270 | int r; |
2271 | ||
2272 | /* Make this a NOP if IPv6 is not available */ | |
2273 | if (!socket_ipv6_is_supported()) | |
2274 | return 0; | |
2275 | ||
2276 | if (link->flags & IFF_LOOPBACK) | |
2277 | return 0; | |
2278 | ||
d68e2e59 LP |
2279 | if (!link->network) |
2280 | return 0; | |
2281 | ||
4f2e437a | 2282 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/accept_ra"); |
4f2e437a | 2283 | |
0053598f | 2284 | /* We handle router advertisements ourselves, tell the kernel to GTFO */ |
fe307276 | 2285 | r = write_string_file(p, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2286 | if (r < 0) |
fe307276 | 2287 | log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m"); |
4f2e437a SS |
2288 | |
2289 | return 0; | |
2290 | } | |
2291 | ||
8749cbcd | 2292 | static int link_set_ipv6_dad_transmits(Link *link) { |
66a6bd68 | 2293 | char buf[DECIMAL_STR_MAX(int) + 1]; |
8749cbcd SS |
2294 | const char *p = NULL; |
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 | ||
8749cbcd SS |
2307 | if (link->network->ipv6_dad_transmits < 0) |
2308 | return 0; | |
2309 | ||
2310 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/dad_transmits"); | |
66a6bd68 | 2311 | xsprintf(buf, "%i", link->network->ipv6_dad_transmits); |
8749cbcd | 2312 | |
eb3da901 LP |
2313 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2314 | if (r < 0) | |
8749cbcd | 2315 | log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m"); |
8749cbcd SS |
2316 | |
2317 | return 0; | |
2318 | } | |
2319 | ||
b69c3180 | 2320 | static int link_set_ipv6_hop_limit(Link *link) { |
66a6bd68 | 2321 | char buf[DECIMAL_STR_MAX(int) + 1]; |
b69c3180 SS |
2322 | const char *p = NULL; |
2323 | int r; | |
2324 | ||
2325 | /* Make this a NOP if IPv6 is not available */ | |
2326 | if (!socket_ipv6_is_supported()) | |
2327 | return 0; | |
2328 | ||
2329 | if (link->flags & IFF_LOOPBACK) | |
2330 | return 0; | |
2331 | ||
d68e2e59 LP |
2332 | if (!link->network) |
2333 | return 0; | |
2334 | ||
b69c3180 SS |
2335 | if (link->network->ipv6_hop_limit < 0) |
2336 | return 0; | |
2337 | ||
2338 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/hop_limit"); | |
66a6bd68 | 2339 | xsprintf(buf, "%i", link->network->ipv6_hop_limit); |
b69c3180 | 2340 | |
eb3da901 LP |
2341 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2342 | if (r < 0) | |
b69c3180 | 2343 | log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m"); |
b69c3180 SS |
2344 | |
2345 | return 0; | |
2346 | } | |
2347 | ||
5e5b137a TG |
2348 | static int link_drop_foreign_config(Link *link) { |
2349 | Address *address; | |
2350 | Route *route; | |
2351 | Iterator i; | |
2352 | int r; | |
2353 | ||
2354 | SET_FOREACH(address, link->addresses_foreign, i) { | |
fe307276 | 2355 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5e5b137a TG |
2356 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) |
2357 | continue; | |
2358 | ||
2359 | r = address_remove(address, link, link_address_remove_handler); | |
2360 | if (r < 0) | |
2361 | return r; | |
2362 | } | |
2363 | ||
2364 | SET_FOREACH(route, link->routes_foreign, i) { | |
fe307276 | 2365 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2366 | if (route->protocol == RTPROT_KERNEL) |
2367 | continue; | |
2368 | ||
3fb1ac5d | 2369 | r = route_remove(route, link, link_route_remove_handler); |
5e5b137a TG |
2370 | if (r < 0) |
2371 | return r; | |
2372 | } | |
2373 | ||
2374 | return 0; | |
2375 | } | |
2376 | ||
3104883d SS |
2377 | static int link_drop_config(Link *link) { |
2378 | Address *address; | |
2379 | Route *route; | |
2380 | Iterator i; | |
2381 | int r; | |
2382 | ||
2383 | SET_FOREACH(address, link->addresses, i) { | |
2384 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
2385 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) | |
2386 | continue; | |
2387 | ||
2388 | r = address_remove(address, link, link_address_remove_handler); | |
2389 | if (r < 0) | |
2390 | return r; | |
2391 | } | |
2392 | ||
2393 | SET_FOREACH(route, link->routes, i) { | |
2394 | /* do not touch routes managed by the kernel */ | |
2395 | if (route->protocol == RTPROT_KERNEL) | |
2396 | continue; | |
2397 | ||
2398 | r = route_remove(route, link, link_route_remove_handler); | |
2399 | if (r < 0) | |
2400 | return r; | |
2401 | } | |
2402 | ||
2403 | return 0; | |
2404 | } | |
2405 | ||
273eec24 LP |
2406 | static int link_update_lldp(Link *link) { |
2407 | int r; | |
2408 | ||
2409 | assert(link); | |
2410 | ||
2411 | if (!link->lldp) | |
2412 | return 0; | |
2413 | ||
2414 | if (link->flags & IFF_UP) { | |
2415 | r = sd_lldp_start(link->lldp); | |
2416 | if (r > 0) | |
2417 | log_link_debug(link, "Started LLDP."); | |
2418 | } else { | |
2419 | r = sd_lldp_stop(link->lldp); | |
2420 | if (r > 0) | |
2421 | log_link_debug(link, "Stopped LLDP."); | |
2422 | } | |
2423 | ||
2424 | return r; | |
2425 | } | |
2426 | ||
a748b692 | 2427 | static int link_configure(Link *link) { |
02b59d57 TG |
2428 | int r; |
2429 | ||
ef1ba606 | 2430 | assert(link); |
b22d8a00 | 2431 | assert(link->network); |
8434fd5c | 2432 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 2433 | |
02e28621 ДГ |
2434 | /* Drop foreign config, but ignore loopback or critical devices. |
2435 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
2436 | if (!(link->flags & IFF_LOOPBACK) && !(link->network->dhcp_critical)) { | |
e5d44b34 CH |
2437 | r = link_drop_foreign_config(link); |
2438 | if (r < 0) | |
2439 | return r; | |
2440 | } | |
5e5b137a | 2441 | |
b98b483b AR |
2442 | r = link_set_bridge_fdb(link); |
2443 | if (r < 0) | |
2444 | return r; | |
2445 | ||
23d8b221 SS |
2446 | r = link_set_proxy_arp(link); |
2447 | if (r < 0) | |
2448 | return r; | |
2449 | ||
769d324c LP |
2450 | r = link_set_ipv4_forward(link); |
2451 | if (r < 0) | |
2452 | return r; | |
2453 | ||
2454 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2455 | if (r < 0) |
2456 | return r; | |
2457 | ||
49092e22 SS |
2458 | r = link_set_ipv6_privacy_extensions(link); |
2459 | if (r < 0) | |
2460 | return r; | |
2461 | ||
4f2e437a SS |
2462 | r = link_set_ipv6_accept_ra(link); |
2463 | if (r < 0) | |
2464 | return r; | |
2465 | ||
8749cbcd SS |
2466 | r = link_set_ipv6_dad_transmits(link); |
2467 | if (r < 0) | |
2468 | return r; | |
b69c3180 SS |
2469 | |
2470 | r = link_set_ipv6_hop_limit(link); | |
2471 | if (r < 0) | |
2472 | return r; | |
8749cbcd | 2473 | |
99d2baa2 SS |
2474 | r = link_set_flags(link); |
2475 | if (r < 0) | |
2476 | return r; | |
2477 | ||
78c958f8 | 2478 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 2479 | r = ipv4ll_configure(link); |
eb34d4af TG |
2480 | if (r < 0) |
2481 | return r; | |
2482 | } | |
2483 | ||
78c958f8 | 2484 | if (link_dhcp4_enabled(link)) { |
3c9b8860 | 2485 | r = dhcp4_configure(link); |
eb34d4af TG |
2486 | if (r < 0) |
2487 | return r; | |
eb34d4af TG |
2488 | } |
2489 | ||
78c958f8 | 2490 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2491 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2492 | if (r < 0) | |
2493 | return r; | |
2494 | ||
2495 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2496 | if (r < 0) | |
2497 | return r; | |
dd43110f TG |
2498 | } |
2499 | ||
62379e88 TG |
2500 | if (link_dhcp6_enabled(link) || |
2501 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
2502 | r = dhcp6_configure(link); |
2503 | if (r < 0) | |
2504 | return r; | |
2505 | } | |
2506 | ||
2507 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 2508 | r = ndisc_configure(link); |
4138fb2c PF |
2509 | if (r < 0) |
2510 | return r; | |
2511 | } | |
2512 | ||
8e1ad1ea | 2513 | if (link_lldp_rx_enabled(link)) { |
fc6a313b LP |
2514 | r = sd_lldp_new(&link->lldp); |
2515 | if (r < 0) | |
2516 | return r; | |
2517 | ||
2518 | r = sd_lldp_set_ifindex(link->lldp, link->ifindex); | |
34437b4f LP |
2519 | if (r < 0) |
2520 | return r; | |
2521 | ||
2522 | r = sd_lldp_match_capabilities(link->lldp, | |
2523 | link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ? | |
6afa6767 BG |
2524 | SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS : |
2525 | SD_LLDP_SYSTEM_CAPABILITIES_ALL); | |
ce43e484 SS |
2526 | if (r < 0) |
2527 | return r; | |
2528 | ||
b553a6b1 LP |
2529 | r = sd_lldp_set_filter_address(link->lldp, &link->mac); |
2530 | if (r < 0) | |
2531 | return r; | |
2532 | ||
ce43e484 SS |
2533 | r = sd_lldp_attach_event(link->lldp, NULL, 0); |
2534 | if (r < 0) | |
2535 | return r; | |
49699bac | 2536 | |
032b27f5 | 2537 | r = sd_lldp_set_callback(link->lldp, lldp_handler, link); |
49699bac SS |
2538 | if (r < 0) |
2539 | return r; | |
273eec24 LP |
2540 | |
2541 | r = link_update_lldp(link); | |
2542 | if (r < 0) | |
2543 | return r; | |
ce43e484 SS |
2544 | } |
2545 | ||
a61bb41c | 2546 | if (link_has_carrier(link)) { |
1e9be60b TG |
2547 | r = link_acquire_conf(link); |
2548 | if (r < 0) | |
2549 | return r; | |
cc544d5f | 2550 | } |
1e9be60b | 2551 | |
3f265037 | 2552 | return link_enter_join_netdev(link); |
505f8da7 TG |
2553 | } |
2554 | ||
1c4baffc | 2555 | static int link_initialized_and_synced(sd_netlink *rtnl, sd_netlink_message *m, |
3c9b8860 | 2556 | void *userdata) { |
5da8149f | 2557 | _cleanup_link_unref_ Link *link = userdata; |
505f8da7 | 2558 | Network *network; |
505f8da7 TG |
2559 | int r; |
2560 | ||
2561 | assert(link); | |
2562 | assert(link->ifname); | |
2563 | assert(link->manager); | |
2564 | ||
8434fd5c | 2565 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 2566 | return 1; |
505f8da7 | 2567 | |
6a7a4e4d | 2568 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 2569 | |
0d4ad91d AR |
2570 | r = link_new_bound_by_list(link); |
2571 | if (r < 0) | |
2572 | return r; | |
2573 | ||
2574 | r = link_handle_bound_by_list(link); | |
2575 | if (r < 0) | |
2576 | return r; | |
2577 | ||
c4a03a56 TG |
2578 | if (!link->network) { |
2579 | r = network_get(link->manager, link->udev_device, link->ifname, | |
2580 | &link->mac, &network); | |
2581 | if (r == -ENOENT) { | |
2582 | link_enter_unmanaged(link); | |
2583 | return 1; | |
2584 | } else if (r < 0) | |
2585 | return r; | |
505f8da7 | 2586 | |
c4a03a56 TG |
2587 | if (link->flags & IFF_LOOPBACK) { |
2588 | if (network->link_local != ADDRESS_FAMILY_NO) | |
2589 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 2590 | |
c4a03a56 TG |
2591 | if (network->dhcp != ADDRESS_FAMILY_NO) |
2592 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 2593 | |
c4a03a56 TG |
2594 | if (network->dhcp_server) |
2595 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
2596 | } | |
bd2efe92 | 2597 | |
c4a03a56 TG |
2598 | r = network_apply(link->manager, network, link); |
2599 | if (r < 0) | |
2600 | return r; | |
2601 | } | |
505f8da7 | 2602 | |
0d4ad91d AR |
2603 | r = link_new_bound_to_list(link); |
2604 | if (r < 0) | |
2605 | return r; | |
2606 | ||
a748b692 TG |
2607 | r = link_configure(link); |
2608 | if (r < 0) | |
2609 | return r; | |
2610 | ||
5da8149f | 2611 | return 1; |
505f8da7 TG |
2612 | } |
2613 | ||
4f561e8e | 2614 | int link_initialized(Link *link, struct udev_device *device) { |
4afd3348 | 2615 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
2616 | int r; |
2617 | ||
2618 | assert(link); | |
2619 | assert(link->manager); | |
2620 | assert(link->manager->rtnl); | |
2621 | assert(device); | |
2622 | ||
8434fd5c | 2623 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
2624 | return 0; |
2625 | ||
679b3605 TG |
2626 | if (link->udev_device) |
2627 | return 0; | |
2628 | ||
79008bdd | 2629 | log_link_debug(link, "udev initialized link"); |
4f561e8e TG |
2630 | |
2631 | link->udev_device = udev_device_ref(device); | |
2632 | ||
3c9b8860 TG |
2633 | /* udev has initialized the link, but we don't know if we have yet |
2634 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
2635 | * when it returns we know that the pending NEWLINKs have already been | |
2636 | * processed and that we are up-to-date */ | |
4f561e8e | 2637 | |
3c9b8860 TG |
2638 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
2639 | link->ifindex); | |
4f561e8e TG |
2640 | if (r < 0) |
2641 | return r; | |
2642 | ||
1c4baffc | 2643 | r = sd_netlink_call_async(link->manager->rtnl, req, |
3c9b8860 | 2644 | link_initialized_and_synced, link, 0, NULL); |
4f561e8e TG |
2645 | if (r < 0) |
2646 | return r; | |
2647 | ||
5da8149f TG |
2648 | link_ref(link); |
2649 | ||
4f561e8e TG |
2650 | return 0; |
2651 | } | |
2652 | ||
c4a03a56 | 2653 | static int link_load(Link *link) { |
0bc70f1d TG |
2654 | _cleanup_free_ char *network_file = NULL, |
2655 | *addresses = NULL, | |
f703cc2c | 2656 | *routes = NULL, |
0bc70f1d TG |
2657 | *dhcp4_address = NULL, |
2658 | *ipv4ll_address = NULL; | |
2659 | union in_addr_union address; | |
f703cc2c | 2660 | union in_addr_union route_dst; |
c598ac76 | 2661 | const char *p; |
c4a03a56 TG |
2662 | int r; |
2663 | ||
2664 | assert(link); | |
2665 | ||
2666 | r = parse_env_file(link->state_file, NEWLINE, | |
2667 | "NETWORK_FILE", &network_file, | |
2668 | "ADDRESSES", &addresses, | |
f703cc2c | 2669 | "ROUTES", &routes, |
0bc70f1d TG |
2670 | "DHCP4_ADDRESS", &dhcp4_address, |
2671 | "IPV4LL_ADDRESS", &ipv4ll_address, | |
c4a03a56 TG |
2672 | NULL); |
2673 | if (r < 0 && r != -ENOENT) | |
2674 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
2675 | ||
2676 | if (network_file) { | |
2677 | Network *network; | |
2678 | char *suffix; | |
2679 | ||
2680 | /* drop suffix */ | |
2681 | suffix = strrchr(network_file, '.'); | |
2682 | if (!suffix) { | |
2683 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
2684 | goto network_file_fail; | |
2685 | } | |
2686 | *suffix = '\0'; | |
2687 | ||
2688 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
2689 | if (r < 0) { | |
2690 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
2691 | goto network_file_fail; | |
2692 | } | |
2693 | ||
2694 | r = network_apply(link->manager, network, link); | |
2695 | if (r < 0) | |
2696 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
2697 | } | |
2698 | ||
2699 | network_file_fail: | |
2700 | ||
2701 | if (addresses) { | |
c598ac76 | 2702 | p = addresses; |
c4a03a56 | 2703 | |
c598ac76 TG |
2704 | for (;;) { |
2705 | _cleanup_free_ char *address_str = NULL; | |
c4a03a56 TG |
2706 | char *prefixlen_str; |
2707 | int family; | |
2708 | unsigned char prefixlen; | |
c4a03a56 | 2709 | |
c598ac76 TG |
2710 | r = extract_first_word(&p, &address_str, NULL, 0); |
2711 | if (r < 0) { | |
2712 | log_link_debug_errno(link, r, "Failed to extract next address string: %m"); | |
2713 | continue; | |
928bc597 TA |
2714 | } |
2715 | if (r == 0) | |
c598ac76 TG |
2716 | break; |
2717 | ||
2718 | prefixlen_str = strchr(address_str, '/'); | |
c4a03a56 | 2719 | if (!prefixlen_str) { |
c598ac76 | 2720 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); |
c4a03a56 TG |
2721 | continue; |
2722 | } | |
2723 | ||
313cefa1 | 2724 | *prefixlen_str++ = '\0'; |
c4a03a56 TG |
2725 | |
2726 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); | |
2727 | if (r != 1) { | |
2728 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
2729 | continue; | |
2730 | } | |
2731 | ||
c598ac76 | 2732 | r = in_addr_from_string_auto(address_str, &family, &address); |
c4a03a56 | 2733 | if (r < 0) { |
c598ac76 | 2734 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); |
c4a03a56 TG |
2735 | continue; |
2736 | } | |
2737 | ||
2738 | r = address_add(link, family, &address, prefixlen, NULL); | |
2739 | if (r < 0) | |
2740 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
2741 | } | |
2742 | } | |
2743 | ||
f703cc2c | 2744 | if (routes) { |
74544b4e TA |
2745 | p = routes; |
2746 | ||
c598ac76 | 2747 | for (;;) { |
f833694d | 2748 | Route *route; |
c598ac76 | 2749 | _cleanup_free_ char *route_str = NULL; |
4afd3348 | 2750 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; |
f833694d | 2751 | usec_t lifetime; |
f703cc2c TG |
2752 | char *prefixlen_str; |
2753 | int family; | |
2754 | unsigned char prefixlen, tos, table; | |
2755 | uint32_t priority; | |
2756 | ||
c598ac76 TG |
2757 | r = extract_first_word(&p, &route_str, NULL, 0); |
2758 | if (r < 0) { | |
2759 | log_link_debug_errno(link, r, "Failed to extract next route string: %m"); | |
2760 | continue; | |
928bc597 TA |
2761 | } |
2762 | if (r == 0) | |
c598ac76 TG |
2763 | break; |
2764 | ||
2765 | prefixlen_str = strchr(route_str, '/'); | |
f703cc2c | 2766 | if (!prefixlen_str) { |
c598ac76 | 2767 | log_link_debug(link, "Failed to parse route %s", route_str); |
f703cc2c TG |
2768 | continue; |
2769 | } | |
2770 | ||
313cefa1 | 2771 | *prefixlen_str++ = '\0'; |
f703cc2c | 2772 | |
f833694d TG |
2773 | r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%hhu/"USEC_FMT, &prefixlen, &tos, &priority, &table, &lifetime); |
2774 | if (r != 5) { | |
2775 | log_link_debug(link, | |
2776 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
2777 | prefixlen_str); | |
f703cc2c TG |
2778 | continue; |
2779 | } | |
2780 | ||
c598ac76 | 2781 | r = in_addr_from_string_auto(route_str, &family, &route_dst); |
f703cc2c | 2782 | if (r < 0) { |
c598ac76 | 2783 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); |
f703cc2c TG |
2784 | continue; |
2785 | } | |
2786 | ||
f833694d | 2787 | r = route_add(link, family, &route_dst, prefixlen, tos, priority, table, &route); |
f703cc2c TG |
2788 | if (r < 0) |
2789 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f833694d TG |
2790 | |
2791 | if (lifetime != USEC_INFINITY) { | |
2792 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime, | |
2793 | 0, route_expire_handler, route); | |
2794 | if (r < 0) | |
2795 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); | |
2796 | } | |
2797 | ||
2798 | route->lifetime = lifetime; | |
2799 | sd_event_source_unref(route->expire); | |
2800 | route->expire = expire; | |
2801 | expire = NULL; | |
f703cc2c TG |
2802 | } |
2803 | } | |
2804 | ||
0bc70f1d TG |
2805 | if (dhcp4_address) { |
2806 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
2807 | if (r < 0) { | |
b68d26b8 | 2808 | log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
2809 | goto dhcp4_address_fail; |
2810 | } | |
2811 | ||
2812 | r = sd_dhcp_client_new(&link->dhcp_client); | |
2813 | if (r < 0) | |
b68d26b8 | 2814 | return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m"); |
0bc70f1d TG |
2815 | |
2816 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
2817 | if (r < 0) | |
b68d26b8 | 2818 | return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
2819 | } |
2820 | ||
2821 | dhcp4_address_fail: | |
2822 | ||
2823 | if (ipv4ll_address) { | |
2824 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
2825 | if (r < 0) { | |
b68d26b8 | 2826 | log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
2827 | goto ipv4ll_address_fail; |
2828 | } | |
2829 | ||
2830 | r = sd_ipv4ll_new(&link->ipv4ll); | |
2831 | if (r < 0) | |
b68d26b8 | 2832 | return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m"); |
0bc70f1d TG |
2833 | |
2834 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); | |
2835 | if (r < 0) | |
b68d26b8 | 2836 | return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
2837 | } |
2838 | ||
2839 | ipv4ll_address_fail: | |
2840 | ||
c4a03a56 TG |
2841 | return 0; |
2842 | } | |
2843 | ||
1c4baffc | 2844 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
505f8da7 TG |
2845 | Link *link; |
2846 | _cleanup_udev_device_unref_ struct udev_device *device = NULL; | |
2847 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; | |
2848 | int r; | |
2849 | ||
2850 | assert(m); | |
fbbeb65a | 2851 | assert(m->rtnl); |
505f8da7 TG |
2852 | assert(message); |
2853 | assert(ret); | |
2854 | ||
2855 | r = link_new(m, message, ret); | |
2856 | if (r < 0) | |
2857 | return r; | |
2858 | ||
2859 | link = *ret; | |
2860 | ||
6a7a4e4d | 2861 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 2862 | |
c4a03a56 TG |
2863 | r = link_load(link); |
2864 | if (r < 0) | |
2865 | return r; | |
2866 | ||
75f86906 | 2867 | if (detect_container() <= 0) { |
505f8da7 | 2868 | /* not in a container, udev will be around */ |
ae06ab10 | 2869 | sprintf(ifindex_str, "n%d", link->ifindex); |
505f8da7 | 2870 | device = udev_device_new_from_device_id(m->udev, ifindex_str); |
5c416fc4 TG |
2871 | if (!device) { |
2872 | r = log_link_warning_errno(link, errno, "Could not find udev device: %m"); | |
2873 | goto failed; | |
2874 | } | |
505f8da7 | 2875 | |
3c4cb064 | 2876 | if (udev_device_get_is_initialized(device) <= 0) { |
505f8da7 | 2877 | /* not yet ready */ |
79008bdd | 2878 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 2879 | return 0; |
3c4cb064 | 2880 | } |
505f8da7 | 2881 | |
4f561e8e TG |
2882 | r = link_initialized(link, device); |
2883 | if (r < 0) | |
5c416fc4 | 2884 | goto failed; |
4f561e8e | 2885 | } else { |
5da8149f TG |
2886 | /* we are calling a callback directly, so must take a ref */ |
2887 | link_ref(link); | |
2888 | ||
4f561e8e TG |
2889 | r = link_initialized_and_synced(m->rtnl, NULL, link); |
2890 | if (r < 0) | |
5c416fc4 | 2891 | goto failed; |
4f561e8e | 2892 | } |
505f8da7 | 2893 | |
a748b692 | 2894 | return 0; |
5c416fc4 TG |
2895 | failed: |
2896 | link_enter_failed(link); | |
2897 | return r; | |
a748b692 TG |
2898 | } |
2899 | ||
c601ebf7 | 2900 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
2901 | int r; |
2902 | ||
2903 | assert(link); | |
2904 | ||
2905 | log_link_info(link, "Gained IPv6LL"); | |
2906 | ||
c601ebf7 | 2907 | link->ipv6ll_address = *address; |
e7ab854c TG |
2908 | link_check_ready(link); |
2909 | ||
201f0c91 | 2910 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
e7ab854c TG |
2911 | r = link_acquire_ipv6_conf(link); |
2912 | if (r < 0) { | |
2913 | link_enter_failed(link); | |
2914 | return r; | |
2915 | } | |
2916 | } | |
2917 | ||
2918 | return 0; | |
2919 | } | |
2920 | ||
9c0a72f9 TG |
2921 | static int link_carrier_gained(Link *link) { |
2922 | int r; | |
2923 | ||
2924 | assert(link); | |
2925 | ||
201f0c91 | 2926 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
9c0a72f9 TG |
2927 | r = link_acquire_conf(link); |
2928 | if (r < 0) { | |
2929 | link_enter_failed(link); | |
2930 | return r; | |
2931 | } | |
6fc25497 SS |
2932 | |
2933 | r = link_enter_set_addresses(link); | |
2934 | if (r < 0) | |
2935 | return r; | |
9c0a72f9 TG |
2936 | } |
2937 | ||
0d4ad91d AR |
2938 | r = link_handle_bound_by_list(link); |
2939 | if (r < 0) | |
2940 | return r; | |
2941 | ||
9c0a72f9 TG |
2942 | return 0; |
2943 | } | |
2944 | ||
2945 | static int link_carrier_lost(Link *link) { | |
2946 | int r; | |
2947 | ||
2948 | assert(link); | |
2949 | ||
2950 | r = link_stop_clients(link); | |
2951 | if (r < 0) { | |
2952 | link_enter_failed(link); | |
2953 | return r; | |
2954 | } | |
2955 | ||
3104883d SS |
2956 | r = link_drop_config(link); |
2957 | if (r < 0) | |
2958 | return r; | |
2959 | ||
c436d553 MM |
2960 | if (link->state != LINK_STATE_UNMANAGED) { |
2961 | r = link_drop_foreign_config(link); | |
2962 | if (r < 0) | |
2963 | return r; | |
2964 | } | |
3104883d | 2965 | |
0d4ad91d AR |
2966 | r = link_handle_bound_by_list(link); |
2967 | if (r < 0) | |
2968 | return r; | |
2969 | ||
9c0a72f9 TG |
2970 | return 0; |
2971 | } | |
2972 | ||
2973 | int link_carrier_reset(Link *link) { | |
2974 | int r; | |
2975 | ||
2976 | assert(link); | |
2977 | ||
2978 | if (link_has_carrier(link)) { | |
2979 | r = link_carrier_lost(link); | |
2980 | if (r < 0) | |
2981 | return r; | |
2982 | ||
2983 | r = link_carrier_gained(link); | |
2984 | if (r < 0) | |
2985 | return r; | |
2986 | ||
6a7a4e4d | 2987 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
2988 | } |
2989 | ||
2990 | return 0; | |
2991 | } | |
2992 | ||
1c4baffc | 2993 | int link_update(Link *link, sd_netlink_message *m) { |
c49b33ac | 2994 | struct ether_addr mac; |
ca4e095a | 2995 | const char *ifname; |
afe7fd56 | 2996 | uint32_t mtu; |
a61bb41c | 2997 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
2998 | int r; |
2999 | ||
dd3efc09 | 3000 | assert(link); |
b8941f74 | 3001 | assert(link->ifname); |
22936833 TG |
3002 | assert(m); |
3003 | ||
7619683b TG |
3004 | if (link->state == LINK_STATE_LINGER) { |
3005 | link_ref(link); | |
6a7a4e4d | 3006 | log_link_info(link, "Link readded"); |
e331e246 | 3007 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
3008 | |
3009 | r = link_new_carrier_maps(link); | |
3010 | if (r < 0) | |
3011 | return r; | |
7619683b TG |
3012 | } |
3013 | ||
1c4baffc | 3014 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 3015 | if (r >= 0 && !streq(ifname, link->ifname)) { |
6a7a4e4d | 3016 | log_link_info(link, "Renamed to %s", ifname); |
b8941f74 | 3017 | |
0d4ad91d AR |
3018 | link_free_carrier_maps(link); |
3019 | ||
2fc09a9c DM |
3020 | r = free_and_strdup(&link->ifname, ifname); |
3021 | if (r < 0) | |
3022 | return r; | |
0d4ad91d AR |
3023 | |
3024 | r = link_new_carrier_maps(link); | |
3025 | if (r < 0) | |
3026 | return r; | |
b8941f74 TG |
3027 | } |
3028 | ||
1c4baffc | 3029 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
3030 | if (r >= 0 && mtu > 0) { |
3031 | link->mtu = mtu; | |
3032 | if (!link->original_mtu) { | |
3033 | link->original_mtu = mtu; | |
6a7a4e4d | 3034 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
3035 | } |
3036 | ||
3037 | if (link->dhcp_client) { | |
3c9b8860 TG |
3038 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
3039 | link->mtu); | |
afe7fd56 | 3040 | if (r < 0) { |
6a7a4e4d | 3041 | log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); |
afe7fd56 TG |
3042 | return r; |
3043 | } | |
3044 | } | |
9842de0d | 3045 | } |
69629de9 | 3046 | |
e9189a1f TG |
3047 | /* The kernel may broadcast NEWLINK messages without the MAC address |
3048 | set, simply ignore them. */ | |
1c4baffc | 3049 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 3050 | if (r >= 0) { |
3c9b8860 TG |
3051 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3052 | ETH_ALEN)) { | |
c49b33ac | 3053 | |
3c9b8860 TG |
3054 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3055 | ETH_ALEN); | |
c49b33ac | 3056 | |
79008bdd | 3057 | log_link_debug(link, "MAC address: " |
20861203 TG |
3058 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
3059 | mac.ether_addr_octet[0], | |
3060 | mac.ether_addr_octet[1], | |
3061 | mac.ether_addr_octet[2], | |
3062 | mac.ether_addr_octet[3], | |
3063 | mac.ether_addr_octet[4], | |
3064 | mac.ether_addr_octet[5]); | |
c49b33ac | 3065 | |
20861203 TG |
3066 | if (link->ipv4ll) { |
3067 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
3068 | if (r < 0) |
3069 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 3070 | } |
c49b33ac | 3071 | |
20861203 | 3072 | if (link->dhcp_client) { |
8341a5c3 ZJS |
3073 | const DUID *duid = link_duid(link); |
3074 | ||
3c9b8860 | 3075 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
3076 | (const uint8_t *) &link->mac, |
3077 | sizeof (link->mac), | |
3078 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3079 | if (r < 0) |
3080 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 3081 | |
8341a5c3 ZJS |
3082 | r = sd_dhcp_client_set_iaid_duid(link->dhcp_client, |
3083 | link->network->iaid, | |
3084 | duid->type, | |
3085 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3086 | duid->raw_data_len); | |
413708d1 VK |
3087 | if (r < 0) |
3088 | return log_link_warning_errno(link, r, "Could not update DUID/IAID in DHCP client: %m"); | |
c49b33ac | 3089 | } |
4138fb2c PF |
3090 | |
3091 | if (link->dhcp6_client) { | |
8341a5c3 ZJS |
3092 | const DUID* duid = link_duid(link); |
3093 | ||
4138fb2c | 3094 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
3095 | (const uint8_t *) &link->mac, |
3096 | sizeof (link->mac), | |
3097 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3098 | if (r < 0) |
3099 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 VK |
3100 | |
3101 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
3102 | link->network->iaid); | |
3103 | if (r < 0) | |
3104 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
3105 | ||
8341a5c3 ZJS |
3106 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
3107 | duid->type, | |
3108 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3109 | duid->raw_data_len); | |
413708d1 VK |
3110 | if (r < 0) |
3111 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 3112 | } |
c49b33ac | 3113 | } |
4f882b2a TG |
3114 | } |
3115 | ||
a61bb41c TG |
3116 | had_carrier = link_has_carrier(link); |
3117 | ||
3118 | r = link_update_flags(link, m); | |
3119 | if (r < 0) | |
3120 | return r; | |
3121 | ||
273eec24 LP |
3122 | r = link_update_lldp(link); |
3123 | if (r < 0) | |
3124 | return r; | |
3125 | ||
a61bb41c TG |
3126 | carrier_gained = !had_carrier && link_has_carrier(link); |
3127 | carrier_lost = had_carrier && !link_has_carrier(link); | |
3128 | ||
3129 | if (carrier_gained) { | |
6a7a4e4d | 3130 | log_link_info(link, "Gained carrier"); |
a61bb41c | 3131 | |
9c0a72f9 TG |
3132 | r = link_carrier_gained(link); |
3133 | if (r < 0) | |
3134 | return r; | |
a61bb41c | 3135 | } else if (carrier_lost) { |
6a7a4e4d | 3136 | log_link_info(link, "Lost carrier"); |
a61bb41c | 3137 | |
9c0a72f9 TG |
3138 | r = link_carrier_lost(link); |
3139 | if (r < 0) | |
a61bb41c | 3140 | return r; |
a61bb41c TG |
3141 | } |
3142 | ||
3143 | return 0; | |
dd3efc09 | 3144 | } |
fe8db0c5 | 3145 | |
b295beea LP |
3146 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
3147 | bool space = false; | |
3148 | Iterator i; | |
3149 | Link *link; | |
3150 | ||
3151 | assert(f); | |
3152 | assert(prefix); | |
3153 | ||
3154 | if (hashmap_isempty(h)) | |
3155 | return; | |
3156 | ||
3157 | fputs(prefix, f); | |
3158 | HASHMAP_FOREACH(link, h, i) { | |
3159 | if (space) | |
3160 | fputc(' ', f); | |
3161 | ||
3162 | fprintf(f, "%i", link->ifindex); | |
3163 | space = true; | |
3164 | } | |
3165 | ||
3166 | fputc('\n', f); | |
3167 | } | |
3168 | ||
fe8db0c5 | 3169 | int link_save(Link *link) { |
68a8723c | 3170 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 3171 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 3172 | const char *admin_state, *oper_state; |
e7780c8d | 3173 | Address *a; |
c1eb9872 | 3174 | Route *route; |
e7780c8d | 3175 | Iterator i; |
fe8db0c5 TG |
3176 | int r; |
3177 | ||
3178 | assert(link); | |
3179 | assert(link->state_file); | |
68a8723c | 3180 | assert(link->lease_file); |
bbf7c048 TG |
3181 | assert(link->manager); |
3182 | ||
370e9930 TG |
3183 | if (link->state == LINK_STATE_LINGER) { |
3184 | unlink(link->state_file); | |
3185 | return 0; | |
3186 | } | |
3187 | ||
34437b4f LP |
3188 | link_lldp_save(link); |
3189 | ||
deb2e523 TG |
3190 | admin_state = link_state_to_string(link->state); |
3191 | assert(admin_state); | |
3192 | ||
e375dcde TG |
3193 | oper_state = link_operstate_to_string(link->operstate); |
3194 | assert(oper_state); | |
deb2e523 | 3195 | |
fe8db0c5 TG |
3196 | r = fopen_temporary(link->state_file, &f, &temp_path); |
3197 | if (r < 0) | |
6a7a4e4d | 3198 | goto fail; |
fe8db0c5 TG |
3199 | |
3200 | fchmod(fileno(f), 0644); | |
3201 | ||
3202 | fprintf(f, | |
3203 | "# This is private data. Do not parse.\n" | |
deb2e523 | 3204 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
3205 | "OPER_STATE=%s\n", |
3206 | admin_state, oper_state); | |
fe8db0c5 | 3207 | |
bcb7a07e | 3208 | if (link->network) { |
ea352b40 | 3209 | bool space; |
07bdc70d | 3210 | sd_dhcp6_lease *dhcp6_lease = NULL; |
b2a81c0b LP |
3211 | const char *dhcp_domainname = NULL; |
3212 | char **dhcp6_domains = NULL; | |
07bdc70d PF |
3213 | |
3214 | if (link->dhcp6_client) { | |
4058d339 TG |
3215 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
3216 | if (r < 0 && r != -ENOMSG) | |
07bdc70d PF |
3217 | log_link_debug(link, "No DHCPv6 lease"); |
3218 | } | |
b0e39c82 | 3219 | |
adc5b2e2 TG |
3220 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
3221 | ||
b0e39c82 | 3222 | fputs("DNS=", f); |
ea352b40 | 3223 | space = false; |
3df9bec5 | 3224 | fputstrv(f, link->network->dns, NULL, &space); |
d5314fff | 3225 | |
27cb34f5 | 3226 | if (link->network->dhcp_use_dns && |
b0e39c82 TG |
3227 | link->dhcp_lease) { |
3228 | const struct in_addr *addresses; | |
3229 | ||
3230 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
3231 | if (r > 0) { | |
ea352b40 LP |
3232 | if (space) |
3233 | fputc(' ', f); | |
b0e39c82 | 3234 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3235 | space = true; |
3236 | } | |
3237 | } | |
3238 | ||
27cb34f5 | 3239 | if (link->network->dhcp_use_dns && dhcp6_lease) { |
07bdc70d PF |
3240 | struct in6_addr *in6_addrs; |
3241 | ||
3242 | r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs); | |
3243 | if (r > 0) { | |
3244 | if (space) | |
3245 | fputc(' ', f); | |
3246 | serialize_in6_addrs(f, in6_addrs, r); | |
1e7a0e21 LP |
3247 | space = true; |
3248 | } | |
3249 | } | |
3250 | ||
3251 | /* Make sure to flush out old entries before we use the NDISC data */ | |
3252 | ndisc_vacuum(link); | |
3253 | ||
3254 | if (link->network->dhcp_use_dns && link->ndisc_rdnss) { | |
3255 | NDiscRDNSS *dd; | |
3256 | ||
3257 | SET_FOREACH(dd, link->ndisc_rdnss, i) { | |
3258 | if (space) | |
3259 | fputc(' ', f); | |
3260 | ||
3261 | serialize_in6_addrs(f, &dd->address, 1); | |
3262 | space = true; | |
b0e39c82 TG |
3263 | } |
3264 | } | |
3265 | ||
2ce40956 | 3266 | fputc('\n', f); |
b0e39c82 | 3267 | |
2ce40956 | 3268 | fputs("NTP=", f); |
ea352b40 | 3269 | space = false; |
3df9bec5 | 3270 | fputstrv(f, link->network->ntp, NULL, &space); |
d5314fff | 3271 | |
27cb34f5 | 3272 | if (link->network->dhcp_use_ntp && |
b0e39c82 TG |
3273 | link->dhcp_lease) { |
3274 | const struct in_addr *addresses; | |
3275 | ||
3276 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
3277 | if (r > 0) { | |
ea352b40 LP |
3278 | if (space) |
3279 | fputc(' ', f); | |
b0e39c82 | 3280 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3281 | space = true; |
3282 | } | |
3283 | } | |
3284 | ||
27cb34f5 | 3285 | if (link->network->dhcp_use_ntp && dhcp6_lease) { |
07bdc70d PF |
3286 | struct in6_addr *in6_addrs; |
3287 | char **hosts; | |
07bdc70d PF |
3288 | |
3289 | r = sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease, | |
3290 | &in6_addrs); | |
3291 | if (r > 0) { | |
3292 | if (space) | |
3293 | fputc(' ', f); | |
3294 | serialize_in6_addrs(f, in6_addrs, r); | |
3295 | space = true; | |
3296 | } | |
3297 | ||
3298 | r = sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease, &hosts); | |
3df9bec5 LP |
3299 | if (r > 0) |
3300 | fputstrv(f, hosts, NULL, &space); | |
b0e39c82 TG |
3301 | } |
3302 | ||
2ce40956 | 3303 | fputc('\n', f); |
bd8f6538 | 3304 | |
b2a81c0b LP |
3305 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
3306 | if (link->dhcp_lease) | |
3307 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); | |
b2a81c0b LP |
3308 | if (dhcp6_lease) |
3309 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3310 | } |
3311 | ||
b2a81c0b LP |
3312 | fputs("DOMAINS=", f); |
3313 | fputstrv(f, link->network->search_domains, NULL, &space); | |
07bdc70d | 3314 | |
1e7a0e21 LP |
3315 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
3316 | NDiscDNSSL *dd; | |
9b4d1882 | 3317 | |
1e7a0e21 LP |
3318 | if (dhcp_domainname) |
3319 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
3320 | if (dhcp6_domains) | |
3321 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3322 | ||
3323 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3324 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3325 | } | |
b2a81c0b | 3326 | |
2ce40956 | 3327 | fputc('\n', f); |
6192b846 | 3328 | |
3df9bec5 LP |
3329 | fputs("ROUTE_DOMAINS=", f); |
3330 | fputstrv(f, link->network->route_domains, NULL, NULL); | |
b2a81c0b | 3331 | |
1e7a0e21 LP |
3332 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
3333 | NDiscDNSSL *dd; | |
b2a81c0b | 3334 | |
1e7a0e21 LP |
3335 | if (dhcp_domainname) |
3336 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
3337 | if (dhcp6_domains) | |
3338 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3339 | ||
3340 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3341 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3342 | } | |
b2a81c0b | 3343 | |
3df9bec5 | 3344 | fputc('\n', f); |
67272d15 | 3345 | |
3c9b8860 | 3346 | fprintf(f, "LLMNR=%s\n", |
a7e5da6e | 3347 | resolve_support_to_string(link->network->llmnr)); |
aaa297d4 LP |
3348 | fprintf(f, "MDNS=%s\n", |
3349 | resolve_support_to_string(link->network->mdns)); | |
e7780c8d | 3350 | |
ad6c0475 LP |
3351 | if (link->network->dnssec_mode != _DNSSEC_MODE_INVALID) |
3352 | fprintf(f, "DNSSEC=%s\n", | |
3353 | dnssec_mode_to_string(link->network->dnssec_mode)); | |
3354 | ||
8a516214 LP |
3355 | if (!set_isempty(link->network->dnssec_negative_trust_anchors)) { |
3356 | const char *n; | |
3357 | ||
3358 | fputs("DNSSEC_NTA=", f); | |
3359 | space = false; | |
d390f8ef LP |
3360 | SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) |
3361 | fputs_with_space(f, n, NULL, &space); | |
8a516214 LP |
3362 | fputc('\n', f); |
3363 | } | |
3364 | ||
2ce40956 | 3365 | fputs("ADDRESSES=", f); |
e7780c8d TG |
3366 | space = false; |
3367 | SET_FOREACH(a, link->addresses, i) { | |
3368 | _cleanup_free_ char *address_str = NULL; | |
3369 | ||
3370 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
3371 | if (r < 0) | |
3372 | goto fail; | |
3373 | ||
e7780c8d TG |
3374 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
3375 | space = true; | |
3376 | } | |
2ce40956 | 3377 | fputc('\n', f); |
c1eb9872 TG |
3378 | |
3379 | fputs("ROUTES=", f); | |
3380 | space = false; | |
3381 | SET_FOREACH(route, link->routes, i) { | |
3382 | _cleanup_free_ char *route_str = NULL; | |
3383 | ||
3384 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
3385 | if (r < 0) | |
3386 | goto fail; | |
3387 | ||
f833694d TG |
3388 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%hhu/"USEC_FMT, space ? " " : "", route_str, |
3389 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); | |
c1eb9872 TG |
3390 | space = true; |
3391 | } | |
3392 | ||
3393 | fputc('\n', f); | |
bcb7a07e | 3394 | } |
7374f9d8 | 3395 | |
b295beea LP |
3396 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
3397 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 3398 | |
8eb9058d | 3399 | if (link->dhcp_lease) { |
0bc70f1d | 3400 | struct in_addr address; |
8eb9058d LP |
3401 | const char *tz = NULL; |
3402 | ||
0bc70f1d TG |
3403 | assert(link->network); |
3404 | ||
8eb9058d LP |
3405 | r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
3406 | if (r >= 0) | |
3407 | fprintf(f, "TIMEZONE=%s\n", tz); | |
8eb9058d | 3408 | |
0bc70f1d TG |
3409 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
3410 | if (r >= 0) { | |
3411 | fputs("DHCP4_ADDRESS=", f); | |
3412 | serialize_in_addrs(f, &address, 1); | |
3413 | fputc('\n', f); | |
3414 | } | |
d9876a52 | 3415 | |
bd91b83e | 3416 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 3417 | if (r < 0) |
c2d6bd61 | 3418 | goto fail; |
fe8db0c5 | 3419 | |
7374f9d8 | 3420 | fprintf(f, |
b0e39c82 TG |
3421 | "DHCP_LEASE=%s\n", |
3422 | link->lease_file); | |
deb2e523 | 3423 | } else |
68a8723c | 3424 | unlink(link->lease_file); |
fe8db0c5 | 3425 | |
0bc70f1d TG |
3426 | if (link->ipv4ll) { |
3427 | struct in_addr address; | |
3428 | ||
3429 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
3430 | if (r >= 0) { | |
3431 | fputs("IPV4LL_ADDRESS=", f); | |
3432 | serialize_in_addrs(f, &address, 1); | |
3433 | fputc('\n', f); | |
3434 | } | |
3435 | } | |
3436 | ||
c2d6bd61 LP |
3437 | r = fflush_and_check(f); |
3438 | if (r < 0) | |
3439 | goto fail; | |
fe8db0c5 | 3440 | |
c2d6bd61 | 3441 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 3442 | r = -errno; |
c2d6bd61 | 3443 | goto fail; |
fe8db0c5 TG |
3444 | } |
3445 | ||
c2d6bd61 | 3446 | return 0; |
dacd6cee | 3447 | |
c2d6bd61 | 3448 | fail: |
6a7a4e4d | 3449 | (void) unlink(link->state_file); |
6a7a4e4d LP |
3450 | if (temp_path) |
3451 | (void) unlink(temp_path); | |
3452 | ||
dacd6cee | 3453 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
3454 | } |
3455 | ||
84de38c5 TG |
3456 | /* The serialized state in /run is no longer up-to-date. */ |
3457 | void link_dirty(Link *link) { | |
3458 | int r; | |
3459 | ||
3460 | assert(link); | |
3461 | ||
0c241a37 SS |
3462 | /* mark manager dirty as link is dirty */ |
3463 | manager_dirty(link->manager); | |
3464 | ||
84de38c5 TG |
3465 | r = set_ensure_allocated(&link->manager->dirty_links, NULL); |
3466 | if (r < 0) | |
3467 | /* allocation errors are ignored */ | |
3468 | return; | |
3469 | ||
3470 | r = set_put(link->manager->dirty_links, link); | |
0c241a37 SS |
3471 | if (r <= 0) |
3472 | /* don't take another ref if the link was already dirty */ | |
84de38c5 TG |
3473 | return; |
3474 | ||
3475 | link_ref(link); | |
3476 | } | |
3477 | ||
3478 | /* The serialized state in /run is up-to-date */ | |
3479 | void link_clean(Link *link) { | |
3480 | assert(link); | |
3481 | assert(link->manager); | |
3482 | ||
3483 | set_remove(link->manager->dirty_links, link); | |
3484 | link_unref(link); | |
3485 | } | |
3486 | ||
fe8db0c5 | 3487 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 3488 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
3489 | [LINK_STATE_ENSLAVING] = "configuring", |
3490 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
3491 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
3492 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 3493 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 3494 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 3495 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
3496 | }; |
3497 | ||
3498 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
3499 | |
3500 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
3501 | [LINK_OPERSTATE_OFF] = "off", |
3502 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
3503 | [LINK_OPERSTATE_DORMANT] = "dormant", |
3504 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
3505 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
3506 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
3507 | }; | |
3508 | ||
3509 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |