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