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