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