]>
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 | ||
e1853b00 | 1317 | static int link_set_bridge(Link *link) { |
4afd3348 | 1318 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
e1853b00 SS |
1319 | int r; |
1320 | ||
1321 | assert(link); | |
1322 | assert(link->network); | |
1323 | ||
6a7a4e4d LP |
1324 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1325 | if (r < 0) | |
1326 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
e1853b00 SS |
1327 | |
1328 | r = sd_rtnl_message_link_set_family(req, PF_BRIDGE); | |
6a7a4e4d LP |
1329 | if (r < 0) |
1330 | return log_link_error_errno(link, r, "Could not set message family: %m"); | |
e1853b00 | 1331 | |
1c4baffc | 1332 | r = sd_netlink_message_open_container(req, IFLA_PROTINFO); |
6a7a4e4d LP |
1333 | if (r < 0) |
1334 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
e1853b00 | 1335 | |
84c34096 | 1336 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_GUARD, !link->network->use_bpdu); |
eb7ff4dd SS |
1337 | if (r < 0) |
1338 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_GUARD attribute: %m"); | |
1339 | ||
1340 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_MODE, link->network->hairpin); | |
1341 | if (r < 0) | |
1342 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_MODE attribute: %m"); | |
1343 | ||
1344 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_FAST_LEAVE, link->network->fast_leave); | |
1345 | if (r < 0) | |
1346 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m"); | |
1347 | ||
23da66bb | 1348 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_PROTECT, !link->network->allow_port_to_be_root); |
eb7ff4dd SS |
1349 | if (r < 0) |
1350 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PROTECT attribute: %m"); | |
1351 | ||
1352 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_UNICAST_FLOOD, link->network->unicast_flood); | |
1353 | if (r < 0) | |
1354 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m"); | |
1355 | ||
34437b4f | 1356 | if (link->network->cost != 0) { |
1c4baffc | 1357 | r = sd_netlink_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost); |
6a7a4e4d LP |
1358 | if (r < 0) |
1359 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_COST attribute: %m"); | |
e1853b00 SS |
1360 | } |
1361 | ||
1c4baffc | 1362 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1363 | if (r < 0) |
1364 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
e1853b00 | 1365 | |
1c4baffc | 1366 | r = sd_netlink_call_async(link->manager->rtnl, req, link_set_handler, link, 0, NULL); |
6a7a4e4d LP |
1367 | if (r < 0) |
1368 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
e1853b00 SS |
1369 | |
1370 | link_ref(link); | |
1371 | ||
1372 | return r; | |
1373 | } | |
1374 | ||
34437b4f LP |
1375 | static int link_lldp_save(Link *link) { |
1376 | _cleanup_free_ char *temp_path = NULL; | |
1377 | _cleanup_fclose_ FILE *f = NULL; | |
1378 | sd_lldp_neighbor **l = NULL; | |
1379 | int n = 0, r, i; | |
49699bac SS |
1380 | |
1381 | assert(link); | |
34437b4f LP |
1382 | assert(link->lldp_file); |
1383 | ||
1384 | if (!link->lldp) { | |
1385 | (void) unlink(link->lldp_file); | |
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | r = sd_lldp_get_neighbors(link->lldp, &l); | |
1390 | if (r < 0) | |
1391 | goto finish; | |
1392 | if (r == 0) { | |
1393 | (void) unlink(link->lldp_file); | |
1394 | goto finish; | |
1395 | } | |
1396 | ||
1397 | n = r; | |
1398 | ||
1399 | r = fopen_temporary(link->lldp_file, &f, &temp_path); | |
1400 | if (r < 0) | |
1401 | goto finish; | |
1402 | ||
1403 | fchmod(fileno(f), 0644); | |
49699bac | 1404 | |
34437b4f LP |
1405 | for (i = 0; i < n; i++) { |
1406 | const void *p; | |
1407 | le64_t u; | |
1408 | size_t sz; | |
1409 | ||
1410 | r = sd_lldp_neighbor_get_raw(l[i], &p, &sz); | |
9ef61f2e | 1411 | if (r < 0) |
34437b4f LP |
1412 | goto finish; |
1413 | ||
1414 | u = htole64(sz); | |
1415 | (void) fwrite(&u, 1, sizeof(u), f); | |
1416 | (void) fwrite(p, 1, sz, f); | |
1417 | } | |
49699bac | 1418 | |
34437b4f LP |
1419 | r = fflush_and_check(f); |
1420 | if (r < 0) | |
1421 | goto finish; | |
1422 | ||
1423 | if (rename(temp_path, link->lldp_file) < 0) { | |
1424 | r = -errno; | |
1425 | goto finish; | |
9ef61f2e | 1426 | } |
34437b4f LP |
1427 | |
1428 | finish: | |
1429 | if (r < 0) { | |
1430 | (void) unlink(link->lldp_file); | |
1431 | if (temp_path) | |
1432 | (void) unlink(temp_path); | |
1433 | ||
1434 | log_link_error_errno(link, r, "Failed to save LLDP data to %s: %m", link->lldp_file); | |
1435 | } | |
1436 | ||
1437 | if (l) { | |
1438 | for (i = 0; i < n; i++) | |
1439 | sd_lldp_neighbor_unref(l[i]); | |
1440 | free(l); | |
1441 | } | |
1442 | ||
1443 | return r; | |
1444 | } | |
1445 | ||
90dffb22 | 1446 | static void lldp_handler(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata) { |
34437b4f | 1447 | Link *link = userdata; |
90dffb22 | 1448 | int r; |
34437b4f LP |
1449 | |
1450 | assert(link); | |
1451 | ||
1452 | (void) link_lldp_save(link); | |
90dffb22 | 1453 | |
7272b25e | 1454 | if (link_lldp_emit_enabled(link) && event == SD_LLDP_EVENT_ADDED) { |
90dffb22 LP |
1455 | /* If we received information about a new neighbor, restart the LLDP "fast" logic */ |
1456 | ||
1457 | log_link_debug(link, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission."); | |
1458 | ||
7272b25e | 1459 | r = link_lldp_emit_start(link); |
90dffb22 LP |
1460 | if (r < 0) |
1461 | log_link_warning_errno(link, r, "Failed to restart LLDP transmission: %m"); | |
1462 | } | |
49699bac SS |
1463 | } |
1464 | ||
e7ab854c TG |
1465 | static int link_acquire_ipv6_conf(Link *link) { |
1466 | int r; | |
1467 | ||
1468 | assert(link); | |
1469 | ||
1470 | if (link_dhcp6_enabled(link)) { | |
1471 | assert(link->dhcp6_client); | |
c601ebf7 | 1472 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); |
e7ab854c | 1473 | |
720bec40 TY |
1474 | /* start DHCPv6 client in stateless mode */ |
1475 | r = dhcp6_request_address(link, true); | |
63348d13 | 1476 | if (r < 0 && r != -EBUSY) |
e7ab854c | 1477 | return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease: %m"); |
720bec40 TY |
1478 | else |
1479 | log_link_debug(link, "Acquiring DHCPv6 lease"); | |
e7ab854c TG |
1480 | } |
1481 | ||
1482 | if (link_ipv6_accept_ra_enabled(link)) { | |
1e7a0e21 | 1483 | assert(link->ndisc); |
e7ab854c TG |
1484 | |
1485 | log_link_debug(link, "Discovering IPv6 routers"); | |
1486 | ||
1e7a0e21 | 1487 | r = sd_ndisc_start(link->ndisc); |
63348d13 | 1488 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1489 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m"); |
1490 | } | |
1491 | ||
1492 | return 0; | |
1493 | } | |
1494 | ||
6fc25497 | 1495 | static int link_acquire_ipv4_conf(Link *link) { |
ff254138 TG |
1496 | int r; |
1497 | ||
1498 | assert(link); | |
1499 | assert(link->network); | |
ff254138 TG |
1500 | assert(link->manager); |
1501 | assert(link->manager->event); | |
1502 | ||
78c958f8 | 1503 | if (link_ipv4ll_enabled(link)) { |
eb34d4af | 1504 | assert(link->ipv4ll); |
ff254138 | 1505 | |
6a7a4e4d | 1506 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
1507 | |
1508 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
1509 | if (r < 0) |
1510 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
1511 | } |
1512 | ||
78c958f8 | 1513 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 1514 | assert(link->dhcp_client); |
ff254138 | 1515 | |
6a7a4e4d | 1516 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 1517 | |
5c1d3fc9 | 1518 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
1519 | if (r < 0) |
1520 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 1521 | } |
ff254138 | 1522 | |
6fc25497 SS |
1523 | return 0; |
1524 | } | |
1525 | ||
1526 | static int link_acquire_conf(Link *link) { | |
1527 | int r; | |
1528 | ||
1529 | assert(link); | |
1530 | ||
1531 | r = link_acquire_ipv4_conf(link); | |
1532 | if (r < 0) | |
1533 | return r; | |
1534 | ||
1535 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) == 0) { | |
1536 | r = link_acquire_ipv6_conf(link); | |
1537 | if (r < 0) | |
1538 | return r; | |
1539 | } | |
1540 | ||
7272b25e LP |
1541 | if (link_lldp_emit_enabled(link)) { |
1542 | r = link_lldp_emit_start(link); | |
8e1ad1ea LP |
1543 | if (r < 0) |
1544 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
1545 | } | |
1546 | ||
ff254138 TG |
1547 | return 0; |
1548 | } | |
1549 | ||
a61bb41c | 1550 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
1551 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1552 | ||
a61bb41c | 1553 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
1554 | return true; |
1555 | ||
a61bb41c | 1556 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 1557 | /* operstate may not be implemented, so fall back to flags */ |
a61bb41c | 1558 | if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) |
deb2e523 TG |
1559 | return true; |
1560 | ||
1561 | return false; | |
1562 | } | |
1563 | ||
1c4baffc | 1564 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 1565 | _cleanup_link_unref_ Link *link = userdata; |
dd3efc09 TG |
1566 | int r; |
1567 | ||
1746cf2a TG |
1568 | assert(link); |
1569 | ||
5da8149f | 1570 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1571 | return 1; |
1572 | ||
1c4baffc | 1573 | r = sd_netlink_message_get_errno(m); |
6a7a4e4d LP |
1574 | if (r < 0) |
1575 | /* we warn but don't fail the link, as it may be | |
1576 | brought up later */ | |
a2fae7bb | 1577 | log_link_warning_errno(link, r, "Could not bring up interface: %m"); |
45ad2c13 | 1578 | |
f882c247 TG |
1579 | return 1; |
1580 | } | |
1581 | ||
1582 | static int link_up(Link *link) { | |
4afd3348 | 1583 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
5c3072ea | 1584 | uint8_t ipv6ll_mode; |
f579559b TG |
1585 | int r; |
1586 | ||
f882c247 | 1587 | assert(link); |
c106cc36 | 1588 | assert(link->network); |
f882c247 TG |
1589 | assert(link->manager); |
1590 | assert(link->manager->rtnl); | |
1591 | ||
6a7a4e4d | 1592 | log_link_debug(link, "Bringing link up"); |
449f7554 | 1593 | |
6a7a4e4d LP |
1594 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1595 | if (r < 0) | |
1596 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 1597 | |
2b2d8603 | 1598 | /* set it free if not enslaved with networkd */ |
6cb955c6 | 1599 | if (!link->network->bridge && !link->network->bond && !link->network->vrf) { |
2b2d8603 TY |
1600 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0); |
1601 | if (r < 0) | |
1602 | return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m"); | |
1603 | } | |
1604 | ||
5d4795f3 | 1605 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
1606 | if (r < 0) |
1607 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 1608 | |
c106cc36 | 1609 | if (link->network->mac) { |
1c4baffc | 1610 | r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); |
6a7a4e4d LP |
1611 | if (r < 0) |
1612 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
1613 | } |
1614 | ||
4cef7fe3 | 1615 | /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled) |
2b00a4e0 | 1616 | for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */ |
439689c6 SS |
1617 | (void) link_enable_ipv6(link); |
1618 | ||
c106cc36 | 1619 | if (link->network->mtu) { |
439689c6 SS |
1620 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes |
1621 | on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */ | |
1622 | if (link_ipv6_enabled(link) && link->network->mtu < IPV6_MIN_MTU) { | |
1623 | ||
1624 | log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as " | |
1625 | "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes: %m"); | |
1626 | ||
1627 | link->network->mtu = IPV6_MIN_MTU; | |
1628 | } | |
1629 | ||
1c4baffc | 1630 | r = sd_netlink_message_append_u32(req, IFLA_MTU, link->network->mtu); |
6a7a4e4d LP |
1631 | if (r < 0) |
1632 | return log_link_error_errno(link, r, "Could not set MTU: %m"); | |
c106cc36 TG |
1633 | } |
1634 | ||
1c4baffc | 1635 | r = sd_netlink_message_open_container(req, IFLA_AF_SPEC); |
6a7a4e4d LP |
1636 | if (r < 0) |
1637 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1638 | |
439689c6 | 1639 | if (link_ipv6_enabled(link)) { |
01d28f81 | 1640 | /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */ |
1c4baffc | 1641 | r = sd_netlink_message_open_container(req, AF_INET6); |
01d28f81 TG |
1642 | if (r < 0) |
1643 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
d0d6a4cd | 1644 | |
fbc38f23 TY |
1645 | if (!link_ipv6ll_enabled(link)) |
1646 | ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE; | |
1647 | else { | |
1648 | const char *p = NULL; | |
1649 | _cleanup_free_ char *stable_secret = NULL; | |
1650 | ||
1651 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/stable_secret"); | |
1652 | r = read_one_line_file(p, &stable_secret); | |
1653 | ||
1654 | if (r < 0) | |
1655 | ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64; | |
1656 | else | |
1657 | ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
1658 | } | |
1c4baffc | 1659 | r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); |
01d28f81 TG |
1660 | if (r < 0) |
1661 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
d0d6a4cd | 1662 | |
01d28f81 | 1663 | if (!in_addr_is_null(AF_INET6, &link->network->ipv6_token)) { |
1c4baffc | 1664 | r = sd_netlink_message_append_in6_addr(req, IFLA_INET6_TOKEN, &link->network->ipv6_token.in6); |
01d28f81 TG |
1665 | if (r < 0) |
1666 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_TOKEN: %m"); | |
1667 | } | |
1668 | ||
1c4baffc | 1669 | r = sd_netlink_message_close_container(req); |
6a7a4e4d | 1670 | if (r < 0) |
01d28f81 | 1671 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); |
7f77697a | 1672 | } |
d0d6a4cd | 1673 | |
1c4baffc | 1674 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1675 | if (r < 0) |
1676 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1677 | |
1c4baffc | 1678 | r = sd_netlink_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL); |
6a7a4e4d LP |
1679 | if (r < 0) |
1680 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1681 | |
b226d99b TG |
1682 | link_ref(link); |
1683 | ||
f882c247 TG |
1684 | return 0; |
1685 | } | |
1686 | ||
1c4baffc | 1687 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
0d4ad91d AR |
1688 | _cleanup_link_unref_ Link *link = userdata; |
1689 | int r; | |
1690 | ||
1691 | assert(link); | |
1692 | ||
1693 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1694 | return 1; | |
1695 | ||
1c4baffc | 1696 | r = sd_netlink_message_get_errno(m); |
0d4ad91d | 1697 | if (r < 0) |
a2fae7bb | 1698 | log_link_warning_errno(link, r, "Could not bring down interface: %m"); |
0d4ad91d AR |
1699 | |
1700 | return 1; | |
1701 | } | |
1702 | ||
1703 | static int link_down(Link *link) { | |
4afd3348 | 1704 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
0d4ad91d AR |
1705 | int r; |
1706 | ||
1707 | assert(link); | |
1708 | assert(link->manager); | |
1709 | assert(link->manager->rtnl); | |
1710 | ||
6a7a4e4d | 1711 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
1712 | |
1713 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
1714 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
1715 | if (r < 0) |
1716 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
1717 | |
1718 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
1719 | if (r < 0) |
1720 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 1721 | |
1c4baffc | 1722 | r = sd_netlink_call_async(link->manager->rtnl, req, link_down_handler, link, 0, NULL); |
6a7a4e4d LP |
1723 | if (r < 0) |
1724 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
1725 | |
1726 | link_ref(link); | |
1727 | ||
1728 | return 0; | |
1729 | } | |
1730 | ||
1731 | static int link_handle_bound_to_list(Link *link) { | |
1732 | Link *l; | |
1733 | Iterator i; | |
1734 | int r; | |
1735 | bool required_up = false; | |
1736 | bool link_is_up = false; | |
1737 | ||
1738 | assert(link); | |
1739 | ||
1740 | if (hashmap_isempty(link->bound_to_links)) | |
1741 | return 0; | |
1742 | ||
1743 | if (link->flags & IFF_UP) | |
1744 | link_is_up = true; | |
1745 | ||
1746 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
1747 | if (link_has_carrier(l)) { | |
1748 | required_up = true; | |
1749 | break; | |
1750 | } | |
1751 | ||
1752 | if (!required_up && link_is_up) { | |
1753 | r = link_down(link); | |
1754 | if (r < 0) | |
1755 | return r; | |
1756 | } else if (required_up && !link_is_up) { | |
1757 | r = link_up(link); | |
1758 | if (r < 0) | |
1759 | return r; | |
1760 | } | |
1761 | ||
1762 | return 0; | |
1763 | } | |
1764 | ||
1765 | static int link_handle_bound_by_list(Link *link) { | |
1766 | Iterator i; | |
1767 | Link *l; | |
1768 | int r; | |
1769 | ||
1770 | assert(link); | |
1771 | ||
1772 | if (hashmap_isempty(link->bound_by_links)) | |
1773 | return 0; | |
1774 | ||
1775 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
1776 | r = link_handle_bound_to_list(l); | |
1777 | if (r < 0) | |
1778 | return r; | |
1779 | } | |
1780 | ||
1781 | return 0; | |
1782 | } | |
1783 | ||
1784 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
1785 | int r; | |
1786 | ||
1787 | assert(link); | |
1788 | assert(carrier); | |
1789 | ||
1790 | if (link == carrier) | |
1791 | return 0; | |
1792 | ||
1793 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
1794 | return 0; | |
1795 | ||
1796 | r = hashmap_ensure_allocated(h, NULL); | |
1797 | if (r < 0) | |
1798 | return r; | |
1799 | ||
1800 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
1801 | if (r < 0) | |
1802 | return r; | |
1803 | ||
1804 | return 0; | |
1805 | } | |
1806 | ||
1807 | static int link_new_bound_by_list(Link *link) { | |
1808 | Manager *m; | |
1809 | Link *carrier; | |
1810 | Iterator i; | |
1811 | int r; | |
1812 | bool list_updated = false; | |
1813 | ||
1814 | assert(link); | |
1815 | assert(link->manager); | |
1816 | ||
1817 | m = link->manager; | |
1818 | ||
b295beea | 1819 | HASHMAP_FOREACH(carrier, m->links, i) { |
0d4ad91d AR |
1820 | if (!carrier->network) |
1821 | continue; | |
1822 | ||
1823 | if (strv_isempty(carrier->network->bind_carrier)) | |
1824 | continue; | |
1825 | ||
1826 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
1827 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
1828 | if (r < 0) | |
1829 | return r; | |
1830 | ||
1831 | list_updated = true; | |
1832 | } | |
1833 | } | |
1834 | ||
1835 | if (list_updated) | |
84de38c5 | 1836 | link_dirty(link); |
0d4ad91d | 1837 | |
b295beea | 1838 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
0d4ad91d AR |
1839 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
1840 | if (r < 0) | |
1841 | return r; | |
1842 | ||
84de38c5 | 1843 | link_dirty(carrier); |
0d4ad91d AR |
1844 | } |
1845 | ||
1846 | return 0; | |
1847 | } | |
1848 | ||
1849 | static int link_new_bound_to_list(Link *link) { | |
1850 | Manager *m; | |
1851 | Link *carrier; | |
1852 | Iterator i; | |
1853 | int r; | |
1854 | bool list_updated = false; | |
1855 | ||
1856 | assert(link); | |
1857 | assert(link->manager); | |
1858 | ||
1859 | if (!link->network) | |
1860 | return 0; | |
1861 | ||
1862 | if (strv_isempty(link->network->bind_carrier)) | |
1863 | return 0; | |
1864 | ||
1865 | m = link->manager; | |
1866 | ||
1867 | HASHMAP_FOREACH (carrier, m->links, i) { | |
1868 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
1869 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
1870 | if (r < 0) | |
1871 | return r; | |
1872 | ||
1873 | list_updated = true; | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | if (list_updated) | |
84de38c5 | 1878 | link_dirty(link); |
0d4ad91d AR |
1879 | |
1880 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
1881 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
1882 | if (r < 0) | |
1883 | return r; | |
1884 | ||
84de38c5 | 1885 | link_dirty(carrier); |
0d4ad91d AR |
1886 | } |
1887 | ||
1888 | return 0; | |
1889 | } | |
1890 | ||
1891 | static int link_new_carrier_maps(Link *link) { | |
1892 | int r; | |
1893 | ||
1894 | r = link_new_bound_by_list(link); | |
1895 | if (r < 0) | |
1896 | return r; | |
1897 | ||
1898 | r = link_handle_bound_by_list(link); | |
1899 | if (r < 0) | |
1900 | return r; | |
1901 | ||
1902 | r = link_new_bound_to_list(link); | |
1903 | if (r < 0) | |
1904 | return r; | |
1905 | ||
1906 | r = link_handle_bound_to_list(link); | |
1907 | if (r < 0) | |
1908 | return r; | |
1909 | ||
1910 | return 0; | |
1911 | } | |
1912 | ||
1913 | static void link_free_bound_to_list(Link *link) { | |
1914 | Link *bound_to; | |
1915 | Iterator i; | |
1916 | ||
1917 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
1918 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
1919 | ||
1920 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 1921 | link_dirty(bound_to); |
0d4ad91d AR |
1922 | } |
1923 | ||
1924 | return; | |
1925 | } | |
1926 | ||
1927 | static void link_free_bound_by_list(Link *link) { | |
1928 | Link *bound_by; | |
1929 | Iterator i; | |
1930 | ||
1931 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
1932 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
1933 | ||
1934 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 1935 | link_dirty(bound_by); |
0d4ad91d AR |
1936 | link_handle_bound_to_list(bound_by); |
1937 | } | |
1938 | } | |
1939 | ||
1940 | return; | |
1941 | } | |
1942 | ||
1943 | static void link_free_carrier_maps(Link *link) { | |
1944 | bool list_updated = false; | |
1945 | ||
1946 | assert(link); | |
1947 | ||
1948 | if (!hashmap_isempty(link->bound_to_links)) { | |
1949 | link_free_bound_to_list(link); | |
1950 | list_updated = true; | |
1951 | } | |
1952 | ||
1953 | if (!hashmap_isempty(link->bound_by_links)) { | |
1954 | link_free_bound_by_list(link); | |
1955 | list_updated = true; | |
1956 | } | |
1957 | ||
1958 | if (list_updated) | |
84de38c5 | 1959 | link_dirty(link); |
0d4ad91d AR |
1960 | |
1961 | return; | |
1962 | } | |
1963 | ||
1964 | void link_drop(Link *link) { | |
1965 | if (!link || link->state == LINK_STATE_LINGER) | |
1966 | return; | |
1967 | ||
1968 | link_set_state(link, LINK_STATE_LINGER); | |
1969 | ||
1970 | link_free_carrier_maps(link); | |
1971 | ||
6a7a4e4d | 1972 | log_link_debug(link, "Link removed"); |
0d4ad91d | 1973 | |
84de38c5 | 1974 | (void)unlink(link->state_file); |
0d4ad91d AR |
1975 | link_unref(link); |
1976 | ||
1977 | return; | |
1978 | } | |
1979 | ||
3f265037 | 1980 | static int link_joined(Link *link) { |
f882c247 TG |
1981 | int r; |
1982 | ||
ef1ba606 | 1983 | assert(link); |
f5be5601 | 1984 | assert(link->network); |
dd3efc09 | 1985 | |
0d4ad91d AR |
1986 | if (!hashmap_isempty(link->bound_to_links)) { |
1987 | r = link_handle_bound_to_list(link); | |
1988 | if (r < 0) | |
1989 | return r; | |
1990 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
1991 | r = link_up(link); |
1992 | if (r < 0) { | |
1993 | link_enter_failed(link); | |
1994 | return r; | |
1995 | } | |
ef1ba606 | 1996 | } |
f882c247 | 1997 | |
9ed794a3 | 1998 | if (link->network->bridge) { |
e1853b00 | 1999 | r = link_set_bridge(link); |
6a7a4e4d LP |
2000 | if (r < 0) |
2001 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
2002 | } |
2003 | ||
357bb46a | 2004 | if (link->network->bridge || streq_ptr("bridge", link->kind)) { |
13b498f9 TJ |
2005 | r = link_set_bridge_vlan(link); |
2006 | if (r < 0) | |
2007 | log_link_error_errno(link, r, "Could not set bridge vlan: %m"); | |
2008 | } | |
2009 | ||
fb6730c4 | 2010 | return link_enter_set_addresses(link); |
02b59d57 TG |
2011 | } |
2012 | ||
62e2d5bb | 2013 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 2014 | _cleanup_link_unref_ Link *link = userdata; |
02b59d57 TG |
2015 | int r; |
2016 | ||
1746cf2a | 2017 | assert(link); |
ef1ba606 | 2018 | assert(link->network); |
02b59d57 | 2019 | |
313cefa1 | 2020 | link->enslaving--; |
52433f6b | 2021 | |
5da8149f | 2022 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
2023 | return 1; |
2024 | ||
1c4baffc | 2025 | r = sd_netlink_message_get_errno(m); |
856f962c | 2026 | if (r < 0 && r != -EEXIST) { |
a2fae7bb | 2027 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
ef1ba606 TG |
2028 | link_enter_failed(link); |
2029 | return 1; | |
ba179154 | 2030 | } else |
6a7a4e4d | 2031 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 2032 | |
856f962c | 2033 | if (link->enslaving <= 0) |
3f265037 | 2034 | link_joined(link); |
02b59d57 TG |
2035 | |
2036 | return 1; | |
2037 | } | |
2038 | ||
3f265037 | 2039 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 2040 | NetDev *netdev; |
672682a6 | 2041 | Iterator i; |
02b59d57 TG |
2042 | int r; |
2043 | ||
2044 | assert(link); | |
2045 | assert(link->network); | |
8434fd5c | 2046 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 2047 | |
e331e246 | 2048 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 2049 | |
84de38c5 | 2050 | link_dirty(link); |
fe8db0c5 | 2051 | |
7951dea2 SS |
2052 | if (!link->network->bridge && |
2053 | !link->network->bond && | |
6cb955c6 | 2054 | !link->network->vrf && |
6a0a2f86 | 2055 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 2056 | return link_joined(link); |
02b59d57 | 2057 | |
d9c67ea1 | 2058 | if (link->network->bond) { |
f2341e0a LP |
2059 | log_struct(LOG_DEBUG, |
2060 | LOG_LINK_INTERFACE(link), | |
2061 | LOG_NETDEV_INTERFACE(link->network->bond), | |
2062 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname), | |
2063 | NULL); | |
2064 | ||
2065 | r = netdev_join(link->network->bond, link, netdev_join_handler); | |
52433f6b | 2066 | if (r < 0) { |
f2341e0a LP |
2067 | log_struct_errno(LOG_WARNING, r, |
2068 | LOG_LINK_INTERFACE(link), | |
2069 | LOG_NETDEV_INTERFACE(link->network->bond), | |
2070 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname), | |
2071 | NULL); | |
2072 | ||
52433f6b TG |
2073 | link_enter_failed(link); |
2074 | return r; | |
2075 | } | |
2076 | ||
313cefa1 | 2077 | link->enslaving++; |
0ad6148e MO |
2078 | } |
2079 | ||
d9c67ea1 | 2080 | if (link->network->bridge) { |
f2341e0a LP |
2081 | log_struct(LOG_DEBUG, |
2082 | LOG_LINK_INTERFACE(link), | |
2083 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
2084 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname), | |
2085 | NULL); | |
2086 | ||
2087 | r = netdev_join(link->network->bridge, link, netdev_join_handler); | |
0ad6148e | 2088 | if (r < 0) { |
f2341e0a LP |
2089 | log_struct_errno(LOG_WARNING, r, |
2090 | LOG_LINK_INTERFACE(link), | |
2091 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
2092 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname), | |
2093 | NULL), | |
0ad6148e MO |
2094 | link_enter_failed(link); |
2095 | return r; | |
2096 | } | |
2097 | ||
313cefa1 | 2098 | link->enslaving++; |
52433f6b TG |
2099 | } |
2100 | ||
6cb955c6 AR |
2101 | if (link->network->vrf) { |
2102 | log_struct(LOG_DEBUG, | |
2103 | LOG_LINK_INTERFACE(link), | |
2104 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
2105 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname), | |
2106 | NULL); | |
2107 | r = netdev_join(link->network->vrf, link, netdev_join_handler); | |
2108 | if (r < 0) { | |
2109 | log_struct_errno(LOG_WARNING, r, | |
2110 | LOG_LINK_INTERFACE(link), | |
2111 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
2112 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname), | |
2113 | NULL); | |
2114 | link_enter_failed(link); | |
2115 | return r; | |
2116 | } | |
2117 | ||
2118 | link->enslaving++; | |
2119 | } | |
2120 | ||
6a0a2f86 | 2121 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 2122 | |
f2341e0a LP |
2123 | log_struct(LOG_DEBUG, |
2124 | LOG_LINK_INTERFACE(link), | |
2125 | LOG_NETDEV_INTERFACE(netdev), | |
2126 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname), | |
2127 | NULL); | |
2128 | ||
2129 | r = netdev_join(netdev, link, netdev_join_handler); | |
7951dea2 | 2130 | if (r < 0) { |
f2341e0a LP |
2131 | log_struct_errno(LOG_WARNING, r, |
2132 | LOG_LINK_INTERFACE(link), | |
2133 | LOG_NETDEV_INTERFACE(netdev), | |
2134 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname), | |
2135 | NULL); | |
326cb406 SS |
2136 | link_enter_failed(link); |
2137 | return r; | |
2138 | } | |
2139 | ||
313cefa1 | 2140 | link->enslaving++; |
326cb406 SS |
2141 | } |
2142 | ||
ef1ba606 TG |
2143 | return 0; |
2144 | } | |
2145 | ||
769d324c | 2146 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
2147 | int r; |
2148 | ||
765afd5c | 2149 | if (!link_ipv4_forward_enabled(link)) |
15dee3f0 LP |
2150 | return 0; |
2151 | ||
765afd5c LP |
2152 | /* We propagate the forwarding flag from one interface to the |
2153 | * global setting one way. This means: as long as at least one | |
2154 | * interface was configured at any time that had IP forwarding | |
2155 | * enabled the setting will stay on for good. We do this | |
2156 | * primarily to keep IPv4 and IPv6 packet forwarding behaviour | |
2157 | * somewhat in sync (see below). */ | |
15dee3f0 | 2158 | |
765afd5c | 2159 | r = write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2160 | if (r < 0) |
765afd5c | 2161 | log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m"); |
43c6d5ab | 2162 | |
769d324c LP |
2163 | return 0; |
2164 | } | |
2165 | ||
2166 | static int link_set_ipv6_forward(Link *link) { | |
769d324c LP |
2167 | int r; |
2168 | ||
765afd5c | 2169 | if (!link_ipv6_forward_enabled(link)) |
8add5f79 NO |
2170 | return 0; |
2171 | ||
61233823 | 2172 | /* On Linux, the IPv6 stack does not know a per-interface |
765afd5c LP |
2173 | * packet forwarding setting: either packet forwarding is on |
2174 | * for all, or off for all. We hence don't bother with a | |
2175 | * per-interface setting, but simply propagate the interface | |
2176 | * flag, if it is set, to the global flag, one-way. Note that | |
2177 | * while IPv4 would allow a per-interface flag, we expose the | |
2178 | * same behaviour there and also propagate the setting from | |
2179 | * one to all, to keep things simple (see above). */ | |
15dee3f0 | 2180 | |
765afd5c | 2181 | r = write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2182 | if (r < 0) |
765afd5c | 2183 | log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m"); |
5a8bcb67 LP |
2184 | |
2185 | return 0; | |
2186 | } | |
2187 | ||
49092e22 | 2188 | static int link_set_ipv6_privacy_extensions(Link *link) { |
1f0d9695 LP |
2189 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; |
2190 | IPv6PrivacyExtensions s; | |
49092e22 SS |
2191 | const char *p = NULL; |
2192 | int r; | |
2193 | ||
1f0d9695 | 2194 | s = link_ipv6_privacy_extensions(link); |
66a6bd68 | 2195 | if (s < 0) |
49092e22 SS |
2196 | return 0; |
2197 | ||
2198 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/use_tempaddr"); | |
66a6bd68 | 2199 | xsprintf(buf, "%u", (unsigned) link->network->ipv6_privacy_extensions); |
49092e22 | 2200 | |
eb3da901 LP |
2201 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2202 | if (r < 0) | |
49092e22 SS |
2203 | log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m"); |
2204 | ||
2205 | return 0; | |
2206 | } | |
2207 | ||
4f2e437a | 2208 | static int link_set_ipv6_accept_ra(Link *link) { |
3b015d40 | 2209 | const char *p = NULL; |
4f2e437a SS |
2210 | int r; |
2211 | ||
2212 | /* Make this a NOP if IPv6 is not available */ | |
2213 | if (!socket_ipv6_is_supported()) | |
2214 | return 0; | |
2215 | ||
2216 | if (link->flags & IFF_LOOPBACK) | |
2217 | return 0; | |
2218 | ||
d68e2e59 LP |
2219 | if (!link->network) |
2220 | return 0; | |
2221 | ||
4f2e437a | 2222 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/accept_ra"); |
4f2e437a | 2223 | |
0053598f | 2224 | /* We handle router advertisements ourselves, tell the kernel to GTFO */ |
fe307276 | 2225 | r = write_string_file(p, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2226 | if (r < 0) |
fe307276 | 2227 | log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m"); |
4f2e437a SS |
2228 | |
2229 | return 0; | |
2230 | } | |
2231 | ||
8749cbcd | 2232 | static int link_set_ipv6_dad_transmits(Link *link) { |
66a6bd68 | 2233 | char buf[DECIMAL_STR_MAX(int) + 1]; |
8749cbcd SS |
2234 | const char *p = NULL; |
2235 | int r; | |
2236 | ||
2237 | /* Make this a NOP if IPv6 is not available */ | |
2238 | if (!socket_ipv6_is_supported()) | |
2239 | return 0; | |
2240 | ||
2241 | if (link->flags & IFF_LOOPBACK) | |
2242 | return 0; | |
2243 | ||
d68e2e59 LP |
2244 | if (!link->network) |
2245 | return 0; | |
2246 | ||
8749cbcd SS |
2247 | if (link->network->ipv6_dad_transmits < 0) |
2248 | return 0; | |
2249 | ||
2250 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/dad_transmits"); | |
66a6bd68 | 2251 | xsprintf(buf, "%i", link->network->ipv6_dad_transmits); |
8749cbcd | 2252 | |
eb3da901 LP |
2253 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2254 | if (r < 0) | |
8749cbcd | 2255 | log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m"); |
8749cbcd SS |
2256 | |
2257 | return 0; | |
2258 | } | |
2259 | ||
b69c3180 | 2260 | static int link_set_ipv6_hop_limit(Link *link) { |
66a6bd68 | 2261 | char buf[DECIMAL_STR_MAX(int) + 1]; |
b69c3180 SS |
2262 | const char *p = NULL; |
2263 | int r; | |
2264 | ||
2265 | /* Make this a NOP if IPv6 is not available */ | |
2266 | if (!socket_ipv6_is_supported()) | |
2267 | return 0; | |
2268 | ||
2269 | if (link->flags & IFF_LOOPBACK) | |
2270 | return 0; | |
2271 | ||
d68e2e59 LP |
2272 | if (!link->network) |
2273 | return 0; | |
2274 | ||
b69c3180 SS |
2275 | if (link->network->ipv6_hop_limit < 0) |
2276 | return 0; | |
2277 | ||
2278 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/hop_limit"); | |
66a6bd68 | 2279 | xsprintf(buf, "%i", link->network->ipv6_hop_limit); |
b69c3180 | 2280 | |
eb3da901 LP |
2281 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2282 | if (r < 0) | |
b69c3180 | 2283 | log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m"); |
b69c3180 SS |
2284 | |
2285 | return 0; | |
2286 | } | |
2287 | ||
5e5b137a TG |
2288 | static int link_drop_foreign_config(Link *link) { |
2289 | Address *address; | |
2290 | Route *route; | |
2291 | Iterator i; | |
2292 | int r; | |
2293 | ||
2294 | SET_FOREACH(address, link->addresses_foreign, i) { | |
fe307276 | 2295 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5e5b137a TG |
2296 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) |
2297 | continue; | |
2298 | ||
2299 | r = address_remove(address, link, link_address_remove_handler); | |
2300 | if (r < 0) | |
2301 | return r; | |
2302 | } | |
2303 | ||
2304 | SET_FOREACH(route, link->routes_foreign, i) { | |
fe307276 | 2305 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2306 | if (route->protocol == RTPROT_KERNEL) |
2307 | continue; | |
2308 | ||
3fb1ac5d | 2309 | r = route_remove(route, link, link_route_remove_handler); |
5e5b137a TG |
2310 | if (r < 0) |
2311 | return r; | |
2312 | } | |
2313 | ||
2314 | return 0; | |
2315 | } | |
2316 | ||
273eec24 LP |
2317 | static int link_update_lldp(Link *link) { |
2318 | int r; | |
2319 | ||
2320 | assert(link); | |
2321 | ||
2322 | if (!link->lldp) | |
2323 | return 0; | |
2324 | ||
2325 | if (link->flags & IFF_UP) { | |
2326 | r = sd_lldp_start(link->lldp); | |
2327 | if (r > 0) | |
2328 | log_link_debug(link, "Started LLDP."); | |
2329 | } else { | |
2330 | r = sd_lldp_stop(link->lldp); | |
2331 | if (r > 0) | |
2332 | log_link_debug(link, "Stopped LLDP."); | |
2333 | } | |
2334 | ||
2335 | return r; | |
2336 | } | |
2337 | ||
a748b692 | 2338 | static int link_configure(Link *link) { |
02b59d57 TG |
2339 | int r; |
2340 | ||
ef1ba606 | 2341 | assert(link); |
b22d8a00 | 2342 | assert(link->network); |
8434fd5c | 2343 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 2344 | |
02e28621 ДГ |
2345 | /* Drop foreign config, but ignore loopback or critical devices. |
2346 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
2347 | if (!(link->flags & IFF_LOOPBACK) && !(link->network->dhcp_critical)) { | |
e5d44b34 CH |
2348 | r = link_drop_foreign_config(link); |
2349 | if (r < 0) | |
2350 | return r; | |
2351 | } | |
5e5b137a | 2352 | |
b98b483b AR |
2353 | r = link_set_bridge_fdb(link); |
2354 | if (r < 0) | |
2355 | return r; | |
2356 | ||
23d8b221 SS |
2357 | r = link_set_proxy_arp(link); |
2358 | if (r < 0) | |
2359 | return r; | |
2360 | ||
769d324c LP |
2361 | r = link_set_ipv4_forward(link); |
2362 | if (r < 0) | |
2363 | return r; | |
2364 | ||
2365 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2366 | if (r < 0) |
2367 | return r; | |
2368 | ||
49092e22 SS |
2369 | r = link_set_ipv6_privacy_extensions(link); |
2370 | if (r < 0) | |
2371 | return r; | |
2372 | ||
4f2e437a SS |
2373 | r = link_set_ipv6_accept_ra(link); |
2374 | if (r < 0) | |
2375 | return r; | |
2376 | ||
8749cbcd SS |
2377 | r = link_set_ipv6_dad_transmits(link); |
2378 | if (r < 0) | |
2379 | return r; | |
b69c3180 SS |
2380 | |
2381 | r = link_set_ipv6_hop_limit(link); | |
2382 | if (r < 0) | |
2383 | return r; | |
8749cbcd | 2384 | |
78c958f8 | 2385 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 2386 | r = ipv4ll_configure(link); |
eb34d4af TG |
2387 | if (r < 0) |
2388 | return r; | |
2389 | } | |
2390 | ||
78c958f8 | 2391 | if (link_dhcp4_enabled(link)) { |
3c9b8860 | 2392 | r = dhcp4_configure(link); |
eb34d4af TG |
2393 | if (r < 0) |
2394 | return r; | |
eb34d4af TG |
2395 | } |
2396 | ||
78c958f8 | 2397 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2398 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2399 | if (r < 0) | |
2400 | return r; | |
2401 | ||
2402 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2403 | if (r < 0) | |
2404 | return r; | |
dd43110f TG |
2405 | } |
2406 | ||
62379e88 TG |
2407 | if (link_dhcp6_enabled(link) || |
2408 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
2409 | r = dhcp6_configure(link); |
2410 | if (r < 0) | |
2411 | return r; | |
2412 | } | |
2413 | ||
2414 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 2415 | r = ndisc_configure(link); |
4138fb2c PF |
2416 | if (r < 0) |
2417 | return r; | |
2418 | } | |
2419 | ||
8e1ad1ea | 2420 | if (link_lldp_rx_enabled(link)) { |
fc6a313b LP |
2421 | r = sd_lldp_new(&link->lldp); |
2422 | if (r < 0) | |
2423 | return r; | |
2424 | ||
2425 | r = sd_lldp_set_ifindex(link->lldp, link->ifindex); | |
34437b4f LP |
2426 | if (r < 0) |
2427 | return r; | |
2428 | ||
2429 | r = sd_lldp_match_capabilities(link->lldp, | |
2430 | link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ? | |
6afa6767 BG |
2431 | SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS : |
2432 | SD_LLDP_SYSTEM_CAPABILITIES_ALL); | |
ce43e484 SS |
2433 | if (r < 0) |
2434 | return r; | |
2435 | ||
b553a6b1 LP |
2436 | r = sd_lldp_set_filter_address(link->lldp, &link->mac); |
2437 | if (r < 0) | |
2438 | return r; | |
2439 | ||
ce43e484 SS |
2440 | r = sd_lldp_attach_event(link->lldp, NULL, 0); |
2441 | if (r < 0) | |
2442 | return r; | |
49699bac | 2443 | |
032b27f5 | 2444 | r = sd_lldp_set_callback(link->lldp, lldp_handler, link); |
49699bac SS |
2445 | if (r < 0) |
2446 | return r; | |
273eec24 LP |
2447 | |
2448 | r = link_update_lldp(link); | |
2449 | if (r < 0) | |
2450 | return r; | |
ce43e484 SS |
2451 | } |
2452 | ||
a61bb41c | 2453 | if (link_has_carrier(link)) { |
1e9be60b TG |
2454 | r = link_acquire_conf(link); |
2455 | if (r < 0) | |
2456 | return r; | |
cc544d5f | 2457 | } |
1e9be60b | 2458 | |
3f265037 | 2459 | return link_enter_join_netdev(link); |
505f8da7 TG |
2460 | } |
2461 | ||
1c4baffc | 2462 | static int link_initialized_and_synced(sd_netlink *rtnl, sd_netlink_message *m, |
3c9b8860 | 2463 | void *userdata) { |
5da8149f | 2464 | _cleanup_link_unref_ Link *link = userdata; |
505f8da7 | 2465 | Network *network; |
505f8da7 TG |
2466 | int r; |
2467 | ||
2468 | assert(link); | |
2469 | assert(link->ifname); | |
2470 | assert(link->manager); | |
2471 | ||
8434fd5c | 2472 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 2473 | return 1; |
505f8da7 | 2474 | |
6a7a4e4d | 2475 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 2476 | |
0d4ad91d AR |
2477 | r = link_new_bound_by_list(link); |
2478 | if (r < 0) | |
2479 | return r; | |
2480 | ||
2481 | r = link_handle_bound_by_list(link); | |
2482 | if (r < 0) | |
2483 | return r; | |
2484 | ||
c4a03a56 TG |
2485 | if (!link->network) { |
2486 | r = network_get(link->manager, link->udev_device, link->ifname, | |
2487 | &link->mac, &network); | |
2488 | if (r == -ENOENT) { | |
2489 | link_enter_unmanaged(link); | |
2490 | return 1; | |
2491 | } else if (r < 0) | |
2492 | return r; | |
505f8da7 | 2493 | |
c4a03a56 TG |
2494 | if (link->flags & IFF_LOOPBACK) { |
2495 | if (network->link_local != ADDRESS_FAMILY_NO) | |
2496 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 2497 | |
c4a03a56 TG |
2498 | if (network->dhcp != ADDRESS_FAMILY_NO) |
2499 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 2500 | |
c4a03a56 TG |
2501 | if (network->dhcp_server) |
2502 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
2503 | } | |
bd2efe92 | 2504 | |
c4a03a56 TG |
2505 | r = network_apply(link->manager, network, link); |
2506 | if (r < 0) | |
2507 | return r; | |
2508 | } | |
505f8da7 | 2509 | |
0d4ad91d AR |
2510 | r = link_new_bound_to_list(link); |
2511 | if (r < 0) | |
2512 | return r; | |
2513 | ||
a748b692 TG |
2514 | r = link_configure(link); |
2515 | if (r < 0) | |
2516 | return r; | |
2517 | ||
5da8149f | 2518 | return 1; |
505f8da7 TG |
2519 | } |
2520 | ||
4f561e8e | 2521 | int link_initialized(Link *link, struct udev_device *device) { |
4afd3348 | 2522 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
2523 | int r; |
2524 | ||
2525 | assert(link); | |
2526 | assert(link->manager); | |
2527 | assert(link->manager->rtnl); | |
2528 | assert(device); | |
2529 | ||
8434fd5c | 2530 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
2531 | return 0; |
2532 | ||
679b3605 TG |
2533 | if (link->udev_device) |
2534 | return 0; | |
2535 | ||
79008bdd | 2536 | log_link_debug(link, "udev initialized link"); |
4f561e8e TG |
2537 | |
2538 | link->udev_device = udev_device_ref(device); | |
2539 | ||
3c9b8860 TG |
2540 | /* udev has initialized the link, but we don't know if we have yet |
2541 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
2542 | * when it returns we know that the pending NEWLINKs have already been | |
2543 | * processed and that we are up-to-date */ | |
4f561e8e | 2544 | |
3c9b8860 TG |
2545 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
2546 | link->ifindex); | |
4f561e8e TG |
2547 | if (r < 0) |
2548 | return r; | |
2549 | ||
1c4baffc | 2550 | r = sd_netlink_call_async(link->manager->rtnl, req, |
3c9b8860 | 2551 | link_initialized_and_synced, link, 0, NULL); |
4f561e8e TG |
2552 | if (r < 0) |
2553 | return r; | |
2554 | ||
5da8149f TG |
2555 | link_ref(link); |
2556 | ||
4f561e8e TG |
2557 | return 0; |
2558 | } | |
2559 | ||
c4a03a56 | 2560 | static int link_load(Link *link) { |
0bc70f1d TG |
2561 | _cleanup_free_ char *network_file = NULL, |
2562 | *addresses = NULL, | |
f703cc2c | 2563 | *routes = NULL, |
0bc70f1d TG |
2564 | *dhcp4_address = NULL, |
2565 | *ipv4ll_address = NULL; | |
2566 | union in_addr_union address; | |
f703cc2c | 2567 | union in_addr_union route_dst; |
c598ac76 | 2568 | const char *p; |
c4a03a56 TG |
2569 | int r; |
2570 | ||
2571 | assert(link); | |
2572 | ||
2573 | r = parse_env_file(link->state_file, NEWLINE, | |
2574 | "NETWORK_FILE", &network_file, | |
2575 | "ADDRESSES", &addresses, | |
f703cc2c | 2576 | "ROUTES", &routes, |
0bc70f1d TG |
2577 | "DHCP4_ADDRESS", &dhcp4_address, |
2578 | "IPV4LL_ADDRESS", &ipv4ll_address, | |
c4a03a56 TG |
2579 | NULL); |
2580 | if (r < 0 && r != -ENOENT) | |
2581 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
2582 | ||
2583 | if (network_file) { | |
2584 | Network *network; | |
2585 | char *suffix; | |
2586 | ||
2587 | /* drop suffix */ | |
2588 | suffix = strrchr(network_file, '.'); | |
2589 | if (!suffix) { | |
2590 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
2591 | goto network_file_fail; | |
2592 | } | |
2593 | *suffix = '\0'; | |
2594 | ||
2595 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
2596 | if (r < 0) { | |
2597 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
2598 | goto network_file_fail; | |
2599 | } | |
2600 | ||
2601 | r = network_apply(link->manager, network, link); | |
2602 | if (r < 0) | |
2603 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
2604 | } | |
2605 | ||
2606 | network_file_fail: | |
2607 | ||
2608 | if (addresses) { | |
c598ac76 | 2609 | p = addresses; |
c4a03a56 | 2610 | |
c598ac76 TG |
2611 | for (;;) { |
2612 | _cleanup_free_ char *address_str = NULL; | |
c4a03a56 TG |
2613 | char *prefixlen_str; |
2614 | int family; | |
2615 | unsigned char prefixlen; | |
c4a03a56 | 2616 | |
c598ac76 TG |
2617 | r = extract_first_word(&p, &address_str, NULL, 0); |
2618 | if (r < 0) { | |
2619 | log_link_debug_errno(link, r, "Failed to extract next address string: %m"); | |
2620 | continue; | |
928bc597 TA |
2621 | } |
2622 | if (r == 0) | |
c598ac76 TG |
2623 | break; |
2624 | ||
2625 | prefixlen_str = strchr(address_str, '/'); | |
c4a03a56 | 2626 | if (!prefixlen_str) { |
c598ac76 | 2627 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); |
c4a03a56 TG |
2628 | continue; |
2629 | } | |
2630 | ||
313cefa1 | 2631 | *prefixlen_str++ = '\0'; |
c4a03a56 TG |
2632 | |
2633 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); | |
2634 | if (r != 1) { | |
2635 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
2636 | continue; | |
2637 | } | |
2638 | ||
c598ac76 | 2639 | r = in_addr_from_string_auto(address_str, &family, &address); |
c4a03a56 | 2640 | if (r < 0) { |
c598ac76 | 2641 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); |
c4a03a56 TG |
2642 | continue; |
2643 | } | |
2644 | ||
2645 | r = address_add(link, family, &address, prefixlen, NULL); | |
2646 | if (r < 0) | |
2647 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
2648 | } | |
2649 | } | |
2650 | ||
f703cc2c | 2651 | if (routes) { |
74544b4e TA |
2652 | p = routes; |
2653 | ||
c598ac76 | 2654 | for (;;) { |
f833694d | 2655 | Route *route; |
c598ac76 | 2656 | _cleanup_free_ char *route_str = NULL; |
4afd3348 | 2657 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; |
f833694d | 2658 | usec_t lifetime; |
f703cc2c TG |
2659 | char *prefixlen_str; |
2660 | int family; | |
2661 | unsigned char prefixlen, tos, table; | |
2662 | uint32_t priority; | |
2663 | ||
c598ac76 TG |
2664 | r = extract_first_word(&p, &route_str, NULL, 0); |
2665 | if (r < 0) { | |
2666 | log_link_debug_errno(link, r, "Failed to extract next route string: %m"); | |
2667 | continue; | |
928bc597 TA |
2668 | } |
2669 | if (r == 0) | |
c598ac76 TG |
2670 | break; |
2671 | ||
2672 | prefixlen_str = strchr(route_str, '/'); | |
f703cc2c | 2673 | if (!prefixlen_str) { |
c598ac76 | 2674 | log_link_debug(link, "Failed to parse route %s", route_str); |
f703cc2c TG |
2675 | continue; |
2676 | } | |
2677 | ||
313cefa1 | 2678 | *prefixlen_str++ = '\0'; |
f703cc2c | 2679 | |
f833694d TG |
2680 | r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%hhu/"USEC_FMT, &prefixlen, &tos, &priority, &table, &lifetime); |
2681 | if (r != 5) { | |
2682 | log_link_debug(link, | |
2683 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
2684 | prefixlen_str); | |
f703cc2c TG |
2685 | continue; |
2686 | } | |
2687 | ||
c598ac76 | 2688 | r = in_addr_from_string_auto(route_str, &family, &route_dst); |
f703cc2c | 2689 | if (r < 0) { |
c598ac76 | 2690 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); |
f703cc2c TG |
2691 | continue; |
2692 | } | |
2693 | ||
f833694d | 2694 | r = route_add(link, family, &route_dst, prefixlen, tos, priority, table, &route); |
f703cc2c TG |
2695 | if (r < 0) |
2696 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f833694d TG |
2697 | |
2698 | if (lifetime != USEC_INFINITY) { | |
2699 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime, | |
2700 | 0, route_expire_handler, route); | |
2701 | if (r < 0) | |
2702 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); | |
2703 | } | |
2704 | ||
2705 | route->lifetime = lifetime; | |
2706 | sd_event_source_unref(route->expire); | |
2707 | route->expire = expire; | |
2708 | expire = NULL; | |
f703cc2c TG |
2709 | } |
2710 | } | |
2711 | ||
0bc70f1d TG |
2712 | if (dhcp4_address) { |
2713 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
2714 | if (r < 0) { | |
2715 | log_link_debug_errno(link, r, "Falied to parse DHCPv4 address %s: %m", dhcp4_address); | |
2716 | goto dhcp4_address_fail; | |
2717 | } | |
2718 | ||
2719 | r = sd_dhcp_client_new(&link->dhcp_client); | |
2720 | if (r < 0) | |
2721 | return log_link_error_errno(link, r, "Falied to create DHCPv4 client: %m"); | |
2722 | ||
2723 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
2724 | if (r < 0) | |
595bfe7d | 2725 | return log_link_error_errno(link, r, "Falied to set initial DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
2726 | } |
2727 | ||
2728 | dhcp4_address_fail: | |
2729 | ||
2730 | if (ipv4ll_address) { | |
2731 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
2732 | if (r < 0) { | |
2733 | log_link_debug_errno(link, r, "Falied to parse IPv4LL address %s: %m", ipv4ll_address); | |
2734 | goto ipv4ll_address_fail; | |
2735 | } | |
2736 | ||
2737 | r = sd_ipv4ll_new(&link->ipv4ll); | |
2738 | if (r < 0) | |
2739 | return log_link_error_errno(link, r, "Falied to create IPv4LL client: %m"); | |
2740 | ||
2741 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); | |
2742 | if (r < 0) | |
595bfe7d | 2743 | return log_link_error_errno(link, r, "Falied to set initial IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
2744 | } |
2745 | ||
2746 | ipv4ll_address_fail: | |
2747 | ||
c4a03a56 TG |
2748 | return 0; |
2749 | } | |
2750 | ||
1c4baffc | 2751 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
505f8da7 TG |
2752 | Link *link; |
2753 | _cleanup_udev_device_unref_ struct udev_device *device = NULL; | |
2754 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; | |
2755 | int r; | |
2756 | ||
2757 | assert(m); | |
fbbeb65a | 2758 | assert(m->rtnl); |
505f8da7 TG |
2759 | assert(message); |
2760 | assert(ret); | |
2761 | ||
2762 | r = link_new(m, message, ret); | |
2763 | if (r < 0) | |
2764 | return r; | |
2765 | ||
2766 | link = *ret; | |
2767 | ||
6a7a4e4d | 2768 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 2769 | |
c4a03a56 TG |
2770 | r = link_load(link); |
2771 | if (r < 0) | |
2772 | return r; | |
2773 | ||
75f86906 | 2774 | if (detect_container() <= 0) { |
505f8da7 | 2775 | /* not in a container, udev will be around */ |
ae06ab10 | 2776 | sprintf(ifindex_str, "n%d", link->ifindex); |
505f8da7 | 2777 | device = udev_device_new_from_device_id(m->udev, ifindex_str); |
5c416fc4 TG |
2778 | if (!device) { |
2779 | r = log_link_warning_errno(link, errno, "Could not find udev device: %m"); | |
2780 | goto failed; | |
2781 | } | |
505f8da7 | 2782 | |
3c4cb064 | 2783 | if (udev_device_get_is_initialized(device) <= 0) { |
505f8da7 | 2784 | /* not yet ready */ |
79008bdd | 2785 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 2786 | return 0; |
3c4cb064 | 2787 | } |
505f8da7 | 2788 | |
4f561e8e TG |
2789 | r = link_initialized(link, device); |
2790 | if (r < 0) | |
5c416fc4 | 2791 | goto failed; |
4f561e8e | 2792 | } else { |
5da8149f TG |
2793 | /* we are calling a callback directly, so must take a ref */ |
2794 | link_ref(link); | |
2795 | ||
4f561e8e TG |
2796 | r = link_initialized_and_synced(m->rtnl, NULL, link); |
2797 | if (r < 0) | |
5c416fc4 | 2798 | goto failed; |
4f561e8e | 2799 | } |
505f8da7 | 2800 | |
a748b692 | 2801 | return 0; |
5c416fc4 TG |
2802 | failed: |
2803 | link_enter_failed(link); | |
2804 | return r; | |
a748b692 TG |
2805 | } |
2806 | ||
c601ebf7 | 2807 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
2808 | int r; |
2809 | ||
2810 | assert(link); | |
2811 | ||
2812 | log_link_info(link, "Gained IPv6LL"); | |
2813 | ||
c601ebf7 | 2814 | link->ipv6ll_address = *address; |
e7ab854c TG |
2815 | link_check_ready(link); |
2816 | ||
201f0c91 | 2817 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
e7ab854c TG |
2818 | r = link_acquire_ipv6_conf(link); |
2819 | if (r < 0) { | |
2820 | link_enter_failed(link); | |
2821 | return r; | |
2822 | } | |
2823 | } | |
2824 | ||
2825 | return 0; | |
2826 | } | |
2827 | ||
9c0a72f9 TG |
2828 | static int link_carrier_gained(Link *link) { |
2829 | int r; | |
2830 | ||
2831 | assert(link); | |
2832 | ||
201f0c91 | 2833 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
9c0a72f9 TG |
2834 | r = link_acquire_conf(link); |
2835 | if (r < 0) { | |
2836 | link_enter_failed(link); | |
2837 | return r; | |
2838 | } | |
6fc25497 SS |
2839 | |
2840 | r = link_enter_set_addresses(link); | |
2841 | if (r < 0) | |
2842 | return r; | |
9c0a72f9 TG |
2843 | } |
2844 | ||
0d4ad91d AR |
2845 | r = link_handle_bound_by_list(link); |
2846 | if (r < 0) | |
2847 | return r; | |
2848 | ||
9c0a72f9 TG |
2849 | return 0; |
2850 | } | |
2851 | ||
2852 | static int link_carrier_lost(Link *link) { | |
2853 | int r; | |
2854 | ||
2855 | assert(link); | |
2856 | ||
2857 | r = link_stop_clients(link); | |
2858 | if (r < 0) { | |
2859 | link_enter_failed(link); | |
2860 | return r; | |
2861 | } | |
2862 | ||
0d4ad91d AR |
2863 | r = link_handle_bound_by_list(link); |
2864 | if (r < 0) | |
2865 | return r; | |
2866 | ||
9c0a72f9 TG |
2867 | return 0; |
2868 | } | |
2869 | ||
2870 | int link_carrier_reset(Link *link) { | |
2871 | int r; | |
2872 | ||
2873 | assert(link); | |
2874 | ||
2875 | if (link_has_carrier(link)) { | |
2876 | r = link_carrier_lost(link); | |
2877 | if (r < 0) | |
2878 | return r; | |
2879 | ||
2880 | r = link_carrier_gained(link); | |
2881 | if (r < 0) | |
2882 | return r; | |
2883 | ||
6a7a4e4d | 2884 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
2885 | } |
2886 | ||
2887 | return 0; | |
2888 | } | |
2889 | ||
1c4baffc | 2890 | int link_update(Link *link, sd_netlink_message *m) { |
c49b33ac | 2891 | struct ether_addr mac; |
ca4e095a | 2892 | const char *ifname; |
afe7fd56 | 2893 | uint32_t mtu; |
a61bb41c | 2894 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
2895 | int r; |
2896 | ||
dd3efc09 | 2897 | assert(link); |
b8941f74 | 2898 | assert(link->ifname); |
22936833 TG |
2899 | assert(m); |
2900 | ||
7619683b TG |
2901 | if (link->state == LINK_STATE_LINGER) { |
2902 | link_ref(link); | |
6a7a4e4d | 2903 | log_link_info(link, "Link readded"); |
e331e246 | 2904 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
2905 | |
2906 | r = link_new_carrier_maps(link); | |
2907 | if (r < 0) | |
2908 | return r; | |
7619683b TG |
2909 | } |
2910 | ||
1c4baffc | 2911 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 2912 | if (r >= 0 && !streq(ifname, link->ifname)) { |
6a7a4e4d | 2913 | log_link_info(link, "Renamed to %s", ifname); |
b8941f74 | 2914 | |
0d4ad91d AR |
2915 | link_free_carrier_maps(link); |
2916 | ||
2fc09a9c DM |
2917 | r = free_and_strdup(&link->ifname, ifname); |
2918 | if (r < 0) | |
2919 | return r; | |
0d4ad91d AR |
2920 | |
2921 | r = link_new_carrier_maps(link); | |
2922 | if (r < 0) | |
2923 | return r; | |
b8941f74 TG |
2924 | } |
2925 | ||
1c4baffc | 2926 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
2927 | if (r >= 0 && mtu > 0) { |
2928 | link->mtu = mtu; | |
2929 | if (!link->original_mtu) { | |
2930 | link->original_mtu = mtu; | |
6a7a4e4d | 2931 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
2932 | } |
2933 | ||
2934 | if (link->dhcp_client) { | |
3c9b8860 TG |
2935 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
2936 | link->mtu); | |
afe7fd56 | 2937 | if (r < 0) { |
6a7a4e4d | 2938 | log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); |
afe7fd56 TG |
2939 | return r; |
2940 | } | |
2941 | } | |
9842de0d | 2942 | } |
69629de9 | 2943 | |
e9189a1f TG |
2944 | /* The kernel may broadcast NEWLINK messages without the MAC address |
2945 | set, simply ignore them. */ | |
1c4baffc | 2946 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 2947 | if (r >= 0) { |
3c9b8860 TG |
2948 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
2949 | ETH_ALEN)) { | |
c49b33ac | 2950 | |
3c9b8860 TG |
2951 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
2952 | ETH_ALEN); | |
c49b33ac | 2953 | |
79008bdd | 2954 | log_link_debug(link, "MAC address: " |
20861203 TG |
2955 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
2956 | mac.ether_addr_octet[0], | |
2957 | mac.ether_addr_octet[1], | |
2958 | mac.ether_addr_octet[2], | |
2959 | mac.ether_addr_octet[3], | |
2960 | mac.ether_addr_octet[4], | |
2961 | mac.ether_addr_octet[5]); | |
c49b33ac | 2962 | |
20861203 TG |
2963 | if (link->ipv4ll) { |
2964 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
2965 | if (r < 0) |
2966 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 2967 | } |
c49b33ac | 2968 | |
20861203 | 2969 | if (link->dhcp_client) { |
8341a5c3 ZJS |
2970 | const DUID *duid = link_duid(link); |
2971 | ||
3c9b8860 | 2972 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
2973 | (const uint8_t *) &link->mac, |
2974 | sizeof (link->mac), | |
2975 | ARPHRD_ETHER); | |
6a7a4e4d LP |
2976 | if (r < 0) |
2977 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 2978 | |
8341a5c3 ZJS |
2979 | r = sd_dhcp_client_set_iaid_duid(link->dhcp_client, |
2980 | link->network->iaid, | |
2981 | duid->type, | |
2982 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
2983 | duid->raw_data_len); | |
413708d1 VK |
2984 | if (r < 0) |
2985 | return log_link_warning_errno(link, r, "Could not update DUID/IAID in DHCP client: %m"); | |
c49b33ac | 2986 | } |
4138fb2c PF |
2987 | |
2988 | if (link->dhcp6_client) { | |
8341a5c3 ZJS |
2989 | const DUID* duid = link_duid(link); |
2990 | ||
4138fb2c | 2991 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
2992 | (const uint8_t *) &link->mac, |
2993 | sizeof (link->mac), | |
2994 | ARPHRD_ETHER); | |
6a7a4e4d LP |
2995 | if (r < 0) |
2996 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 VK |
2997 | |
2998 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
2999 | link->network->iaid); | |
3000 | if (r < 0) | |
3001 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
3002 | ||
8341a5c3 ZJS |
3003 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
3004 | duid->type, | |
3005 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3006 | duid->raw_data_len); | |
413708d1 VK |
3007 | if (r < 0) |
3008 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 3009 | } |
c49b33ac | 3010 | } |
4f882b2a TG |
3011 | } |
3012 | ||
a61bb41c TG |
3013 | had_carrier = link_has_carrier(link); |
3014 | ||
3015 | r = link_update_flags(link, m); | |
3016 | if (r < 0) | |
3017 | return r; | |
3018 | ||
273eec24 LP |
3019 | r = link_update_lldp(link); |
3020 | if (r < 0) | |
3021 | return r; | |
3022 | ||
a61bb41c TG |
3023 | carrier_gained = !had_carrier && link_has_carrier(link); |
3024 | carrier_lost = had_carrier && !link_has_carrier(link); | |
3025 | ||
3026 | if (carrier_gained) { | |
6a7a4e4d | 3027 | log_link_info(link, "Gained carrier"); |
a61bb41c | 3028 | |
9c0a72f9 TG |
3029 | r = link_carrier_gained(link); |
3030 | if (r < 0) | |
3031 | return r; | |
a61bb41c | 3032 | } else if (carrier_lost) { |
6a7a4e4d | 3033 | log_link_info(link, "Lost carrier"); |
a61bb41c | 3034 | |
9c0a72f9 TG |
3035 | r = link_carrier_lost(link); |
3036 | if (r < 0) | |
a61bb41c | 3037 | return r; |
a61bb41c TG |
3038 | } |
3039 | ||
3040 | return 0; | |
dd3efc09 | 3041 | } |
fe8db0c5 | 3042 | |
b295beea LP |
3043 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
3044 | bool space = false; | |
3045 | Iterator i; | |
3046 | Link *link; | |
3047 | ||
3048 | assert(f); | |
3049 | assert(prefix); | |
3050 | ||
3051 | if (hashmap_isempty(h)) | |
3052 | return; | |
3053 | ||
3054 | fputs(prefix, f); | |
3055 | HASHMAP_FOREACH(link, h, i) { | |
3056 | if (space) | |
3057 | fputc(' ', f); | |
3058 | ||
3059 | fprintf(f, "%i", link->ifindex); | |
3060 | space = true; | |
3061 | } | |
3062 | ||
3063 | fputc('\n', f); | |
3064 | } | |
3065 | ||
fe8db0c5 | 3066 | int link_save(Link *link) { |
68a8723c | 3067 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 3068 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 3069 | const char *admin_state, *oper_state; |
e7780c8d | 3070 | Address *a; |
c1eb9872 | 3071 | Route *route; |
e7780c8d | 3072 | Iterator i; |
fe8db0c5 TG |
3073 | int r; |
3074 | ||
3075 | assert(link); | |
3076 | assert(link->state_file); | |
68a8723c | 3077 | assert(link->lease_file); |
bbf7c048 TG |
3078 | assert(link->manager); |
3079 | ||
370e9930 TG |
3080 | if (link->state == LINK_STATE_LINGER) { |
3081 | unlink(link->state_file); | |
3082 | return 0; | |
3083 | } | |
3084 | ||
34437b4f LP |
3085 | link_lldp_save(link); |
3086 | ||
deb2e523 TG |
3087 | admin_state = link_state_to_string(link->state); |
3088 | assert(admin_state); | |
3089 | ||
e375dcde TG |
3090 | oper_state = link_operstate_to_string(link->operstate); |
3091 | assert(oper_state); | |
deb2e523 | 3092 | |
fe8db0c5 TG |
3093 | r = fopen_temporary(link->state_file, &f, &temp_path); |
3094 | if (r < 0) | |
6a7a4e4d | 3095 | goto fail; |
fe8db0c5 TG |
3096 | |
3097 | fchmod(fileno(f), 0644); | |
3098 | ||
3099 | fprintf(f, | |
3100 | "# This is private data. Do not parse.\n" | |
deb2e523 | 3101 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
3102 | "OPER_STATE=%s\n", |
3103 | admin_state, oper_state); | |
fe8db0c5 | 3104 | |
bcb7a07e | 3105 | if (link->network) { |
ea352b40 | 3106 | bool space; |
07bdc70d | 3107 | sd_dhcp6_lease *dhcp6_lease = NULL; |
b2a81c0b LP |
3108 | const char *dhcp_domainname = NULL; |
3109 | char **dhcp6_domains = NULL; | |
07bdc70d PF |
3110 | |
3111 | if (link->dhcp6_client) { | |
4058d339 TG |
3112 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
3113 | if (r < 0 && r != -ENOMSG) | |
07bdc70d PF |
3114 | log_link_debug(link, "No DHCPv6 lease"); |
3115 | } | |
b0e39c82 | 3116 | |
adc5b2e2 TG |
3117 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
3118 | ||
b0e39c82 | 3119 | fputs("DNS=", f); |
ea352b40 | 3120 | space = false; |
3df9bec5 | 3121 | fputstrv(f, link->network->dns, NULL, &space); |
d5314fff | 3122 | |
27cb34f5 | 3123 | if (link->network->dhcp_use_dns && |
b0e39c82 TG |
3124 | link->dhcp_lease) { |
3125 | const struct in_addr *addresses; | |
3126 | ||
3127 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
3128 | if (r > 0) { | |
ea352b40 LP |
3129 | if (space) |
3130 | fputc(' ', f); | |
b0e39c82 | 3131 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3132 | space = true; |
3133 | } | |
3134 | } | |
3135 | ||
27cb34f5 | 3136 | if (link->network->dhcp_use_dns && dhcp6_lease) { |
07bdc70d PF |
3137 | struct in6_addr *in6_addrs; |
3138 | ||
3139 | r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs); | |
3140 | if (r > 0) { | |
3141 | if (space) | |
3142 | fputc(' ', f); | |
3143 | serialize_in6_addrs(f, in6_addrs, r); | |
1e7a0e21 LP |
3144 | space = true; |
3145 | } | |
3146 | } | |
3147 | ||
3148 | /* Make sure to flush out old entries before we use the NDISC data */ | |
3149 | ndisc_vacuum(link); | |
3150 | ||
3151 | if (link->network->dhcp_use_dns && link->ndisc_rdnss) { | |
3152 | NDiscRDNSS *dd; | |
3153 | ||
3154 | SET_FOREACH(dd, link->ndisc_rdnss, i) { | |
3155 | if (space) | |
3156 | fputc(' ', f); | |
3157 | ||
3158 | serialize_in6_addrs(f, &dd->address, 1); | |
3159 | space = true; | |
b0e39c82 TG |
3160 | } |
3161 | } | |
3162 | ||
2ce40956 | 3163 | fputc('\n', f); |
b0e39c82 | 3164 | |
2ce40956 | 3165 | fputs("NTP=", f); |
ea352b40 | 3166 | space = false; |
3df9bec5 | 3167 | fputstrv(f, link->network->ntp, NULL, &space); |
d5314fff | 3168 | |
27cb34f5 | 3169 | if (link->network->dhcp_use_ntp && |
b0e39c82 TG |
3170 | link->dhcp_lease) { |
3171 | const struct in_addr *addresses; | |
3172 | ||
3173 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
3174 | if (r > 0) { | |
ea352b40 LP |
3175 | if (space) |
3176 | fputc(' ', f); | |
b0e39c82 | 3177 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3178 | space = true; |
3179 | } | |
3180 | } | |
3181 | ||
27cb34f5 | 3182 | if (link->network->dhcp_use_ntp && dhcp6_lease) { |
07bdc70d PF |
3183 | struct in6_addr *in6_addrs; |
3184 | char **hosts; | |
07bdc70d PF |
3185 | |
3186 | r = sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease, | |
3187 | &in6_addrs); | |
3188 | if (r > 0) { | |
3189 | if (space) | |
3190 | fputc(' ', f); | |
3191 | serialize_in6_addrs(f, in6_addrs, r); | |
3192 | space = true; | |
3193 | } | |
3194 | ||
3195 | r = sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease, &hosts); | |
3df9bec5 LP |
3196 | if (r > 0) |
3197 | fputstrv(f, hosts, NULL, &space); | |
b0e39c82 TG |
3198 | } |
3199 | ||
2ce40956 | 3200 | fputc('\n', f); |
bd8f6538 | 3201 | |
b2a81c0b LP |
3202 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
3203 | if (link->dhcp_lease) | |
3204 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); | |
b2a81c0b LP |
3205 | if (dhcp6_lease) |
3206 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3207 | } |
3208 | ||
b2a81c0b LP |
3209 | fputs("DOMAINS=", f); |
3210 | fputstrv(f, link->network->search_domains, NULL, &space); | |
07bdc70d | 3211 | |
1e7a0e21 LP |
3212 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
3213 | NDiscDNSSL *dd; | |
9b4d1882 | 3214 | |
1e7a0e21 LP |
3215 | if (dhcp_domainname) |
3216 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
3217 | if (dhcp6_domains) | |
3218 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3219 | ||
3220 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3221 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3222 | } | |
b2a81c0b | 3223 | |
2ce40956 | 3224 | fputc('\n', f); |
6192b846 | 3225 | |
3df9bec5 LP |
3226 | fputs("ROUTE_DOMAINS=", f); |
3227 | fputstrv(f, link->network->route_domains, NULL, NULL); | |
b2a81c0b | 3228 | |
1e7a0e21 LP |
3229 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
3230 | NDiscDNSSL *dd; | |
b2a81c0b | 3231 | |
1e7a0e21 LP |
3232 | if (dhcp_domainname) |
3233 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
3234 | if (dhcp6_domains) | |
3235 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3236 | ||
3237 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3238 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3239 | } | |
b2a81c0b | 3240 | |
3df9bec5 | 3241 | fputc('\n', f); |
67272d15 | 3242 | |
3c9b8860 | 3243 | fprintf(f, "LLMNR=%s\n", |
a7e5da6e | 3244 | resolve_support_to_string(link->network->llmnr)); |
aaa297d4 LP |
3245 | fprintf(f, "MDNS=%s\n", |
3246 | resolve_support_to_string(link->network->mdns)); | |
e7780c8d | 3247 | |
ad6c0475 LP |
3248 | if (link->network->dnssec_mode != _DNSSEC_MODE_INVALID) |
3249 | fprintf(f, "DNSSEC=%s\n", | |
3250 | dnssec_mode_to_string(link->network->dnssec_mode)); | |
3251 | ||
8a516214 LP |
3252 | if (!set_isempty(link->network->dnssec_negative_trust_anchors)) { |
3253 | const char *n; | |
3254 | ||
3255 | fputs("DNSSEC_NTA=", f); | |
3256 | space = false; | |
d390f8ef LP |
3257 | SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) |
3258 | fputs_with_space(f, n, NULL, &space); | |
8a516214 LP |
3259 | fputc('\n', f); |
3260 | } | |
3261 | ||
2ce40956 | 3262 | fputs("ADDRESSES=", f); |
e7780c8d TG |
3263 | space = false; |
3264 | SET_FOREACH(a, link->addresses, i) { | |
3265 | _cleanup_free_ char *address_str = NULL; | |
3266 | ||
3267 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
3268 | if (r < 0) | |
3269 | goto fail; | |
3270 | ||
e7780c8d TG |
3271 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
3272 | space = true; | |
3273 | } | |
2ce40956 | 3274 | fputc('\n', f); |
c1eb9872 TG |
3275 | |
3276 | fputs("ROUTES=", f); | |
3277 | space = false; | |
3278 | SET_FOREACH(route, link->routes, i) { | |
3279 | _cleanup_free_ char *route_str = NULL; | |
3280 | ||
3281 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
3282 | if (r < 0) | |
3283 | goto fail; | |
3284 | ||
f833694d TG |
3285 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%hhu/"USEC_FMT, space ? " " : "", route_str, |
3286 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); | |
c1eb9872 TG |
3287 | space = true; |
3288 | } | |
3289 | ||
3290 | fputc('\n', f); | |
bcb7a07e | 3291 | } |
7374f9d8 | 3292 | |
b295beea LP |
3293 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
3294 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 3295 | |
8eb9058d | 3296 | if (link->dhcp_lease) { |
0bc70f1d | 3297 | struct in_addr address; |
8eb9058d LP |
3298 | const char *tz = NULL; |
3299 | ||
0bc70f1d TG |
3300 | assert(link->network); |
3301 | ||
8eb9058d LP |
3302 | r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
3303 | if (r >= 0) | |
3304 | fprintf(f, "TIMEZONE=%s\n", tz); | |
8eb9058d | 3305 | |
0bc70f1d TG |
3306 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
3307 | if (r >= 0) { | |
3308 | fputs("DHCP4_ADDRESS=", f); | |
3309 | serialize_in_addrs(f, &address, 1); | |
3310 | fputc('\n', f); | |
3311 | } | |
d9876a52 | 3312 | |
bd91b83e | 3313 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 3314 | if (r < 0) |
c2d6bd61 | 3315 | goto fail; |
fe8db0c5 | 3316 | |
7374f9d8 | 3317 | fprintf(f, |
b0e39c82 TG |
3318 | "DHCP_LEASE=%s\n", |
3319 | link->lease_file); | |
deb2e523 | 3320 | } else |
68a8723c | 3321 | unlink(link->lease_file); |
fe8db0c5 | 3322 | |
0bc70f1d TG |
3323 | if (link->ipv4ll) { |
3324 | struct in_addr address; | |
3325 | ||
3326 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
3327 | if (r >= 0) { | |
3328 | fputs("IPV4LL_ADDRESS=", f); | |
3329 | serialize_in_addrs(f, &address, 1); | |
3330 | fputc('\n', f); | |
3331 | } | |
3332 | } | |
3333 | ||
c2d6bd61 LP |
3334 | r = fflush_and_check(f); |
3335 | if (r < 0) | |
3336 | goto fail; | |
fe8db0c5 | 3337 | |
c2d6bd61 | 3338 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 3339 | r = -errno; |
c2d6bd61 | 3340 | goto fail; |
fe8db0c5 TG |
3341 | } |
3342 | ||
c2d6bd61 | 3343 | return 0; |
dacd6cee | 3344 | |
c2d6bd61 | 3345 | fail: |
6a7a4e4d | 3346 | (void) unlink(link->state_file); |
6a7a4e4d LP |
3347 | if (temp_path) |
3348 | (void) unlink(temp_path); | |
3349 | ||
dacd6cee | 3350 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
3351 | } |
3352 | ||
84de38c5 TG |
3353 | /* The serialized state in /run is no longer up-to-date. */ |
3354 | void link_dirty(Link *link) { | |
3355 | int r; | |
3356 | ||
3357 | assert(link); | |
3358 | ||
0c241a37 SS |
3359 | /* mark manager dirty as link is dirty */ |
3360 | manager_dirty(link->manager); | |
3361 | ||
84de38c5 TG |
3362 | r = set_ensure_allocated(&link->manager->dirty_links, NULL); |
3363 | if (r < 0) | |
3364 | /* allocation errors are ignored */ | |
3365 | return; | |
3366 | ||
3367 | r = set_put(link->manager->dirty_links, link); | |
0c241a37 SS |
3368 | if (r <= 0) |
3369 | /* don't take another ref if the link was already dirty */ | |
84de38c5 TG |
3370 | return; |
3371 | ||
3372 | link_ref(link); | |
3373 | } | |
3374 | ||
3375 | /* The serialized state in /run is up-to-date */ | |
3376 | void link_clean(Link *link) { | |
3377 | assert(link); | |
3378 | assert(link->manager); | |
3379 | ||
3380 | set_remove(link->manager->dirty_links, link); | |
3381 | link_unref(link); | |
3382 | } | |
3383 | ||
fe8db0c5 | 3384 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 3385 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
3386 | [LINK_STATE_ENSLAVING] = "configuring", |
3387 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
3388 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
3389 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 3390 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 3391 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 3392 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
3393 | }; |
3394 | ||
3395 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
3396 | |
3397 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
3398 | [LINK_OPERSTATE_OFF] = "off", |
3399 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
3400 | [LINK_OPERSTATE_DORMANT] = "dormant", |
3401 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
3402 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
3403 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
3404 | }; | |
3405 | ||
3406 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |