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