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