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