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