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