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