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