]>
Commit | Line | Data |
---|---|---|
f579559b TG |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <netinet/ether.h> | |
23 | #include <linux/if.h> | |
4cc7a82c | 24 | #include <unistd.h> |
f579559b | 25 | |
0b1831c2 | 26 | #include "networkd-link.h" |
3be1d7e0 | 27 | #include "networkd-netdev.h" |
f579559b | 28 | #include "libudev-private.h" |
505f8da7 | 29 | #include "udev-util.h" |
f579559b | 30 | #include "util.h" |
505f8da7 | 31 | #include "virt.h" |
1346b1f0 | 32 | #include "bus-util.h" |
c6f7c917 | 33 | #include "network-internal.h" |
ed942a9e | 34 | #include "conf-parser.h" |
f579559b | 35 | |
fe8db0c5 TG |
36 | #include "dhcp-lease-internal.h" |
37 | ||
505f8da7 | 38 | static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) { |
14b746f7 | 39 | _cleanup_link_unref_ Link *link = NULL; |
505f8da7 | 40 | uint16_t type; |
ca4e095a | 41 | const char *ifname; |
505f8da7 | 42 | int r, ifindex; |
f579559b | 43 | |
0c2f9b84 | 44 | assert(manager); |
505f8da7 | 45 | assert(message); |
f579559b TG |
46 | assert(ret); |
47 | ||
505f8da7 TG |
48 | r = sd_rtnl_message_get_type(message, &type); |
49 | if (r < 0) | |
50 | return r; | |
51 | else if (type != RTM_NEWLINK) | |
52 | return -EINVAL; | |
53 | ||
54 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
55 | if (r < 0) | |
56 | return r; | |
57 | else if (ifindex <= 0) | |
58 | return -EINVAL; | |
59 | ||
60 | r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &ifname); | |
61 | if (r < 0) | |
62 | return r; | |
63 | ||
f579559b TG |
64 | link = new0(Link, 1); |
65 | if (!link) | |
66 | return -ENOMEM; | |
67 | ||
14b746f7 | 68 | link->n_ref = 1; |
5a3eb5a7 | 69 | link->manager = manager; |
505f8da7 TG |
70 | link->state = LINK_STATE_INITIALIZING; |
71 | link->ifindex = ifindex; | |
72 | link->ifname = strdup(ifname); | |
73 | if (!link->ifname) | |
74 | return -ENOMEM; | |
f579559b | 75 | |
512922f8 TG |
76 | r = sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac); |
77 | if (r < 0) | |
1a941ac4 | 78 | log_debug_link(link, "MAC address not found for new device, continuing without"); |
512922f8 | 79 | |
ae06ab10 | 80 | r = asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex); |
fe8db0c5 | 81 | if (r < 0) |
315db1a8 | 82 | return -ENOMEM; |
fe8db0c5 | 83 | |
ae06ab10 | 84 | r = asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex); |
68a8723c TG |
85 | if (r < 0) |
86 | return -ENOMEM; | |
87 | ||
ae06ab10 TG |
88 | r = hashmap_ensure_allocated(&manager->links, NULL, NULL); |
89 | if (r < 0) | |
90 | return r; | |
91 | ||
92 | r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link); | |
f579559b TG |
93 | if (r < 0) |
94 | return r; | |
95 | ||
96 | *ret = link; | |
97 | link = NULL; | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
14b746f7 | 102 | static void link_free(Link *link) { |
428fd0a7 TG |
103 | Address *address; |
104 | ||
f579559b TG |
105 | if (!link) |
106 | return; | |
107 | ||
428fd0a7 TG |
108 | while ((address = link->addresses)) { |
109 | LIST_REMOVE(addresses, link->addresses, address); | |
110 | address_free(address); | |
111 | } | |
112 | ||
11bf3cce LP |
113 | while ((address = link->pool_addresses)) { |
114 | LIST_REMOVE(addresses, link->pool_addresses, address); | |
115 | address_free(address); | |
116 | } | |
117 | ||
e5b04c8d | 118 | sd_dhcp_client_unref(link->dhcp_client); |
a6cc569e | 119 | sd_dhcp_lease_unref(link->dhcp_lease); |
f5be5601 | 120 | |
68a8723c TG |
121 | unlink(link->lease_file); |
122 | free(link->lease_file); | |
123 | ||
56cd007a | 124 | sd_ipv4ll_unref(link->ipv4ll); |
4138fb2c | 125 | sd_dhcp6_client_unref(link->dhcp6_client); |
6b66097b | 126 | sd_icmp6_nd_unref(link->icmp6_router_discovery); |
4138fb2c | 127 | |
28aeb07f | 128 | if (link->manager) |
ae06ab10 | 129 | hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)); |
f579559b | 130 | |
c166a070 | 131 | free(link->ifname); |
68a8723c TG |
132 | |
133 | unlink(link->state_file); | |
fe8db0c5 | 134 | free(link->state_file); |
c166a070 | 135 | |
b5db00e5 UTL |
136 | udev_device_unref(link->udev_device); |
137 | ||
f579559b TG |
138 | free(link); |
139 | } | |
140 | ||
14b746f7 TG |
141 | Link *link_unref(Link *link) { |
142 | if (link && (-- link->n_ref <= 0)) | |
143 | link_free(link); | |
144 | ||
145 | return NULL; | |
146 | } | |
147 | ||
148 | Link *link_ref(Link *link) { | |
149 | if (link) | |
150 | assert_se(++ link->n_ref >= 2); | |
151 | ||
152 | return link; | |
153 | } | |
154 | ||
11a7f229 TG |
155 | int link_get(Manager *m, int ifindex, Link **ret) { |
156 | Link *link; | |
11a7f229 TG |
157 | |
158 | assert(m); | |
11a7f229 TG |
159 | assert(ifindex); |
160 | assert(ret); | |
161 | ||
ae06ab10 | 162 | link = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
11a7f229 TG |
163 | if (!link) |
164 | return -ENODEV; | |
165 | ||
166 | *ret = link; | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
370e9930 TG |
171 | void link_drop(Link *link) { |
172 | if (!link || link->state == LINK_STATE_LINGER) | |
173 | return; | |
174 | ||
175 | link->state = LINK_STATE_LINGER; | |
176 | ||
7619683b | 177 | log_debug_link(link, "link removed"); |
370e9930 TG |
178 | |
179 | link_unref(link); | |
180 | ||
181 | return; | |
182 | } | |
183 | ||
57bd6899 TG |
184 | static void link_enter_unmanaged(Link *link) { |
185 | assert(link); | |
186 | ||
df9aa406 | 187 | log_debug_link(link, "unmanaged"); |
57bd6899 TG |
188 | |
189 | link->state = LINK_STATE_UNMANAGED; | |
190 | ||
191 | link_save(link); | |
192 | } | |
193 | ||
111bb8f9 TG |
194 | static int link_stop_clients(Link *link) { |
195 | int r = 0, k; | |
196 | ||
197 | assert(link); | |
198 | assert(link->manager); | |
199 | assert(link->manager->event); | |
200 | ||
201 | if (!link->network) | |
202 | return 0; | |
203 | ||
ba179154 | 204 | if (link->dhcp_client) { |
111bb8f9 TG |
205 | k = sd_dhcp_client_stop(link->dhcp_client); |
206 | if (k < 0) { | |
207 | log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r)); | |
208 | r = k; | |
209 | } | |
210 | } | |
211 | ||
ba179154 | 212 | if (link->ipv4ll) { |
111bb8f9 TG |
213 | k = sd_ipv4ll_stop(link->ipv4ll); |
214 | if (k < 0) { | |
215 | log_warning_link(link, "Could not stop IPv4 link-local: %s", strerror(-r)); | |
216 | r = k; | |
217 | } | |
218 | } | |
219 | ||
ba179154 | 220 | if (link->dhcp_server) { |
dd43110f TG |
221 | k = sd_dhcp_server_stop(link->dhcp_server); |
222 | if (k < 0) { | |
223 | log_warning_link(link, "Could not stop DHCPv4 server: %s", strerror(-r)); | |
224 | r = k; | |
225 | } | |
226 | } | |
227 | ||
ba179154 | 228 | if(link->icmp6_router_discovery) { |
1873a3d3 PF |
229 | |
230 | if (link->dhcp6_client) { | |
231 | k = sd_dhcp6_client_stop(link->dhcp6_client); | |
232 | if (k < 0) { | |
233 | log_warning_link(link, "Could not stop DHCPv6 client: %s", strerror(-r)); | |
234 | r = k; | |
235 | } | |
236 | } | |
4138fb2c | 237 | |
1873a3d3 | 238 | k = sd_icmp6_nd_stop(link->icmp6_router_discovery); |
4138fb2c | 239 | if (k < 0) { |
1873a3d3 | 240 | log_warning_link(link, "Could not stop ICMPv6 router discovery: %s", strerror(-r)); |
4138fb2c PF |
241 | r = k; |
242 | } | |
243 | } | |
244 | ||
111bb8f9 TG |
245 | return r; |
246 | } | |
247 | ||
b22d8a00 | 248 | void link_enter_failed(Link *link) { |
ef1ba606 | 249 | assert(link); |
f882c247 | 250 | |
370e9930 | 251 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
252 | return; |
253 | ||
39032b87 | 254 | log_warning_link(link, "failed"); |
449f7554 | 255 | |
ef1ba606 | 256 | link->state = LINK_STATE_FAILED; |
fe8db0c5 | 257 | |
111bb8f9 TG |
258 | link_stop_clients(link); |
259 | ||
fe8db0c5 | 260 | link_save(link); |
f882c247 TG |
261 | } |
262 | ||
b22d8a00 TG |
263 | void link_client_handler(Link *link) { |
264 | assert(link); | |
265 | ||
266 | if (link->ipv4ll_address && link->ipv4ll_route) | |
267 | log_debug_link(link, "IPv4LL configured"); | |
268 | else | |
269 | log_debug_link(link, "IPv4LL not configured"); | |
270 | ||
271 | return; | |
272 | } | |
273 | ||
4f434938 LP |
274 | static Address* link_find_dhcp_server_address(Link *link) { |
275 | Address *address; | |
276 | ||
277 | assert(link); | |
278 | assert(link->network); | |
279 | ||
280 | /* The the first statically configured address if there is any */ | |
281 | LIST_FOREACH(addresses, address, link->network->static_addresses) { | |
282 | ||
283 | if (address->family != AF_INET) | |
284 | continue; | |
285 | ||
af93291c | 286 | if (in_addr_is_null(address->family, &address->in_addr)) |
4f434938 LP |
287 | continue; |
288 | ||
289 | return address; | |
290 | } | |
291 | ||
292 | /* If that didn't work, find a suitable address we got from the pool */ | |
293 | LIST_FOREACH(addresses, address, link->pool_addresses) { | |
294 | if (address->family != AF_INET) | |
295 | continue; | |
296 | ||
297 | return address; | |
298 | } | |
299 | ||
300 | return NULL; | |
301 | } | |
302 | ||
dd43110f TG |
303 | static int link_enter_configured(Link *link) { |
304 | int r; | |
305 | ||
306 | assert(link); | |
307 | assert(link->network); | |
308 | assert(link->state == LINK_STATE_SETTING_ROUTES); | |
309 | ||
7c16313f TG |
310 | if (link->network->dhcp_server && |
311 | !sd_dhcp_server_is_running(link->dhcp_server)) { | |
4f434938 LP |
312 | struct in_addr pool_start; |
313 | Address *address; | |
314 | ||
315 | address = link_find_dhcp_server_address(link); | |
316 | if (!address) { | |
317 | log_warning_link(link, "Failed to find suitable address for DHCPv4 server instance."); | |
318 | link_enter_failed(link); | |
319 | return 0; | |
320 | } | |
321 | ||
dd43110f TG |
322 | log_debug_link(link, "offering DHCPv4 leases"); |
323 | ||
59b8f6b6 | 324 | r = sd_dhcp_server_set_address(link->dhcp_server, &address->in_addr.in, address->prefixlen); |
4f434938 LP |
325 | if (r < 0) |
326 | return r; | |
327 | ||
328 | /* offer 32 addresses starting from the address following the server address */ | |
329 | pool_start.s_addr = htobe32(be32toh(address->in_addr.in.s_addr) + 1); | |
330 | r = sd_dhcp_server_set_lease_pool(link->dhcp_server, | |
331 | &pool_start, 32); | |
332 | if (r < 0) | |
333 | return r; | |
334 | ||
335 | /* TODO: | |
336 | r = sd_dhcp_server_set_router(link->dhcp_server, | |
337 | &main_address->in_addr.in); | |
338 | if (r < 0) | |
339 | return r; | |
340 | ||
341 | r = sd_dhcp_server_set_prefixlen(link->dhcp_server, | |
342 | main_address->prefixlen); | |
343 | if (r < 0) | |
344 | return r; | |
345 | */ | |
346 | ||
dd43110f TG |
347 | r = sd_dhcp_server_start(link->dhcp_server); |
348 | if (r < 0) { | |
349 | log_warning_link(link, "could not start DHCPv4 server " | |
350 | "instance: %s", strerror(-r)); | |
351 | ||
352 | link_enter_failed(link); | |
353 | ||
354 | return 0; | |
355 | } | |
356 | } | |
357 | ||
358 | log_info_link(link, "link configured"); | |
359 | ||
360 | link->state = LINK_STATE_CONFIGURED; | |
361 | ||
362 | link_save(link); | |
363 | ||
364 | return 0; | |
365 | } | |
366 | ||
f882c247 | 367 | static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 368 | _cleanup_link_unref_ Link *link = userdata; |
f882c247 TG |
369 | int r; |
370 | ||
f5be5601 | 371 | assert(link->route_messages > 0); |
370e9930 TG |
372 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
373 | LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED, | |
374 | LINK_STATE_LINGER)); | |
f882c247 | 375 | |
f5be5601 | 376 | link->route_messages --; |
f882c247 | 377 | |
77a008c0 | 378 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
379 | return 1; |
380 | ||
381 | r = sd_rtnl_message_get_errno(m); | |
c166a070 | 382 | if (r < 0 && r != -EEXIST) |
c9ccc19f | 383 | log_struct_link(LOG_WARNING, link, |
97578344 | 384 | "MESSAGE=%-*s: could not set route: %s", |
987efa17 | 385 | IFNAMSIZ, |
c9ccc19f TG |
386 | link->ifname, strerror(-r), |
387 | "ERRNO=%d", -r, | |
388 | NULL); | |
f882c247 | 389 | |
f5be5601 TG |
390 | /* we might have received an old reply after moving back to SETTING_ADDRESSES, |
391 | * ignore it */ | |
392 | if (link->route_messages == 0 && link->state == LINK_STATE_SETTING_ROUTES) { | |
39032b87 | 393 | log_debug_link(link, "routes set"); |
dd3efc09 TG |
394 | link_enter_configured(link); |
395 | } | |
f882c247 TG |
396 | |
397 | return 1; | |
398 | } | |
399 | ||
e1ea665e EY |
400 | static int link_set_dhcp_routes(Link *link) { |
401 | struct sd_dhcp_route *static_routes; | |
a2ba62c7 | 402 | int r, n, i; |
e1ea665e EY |
403 | |
404 | assert(link); | |
405 | ||
a2ba62c7 LP |
406 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes); |
407 | if (n < 0) { | |
408 | if (n != -ENOENT) | |
409 | log_warning_link(link, "DHCP error: could not get routes: %s", strerror(-n)); | |
410 | return n; | |
e1ea665e EY |
411 | } |
412 | ||
a2ba62c7 | 413 | for (i = 0; i < n; i++) { |
e1ea665e EY |
414 | _cleanup_route_free_ Route *route = NULL; |
415 | ||
28cc555d | 416 | r = route_new_dynamic(&route, RTPROT_DHCP); |
e1ea665e EY |
417 | if (r < 0) { |
418 | log_error_link(link, "Could not allocate route: %s", | |
419 | strerror(-r)); | |
420 | return r; | |
421 | } | |
422 | ||
423 | route->family = AF_INET; | |
424 | route->in_addr.in = static_routes[i].gw_addr; | |
425 | route->dst_addr.in = static_routes[i].dst_addr; | |
426 | route->dst_prefixlen = static_routes[i].dst_prefixlen; | |
4faefc7f | 427 | route->metrics = DHCP_ROUTE_METRIC; |
e1ea665e EY |
428 | |
429 | r = route_configure(route, link, &route_handler); | |
430 | if (r < 0) { | |
431 | log_warning_link(link, | |
432 | "could not set host route: %s", strerror(-r)); | |
433 | return r; | |
434 | } | |
435 | ||
e1ea665e EY |
436 | link->route_messages ++; |
437 | } | |
438 | ||
439 | return 0; | |
440 | } | |
441 | ||
f882c247 | 442 | static int link_enter_set_routes(Link *link) { |
a6cc569e | 443 | Route *rt; |
f882c247 TG |
444 | int r; |
445 | ||
446 | assert(link); | |
447 | assert(link->network); | |
ef1ba606 | 448 | assert(link->state == LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 449 | |
ef1ba606 | 450 | link->state = LINK_STATE_SETTING_ROUTES; |
f882c247 | 451 | |
3d3d4255 | 452 | LIST_FOREACH(routes, rt, link->network->static_routes) { |
a6cc569e | 453 | r = route_configure(rt, link, &route_handler); |
dd3efc09 | 454 | if (r < 0) { |
3333d748 ZJS |
455 | log_warning_link(link, |
456 | "could not set routes: %s", strerror(-r)); | |
ef1ba606 TG |
457 | link_enter_failed(link); |
458 | return r; | |
dd3efc09 | 459 | } |
c166a070 | 460 | |
f5be5601 TG |
461 | link->route_messages ++; |
462 | } | |
463 | ||
a6cc569e TG |
464 | if (link->dhcp_lease) { |
465 | _cleanup_route_free_ Route *route = NULL; | |
9765ce69 | 466 | _cleanup_route_free_ Route *route_gw = NULL; |
a6cc569e TG |
467 | struct in_addr gateway; |
468 | ||
469 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); | |
8ddbeaa2 | 470 | if (r < 0 && r != -ENOENT) { |
b1666580 TG |
471 | log_warning_link(link, "DHCP error: could not get gateway: %s", |
472 | strerror(-r)); | |
a6cc569e TG |
473 | return r; |
474 | } | |
475 | ||
8ddbeaa2 | 476 | if (r >= 0) { |
28cc555d | 477 | r = route_new_dynamic(&route, RTPROT_DHCP); |
8ddbeaa2 UTL |
478 | if (r < 0) { |
479 | log_error_link(link, "Could not allocate route: %s", | |
480 | strerror(-r)); | |
481 | return r; | |
482 | } | |
a6cc569e | 483 | |
28cc555d | 484 | r = route_new_dynamic(&route_gw, RTPROT_DHCP); |
8ddbeaa2 UTL |
485 | if (r < 0) { |
486 | log_error_link(link, "Could not allocate route: %s", | |
487 | strerror(-r)); | |
488 | return r; | |
489 | } | |
9765ce69 | 490 | |
8ddbeaa2 UTL |
491 | /* The dhcp netmask may mask out the gateway. Add an explicit |
492 | * route for the gw host so that we can route no matter the | |
493 | * netmask or existing kernel route tables. */ | |
494 | route_gw->family = AF_INET; | |
495 | route_gw->dst_addr.in = gateway; | |
496 | route_gw->dst_prefixlen = 32; | |
497 | route_gw->scope = RT_SCOPE_LINK; | |
4faefc7f | 498 | route_gw->metrics = DHCP_ROUTE_METRIC; |
9765ce69 | 499 | |
8ddbeaa2 UTL |
500 | r = route_configure(route_gw, link, &route_handler); |
501 | if (r < 0) { | |
502 | log_warning_link(link, | |
503 | "could not set host route: %s", strerror(-r)); | |
504 | return r; | |
505 | } | |
9765ce69 | 506 | |
8ddbeaa2 | 507 | link->route_messages ++; |
9765ce69 | 508 | |
8ddbeaa2 UTL |
509 | route->family = AF_INET; |
510 | route->in_addr.in = gateway; | |
4faefc7f | 511 | route->metrics = DHCP_ROUTE_METRIC; |
a6cc569e | 512 | |
8ddbeaa2 UTL |
513 | r = route_configure(route, link, &route_handler); |
514 | if (r < 0) { | |
515 | log_warning_link(link, | |
516 | "could not set routes: %s", strerror(-r)); | |
517 | link_enter_failed(link); | |
518 | return r; | |
519 | } | |
520 | ||
521 | link->route_messages ++; | |
f5be5601 | 522 | } |
e1ea665e EY |
523 | |
524 | if (link->network->dhcp_routes) | |
525 | link_set_dhcp_routes(link); | |
8ddbeaa2 | 526 | } |
f5be5601 | 527 | |
8ddbeaa2 UTL |
528 | if (link->route_messages == 0) { |
529 | link_enter_configured(link); | |
431ca2ce TG |
530 | } else |
531 | log_debug_link(link, "setting routes"); | |
f882c247 TG |
532 | |
533 | return 0; | |
534 | } | |
535 | ||
b22d8a00 | 536 | int link_route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 537 | _cleanup_link_unref_ Link *link = userdata; |
5c1d3fc9 UTL |
538 | int r; |
539 | ||
540 | assert(m); | |
541 | assert(link); | |
542 | assert(link->ifname); | |
543 | ||
5da8149f | 544 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
5c1d3fc9 UTL |
545 | return 1; |
546 | ||
547 | r = sd_rtnl_message_get_errno(m); | |
b90b025a | 548 | if (r < 0 && r != -ESRCH) |
5c1d3fc9 | 549 | log_struct_link(LOG_WARNING, link, |
97578344 | 550 | "MESSAGE=%-*s: could not drop route: %s", |
987efa17 | 551 | IFNAMSIZ, |
5c1d3fc9 UTL |
552 | link->ifname, strerror(-r), |
553 | "ERRNO=%d", -r, | |
554 | NULL); | |
555 | ||
5bdd314c | 556 | return 1; |
5c1d3fc9 UTL |
557 | } |
558 | ||
b22d8a00 | 559 | int link_get_address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
4958aee4 TG |
560 | _cleanup_link_unref_ Link *link = userdata; |
561 | int r; | |
562 | ||
563 | assert(rtnl); | |
564 | assert(m); | |
565 | assert(link); | |
566 | assert(link->manager); | |
567 | ||
568 | for (; m; m = sd_rtnl_message_next(m)) { | |
569 | r = sd_rtnl_message_get_errno(m); | |
570 | if (r < 0) { | |
571 | log_debug_link(link, "getting address failed: %s", strerror(-r)); | |
572 | continue; | |
573 | } | |
574 | ||
575 | r = link_rtnl_process_address(rtnl, m, link->manager); | |
576 | if (r < 0) | |
577 | log_warning_link(link, "could not process address: %s", strerror(-r)); | |
578 | } | |
579 | ||
580 | return 1; | |
581 | } | |
582 | ||
f882c247 | 583 | static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 584 | _cleanup_link_unref_ Link *link = userdata; |
f882c247 TG |
585 | int r; |
586 | ||
4958aee4 | 587 | assert(rtnl); |
f5be5601 TG |
588 | assert(m); |
589 | assert(link); | |
590 | assert(link->ifname); | |
591 | assert(link->addr_messages > 0); | |
370e9930 TG |
592 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
593 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
f882c247 | 594 | |
f5be5601 | 595 | link->addr_messages --; |
f882c247 | 596 | |
5da8149f | 597 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
598 | return 1; |
599 | ||
600 | r = sd_rtnl_message_get_errno(m); | |
c166a070 | 601 | if (r < 0 && r != -EEXIST) |
c9ccc19f | 602 | log_struct_link(LOG_WARNING, link, |
97578344 | 603 | "MESSAGE=%-*s: could not set address: %s", |
987efa17 | 604 | IFNAMSIZ, |
3333d748 ZJS |
605 | link->ifname, strerror(-r), |
606 | "ERRNO=%d", -r, | |
607 | NULL); | |
5bdd314c | 608 | else if (r >= 0) { |
4958aee4 TG |
609 | /* calling handler directly so take a ref */ |
610 | link_ref(link); | |
611 | link_get_address_handler(rtnl, m, link); | |
612 | } | |
f882c247 | 613 | |
f5be5601 | 614 | if (link->addr_messages == 0) { |
39032b87 | 615 | log_debug_link(link, "addresses set"); |
ef1ba606 | 616 | link_enter_set_routes(link); |
dd3efc09 | 617 | } |
f882c247 TG |
618 | |
619 | return 1; | |
620 | } | |
621 | ||
622 | static int link_enter_set_addresses(Link *link) { | |
a6cc569e | 623 | Address *ad; |
f882c247 | 624 | int r; |
68ceb9df | 625 | uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; |
f882c247 TG |
626 | |
627 | assert(link); | |
628 | assert(link->network); | |
f5be5601 | 629 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 630 | |
ef1ba606 | 631 | link->state = LINK_STATE_SETTING_ADDRESSES; |
f882c247 | 632 | |
3d3d4255 | 633 | LIST_FOREACH(addresses, ad, link->network->static_addresses) { |
a6cc569e | 634 | r = address_configure(ad, link, &address_handler); |
dd3efc09 | 635 | if (r < 0) { |
3333d748 ZJS |
636 | log_warning_link(link, |
637 | "could not set addresses: %s", strerror(-r)); | |
ef1ba606 TG |
638 | link_enter_failed(link); |
639 | return r; | |
dd3efc09 | 640 | } |
c166a070 | 641 | |
f5be5601 TG |
642 | link->addr_messages ++; |
643 | } | |
644 | ||
a6cc569e TG |
645 | if (link->dhcp_lease) { |
646 | _cleanup_address_free_ Address *address = NULL; | |
647 | struct in_addr addr; | |
648 | struct in_addr netmask; | |
649 | unsigned prefixlen; | |
650 | ||
651 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr); | |
652 | if (r < 0) { | |
653 | log_warning_link(link, "DHCP error: no address: %s", | |
654 | strerror(-r)); | |
655 | return r; | |
656 | } | |
657 | ||
68ceb9df PF |
658 | if (!link->network->dhcp_critical) { |
659 | r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, | |
660 | &lifetime); | |
661 | if (r < 0) { | |
662 | log_warning_link(link, "DHCP error: no lifetime: %s", | |
663 | strerror(-r)); | |
664 | return r; | |
665 | } | |
666 | } | |
667 | ||
a6cc569e TG |
668 | r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); |
669 | if (r < 0) { | |
670 | log_warning_link(link, "DHCP error: no netmask: %s", | |
671 | strerror(-r)); | |
672 | return r; | |
673 | } | |
674 | ||
44e7b949 | 675 | prefixlen = in_addr_netmask_to_prefixlen(&netmask); |
a6cc569e TG |
676 | |
677 | r = address_new_dynamic(&address); | |
678 | if (r < 0) { | |
679 | log_error_link(link, "Could not allocate address: %s", | |
680 | strerror(-r)); | |
681 | return r; | |
682 | } | |
683 | ||
684 | address->family = AF_INET; | |
685 | address->in_addr.in = addr; | |
68ceb9df PF |
686 | address->cinfo.ifa_prefered = lifetime; |
687 | address->cinfo.ifa_valid = lifetime; | |
a6cc569e TG |
688 | address->prefixlen = prefixlen; |
689 | address->broadcast.s_addr = addr.s_addr | ~netmask.s_addr; | |
690 | ||
46ba9fba TG |
691 | /* use update rather than configure so that we will update the lifetime |
692 | of an existing address if it has already been configured */ | |
693 | r = address_update(address, link, &address_handler); | |
f5be5601 | 694 | if (r < 0) { |
3333d748 ZJS |
695 | log_warning_link(link, |
696 | "could not set addresses: %s", strerror(-r)); | |
f5be5601 TG |
697 | link_enter_failed(link); |
698 | return r; | |
699 | } | |
700 | ||
701 | link->addr_messages ++; | |
f882c247 TG |
702 | } |
703 | ||
431ca2ce TG |
704 | if (link->addr_messages == 0) { |
705 | link_enter_set_routes(link); | |
706 | } else | |
707 | log_debug_link(link, "setting addresses"); | |
708 | ||
f882c247 TG |
709 | return 0; |
710 | } | |
711 | ||
b22d8a00 | 712 | int link_address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 713 | _cleanup_link_unref_ Link *link = userdata; |
ff254138 TG |
714 | int r; |
715 | ||
716 | assert(m); | |
717 | assert(link); | |
718 | assert(link->ifname); | |
719 | ||
5da8149f | 720 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
ff254138 TG |
721 | return 1; |
722 | ||
723 | r = sd_rtnl_message_get_errno(m); | |
b90b025a | 724 | if (r < 0 && r != -EADDRNOTAVAIL) |
c9ccc19f | 725 | log_struct_link(LOG_WARNING, link, |
97578344 | 726 | "MESSAGE=%-*s: could not drop address: %s", |
987efa17 | 727 | IFNAMSIZ, |
c9ccc19f TG |
728 | link->ifname, strerror(-r), |
729 | "ERRNO=%d", -r, | |
730 | NULL); | |
ff254138 | 731 | |
5bdd314c | 732 | return 1; |
ff254138 TG |
733 | } |
734 | ||
1346b1f0 | 735 | static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
5da8149f | 736 | _cleanup_link_unref_ Link *link = userdata; |
1346b1f0 TG |
737 | int r; |
738 | ||
b226d99b TG |
739 | assert(link); |
740 | ||
5da8149f | 741 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
370e9930 | 742 | return 1; |
370e9930 | 743 | |
1346b1f0 TG |
744 | r = sd_bus_message_get_errno(m); |
745 | if (r < 0) | |
70b34f5d MM |
746 | r = -r; |
747 | if (r > 0) | |
748 | log_warning_link(link, "Could not set hostname: %s", strerror(r)); | |
1346b1f0 TG |
749 | |
750 | return 1; | |
751 | } | |
752 | ||
b226d99b | 753 | static int link_set_hostname(Link *link, const char *hostname) { |
1346b1f0 TG |
754 | _cleanup_bus_message_unref_ sd_bus_message *m = NULL; |
755 | int r = 0; | |
756 | ||
b226d99b TG |
757 | assert(link); |
758 | assert(link->manager); | |
1346b1f0 TG |
759 | assert(hostname); |
760 | ||
b226d99b | 761 | log_debug_link(link, "Setting transient hostname: '%s'", hostname); |
1346b1f0 | 762 | |
b226d99b TG |
763 | if (!link->manager->bus) { /* TODO: replace by assert when we can rely on kdbus */ |
764 | log_info_link(link, "Not connected to system bus, ignoring transient hostname."); | |
bcbca829 TG |
765 | return 0; |
766 | } | |
767 | ||
1346b1f0 | 768 | r = sd_bus_message_new_method_call( |
b226d99b | 769 | link->manager->bus, |
151b9b96 | 770 | &m, |
1346b1f0 TG |
771 | "org.freedesktop.hostname1", |
772 | "/org/freedesktop/hostname1", | |
773 | "org.freedesktop.hostname1", | |
151b9b96 | 774 | "SetHostname"); |
1346b1f0 TG |
775 | if (r < 0) |
776 | return r; | |
777 | ||
778 | r = sd_bus_message_append(m, "sb", hostname, false); | |
779 | if (r < 0) | |
780 | return r; | |
781 | ||
19befb2d | 782 | r = sd_bus_call_async(link->manager->bus, NULL, m, set_hostname_handler, link, 0); |
5da8149f | 783 | if (r < 0) { |
b226d99b | 784 | log_error_link(link, "Could not set transient hostname: %s", strerror(-r)); |
5da8149f TG |
785 | return r; |
786 | } | |
b226d99b TG |
787 | |
788 | link_ref(link); | |
1346b1f0 | 789 | |
5da8149f | 790 | return 0; |
1346b1f0 TG |
791 | } |
792 | ||
4f882b2a | 793 | static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 794 | _cleanup_link_unref_ Link *link = userdata; |
4f882b2a TG |
795 | int r; |
796 | ||
797 | assert(m); | |
798 | assert(link); | |
799 | assert(link->ifname); | |
800 | ||
5da8149f | 801 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
802 | return 1; |
803 | ||
804 | r = sd_rtnl_message_get_errno(m); | |
c9ccc19f TG |
805 | if (r < 0) |
806 | log_struct_link(LOG_WARNING, link, | |
97578344 TG |
807 | "MESSAGE=%-*s: could not set MTU: %s", |
808 | IFNAMSIZ, link->ifname, strerror(-r), | |
c9ccc19f TG |
809 | "ERRNO=%d", -r, |
810 | NULL); | |
4f882b2a TG |
811 | |
812 | return 1; | |
813 | } | |
814 | ||
815 | static int link_set_mtu(Link *link, uint32_t mtu) { | |
cf6a8911 | 816 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; |
4f882b2a TG |
817 | int r; |
818 | ||
819 | assert(link); | |
820 | assert(link->manager); | |
821 | assert(link->manager->rtnl); | |
822 | ||
823 | log_debug_link(link, "setting MTU: %" PRIu32, mtu); | |
824 | ||
151b9b96 LP |
825 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, |
826 | RTM_SETLINK, link->ifindex); | |
4f882b2a TG |
827 | if (r < 0) { |
828 | log_error_link(link, "Could not allocate RTM_SETLINK message"); | |
829 | return r; | |
830 | } | |
831 | ||
832 | r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu); | |
833 | if (r < 0) { | |
834 | log_error_link(link, "Could not append MTU: %s", strerror(-r)); | |
835 | return r; | |
836 | } | |
837 | ||
838 | r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL); | |
839 | if (r < 0) { | |
840 | log_error_link(link, | |
841 | "Could not send rtnetlink message: %s", strerror(-r)); | |
842 | return r; | |
843 | } | |
844 | ||
ae941762 | 845 | link_ref(link); |
b226d99b | 846 | |
4f882b2a TG |
847 | return 0; |
848 | } | |
849 | ||
a6cc569e TG |
850 | static int dhcp_lease_lost(Link *link) { |
851 | _cleanup_address_free_ Address *address = NULL; | |
852 | struct in_addr addr; | |
853 | struct in_addr netmask; | |
3e790eae | 854 | struct in_addr gateway; |
a6cc569e | 855 | unsigned prefixlen; |
ff254138 TG |
856 | int r; |
857 | ||
858 | assert(link); | |
a6cc569e | 859 | assert(link->dhcp_lease); |
ff254138 | 860 | |
14efd761 TG |
861 | log_warning_link(link, "DHCP lease lost"); |
862 | ||
e1ea665e EY |
863 | if (link->network->dhcp_routes) { |
864 | struct sd_dhcp_route *routes; | |
a2ba62c7 | 865 | int n, i; |
e1ea665e | 866 | |
a2ba62c7 LP |
867 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes); |
868 | if (n >= 0) { | |
869 | for (i = 0; i < n; i++) { | |
e1ea665e EY |
870 | _cleanup_route_free_ Route *route = NULL; |
871 | ||
28cc555d | 872 | r = route_new_dynamic(&route, RTPROT_UNSPEC); |
e1ea665e EY |
873 | if (r >= 0) { |
874 | route->family = AF_INET; | |
875 | route->in_addr.in = routes[i].gw_addr; | |
876 | route->dst_addr.in = routes[i].dst_addr; | |
877 | route->dst_prefixlen = routes[i].dst_prefixlen; | |
878 | ||
b22d8a00 | 879 | route_drop(route, link, &link_route_drop_handler); |
e1ea665e EY |
880 | } |
881 | } | |
882 | } | |
883 | } | |
884 | ||
a6cc569e TG |
885 | r = address_new_dynamic(&address); |
886 | if (r >= 0) { | |
8ddbeaa2 | 887 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); |
3e790eae | 888 | if (r >= 0) { |
e1ea665e EY |
889 | _cleanup_route_free_ Route *route_gw = NULL; |
890 | _cleanup_route_free_ Route *route = NULL; | |
891 | ||
28cc555d | 892 | r = route_new_dynamic(&route_gw, RTPROT_UNSPEC); |
8ddbeaa2 UTL |
893 | if (r >= 0) { |
894 | route_gw->family = AF_INET; | |
895 | route_gw->dst_addr.in = gateway; | |
896 | route_gw->dst_prefixlen = 32; | |
897 | route_gw->scope = RT_SCOPE_LINK; | |
898 | ||
b22d8a00 | 899 | route_drop(route_gw, link, &link_route_drop_handler); |
8ddbeaa2 | 900 | } |
3e790eae | 901 | |
28cc555d | 902 | r = route_new_dynamic(&route, RTPROT_UNSPEC); |
8ddbeaa2 UTL |
903 | if (r >= 0) { |
904 | route->family = AF_INET; | |
905 | route->in_addr.in = gateway; | |
3e790eae | 906 | |
b22d8a00 | 907 | route_drop(route, link, &link_route_drop_handler); |
8ddbeaa2 | 908 | } |
3e790eae UTL |
909 | } |
910 | ||
8ddbeaa2 UTL |
911 | sd_dhcp_lease_get_address(link->dhcp_lease, &addr); |
912 | sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); | |
44e7b949 | 913 | prefixlen = in_addr_netmask_to_prefixlen(&netmask); |
8ddbeaa2 | 914 | |
a6cc569e TG |
915 | address->family = AF_INET; |
916 | address->in_addr.in = addr; | |
917 | address->prefixlen = prefixlen; | |
ff254138 | 918 | |
b22d8a00 | 919 | address_drop(address, link, &link_address_drop_handler); |
c07aeadf | 920 | } |
eb27aeca | 921 | |
c07aeadf TG |
922 | if (link->network->dhcp_mtu) { |
923 | uint16_t mtu; | |
ff254138 | 924 | |
a6cc569e | 925 | r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu); |
c07aeadf TG |
926 | if (r >= 0 && link->original_mtu != mtu) { |
927 | r = link_set_mtu(link, link->original_mtu); | |
928 | if (r < 0) { | |
929 | log_warning_link(link, "DHCP error: could not reset MTU"); | |
930 | link_enter_failed(link); | |
931 | return r; | |
932 | } | |
ff254138 | 933 | } |
c07aeadf | 934 | } |
ff254138 | 935 | |
c07aeadf | 936 | if (link->network->dhcp_hostname) { |
216816c6 TG |
937 | const char *hostname = NULL; |
938 | ||
939 | r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); | |
940 | if (r >= 0 && hostname) { | |
b226d99b | 941 | r = link_set_hostname(link, ""); |
216816c6 | 942 | if (r < 0) |
987efa17 | 943 | log_error_link(link, "Failed to reset transient hostname"); |
216816c6 | 944 | } |
c07aeadf | 945 | } |
4f882b2a | 946 | |
a6cc569e TG |
947 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); |
948 | ||
c07aeadf TG |
949 | return 0; |
950 | } | |
4f882b2a | 951 | |
68ceb9df PF |
952 | static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { |
953 | sd_dhcp_lease *lease; | |
954 | int r; | |
955 | ||
956 | r = sd_dhcp_client_get_lease(client, &lease); | |
957 | if (r < 0) { | |
958 | log_warning_link(link, "DHCP error: no lease %s", | |
959 | strerror(-r)); | |
960 | return r; | |
961 | } | |
962 | ||
963 | sd_dhcp_lease_unref(link->dhcp_lease); | |
964 | link->dhcp_lease = lease; | |
965 | ||
966 | link_enter_set_addresses(link); | |
967 | ||
968 | return 0; | |
969 | } | |
970 | ||
c07aeadf | 971 | static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { |
a6cc569e | 972 | sd_dhcp_lease *lease; |
c07aeadf TG |
973 | struct in_addr address; |
974 | struct in_addr netmask; | |
975 | struct in_addr gateway; | |
976 | unsigned prefixlen; | |
c07aeadf | 977 | int r; |
1346b1f0 | 978 | |
c07aeadf TG |
979 | assert(client); |
980 | assert(link); | |
ff254138 | 981 | |
a6cc569e TG |
982 | r = sd_dhcp_client_get_lease(client, &lease); |
983 | if (r < 0) { | |
984 | log_warning_link(link, "DHCP error: no lease: %s", | |
985 | strerror(-r)); | |
986 | return r; | |
987 | } | |
988 | ||
989 | r = sd_dhcp_lease_get_address(lease, &address); | |
ff254138 | 990 | if (r < 0) { |
c07aeadf TG |
991 | log_warning_link(link, "DHCP error: no address: %s", |
992 | strerror(-r)); | |
993 | return r; | |
ff254138 TG |
994 | } |
995 | ||
a6cc569e | 996 | r = sd_dhcp_lease_get_netmask(lease, &netmask); |
ff254138 | 997 | if (r < 0) { |
c07aeadf TG |
998 | log_warning_link(link, "DHCP error: no netmask: %s", |
999 | strerror(-r)); | |
1000 | return r; | |
ff254138 TG |
1001 | } |
1002 | ||
44e7b949 | 1003 | prefixlen = in_addr_netmask_to_prefixlen(&netmask); |
ff254138 | 1004 | |
a6cc569e | 1005 | r = sd_dhcp_lease_get_router(lease, &gateway); |
8ddbeaa2 | 1006 | if (r < 0 && r != -ENOENT) { |
b1666580 TG |
1007 | log_warning_link(link, "DHCP error: could not get gateway: %s", |
1008 | strerror(-r)); | |
c07aeadf | 1009 | return r; |
ff254138 TG |
1010 | } |
1011 | ||
8ddbeaa2 UTL |
1012 | if (r >= 0) |
1013 | log_struct_link(LOG_INFO, link, | |
97578344 | 1014 | "MESSAGE=%-*s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", |
987efa17 | 1015 | IFNAMSIZ, |
8ddbeaa2 UTL |
1016 | link->ifname, |
1017 | ADDRESS_FMT_VAL(address), | |
1018 | prefixlen, | |
1019 | ADDRESS_FMT_VAL(gateway), | |
1020 | "ADDRESS=%u.%u.%u.%u", | |
1021 | ADDRESS_FMT_VAL(address), | |
1022 | "PREFIXLEN=%u", | |
1023 | prefixlen, | |
1024 | "GATEWAY=%u.%u.%u.%u", | |
1025 | ADDRESS_FMT_VAL(gateway), | |
1026 | NULL); | |
1027 | else | |
1028 | log_struct_link(LOG_INFO, link, | |
97578344 | 1029 | "MESSAGE=%-*s: DHCPv4 address %u.%u.%u.%u/%u", |
987efa17 | 1030 | IFNAMSIZ, |
8ddbeaa2 UTL |
1031 | link->ifname, |
1032 | ADDRESS_FMT_VAL(address), | |
1033 | prefixlen, | |
1034 | "ADDRESS=%u.%u.%u.%u", | |
1035 | ADDRESS_FMT_VAL(address), | |
1036 | "PREFIXLEN=%u", | |
1037 | prefixlen, | |
1038 | NULL); | |
c07aeadf | 1039 | |
d50cf59b TG |
1040 | link->dhcp_lease = lease; |
1041 | ||
c07aeadf TG |
1042 | if (link->network->dhcp_mtu) { |
1043 | uint16_t mtu; | |
1044 | ||
a6cc569e | 1045 | r = sd_dhcp_lease_get_mtu(lease, &mtu); |
c07aeadf TG |
1046 | if (r >= 0) { |
1047 | r = link_set_mtu(link, mtu); | |
1048 | if (r < 0) | |
1049 | log_error_link(link, "Failed to set MTU " | |
1050 | "to %" PRIu16, mtu); | |
1051 | } | |
1052 | } | |
ff254138 | 1053 | |
c07aeadf TG |
1054 | if (link->network->dhcp_hostname) { |
1055 | const char *hostname; | |
ff254138 | 1056 | |
a6cc569e | 1057 | r = sd_dhcp_lease_get_hostname(lease, &hostname); |
c07aeadf | 1058 | if (r >= 0) { |
b226d99b | 1059 | r = link_set_hostname(link, hostname); |
c07aeadf | 1060 | if (r < 0) |
987efa17 | 1061 | log_error_link(link, "Failed to set transient hostname " |
c07aeadf | 1062 | "to '%s'", hostname); |
3bef724f | 1063 | } |
c07aeadf | 1064 | } |
3bef724f | 1065 | |
c07aeadf TG |
1066 | link_enter_set_addresses(link); |
1067 | ||
1068 | return 0; | |
1069 | } | |
1070 | ||
1071 | static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { | |
1072 | Link *link = userdata; | |
aba496a5 | 1073 | int r = 0; |
c07aeadf TG |
1074 | |
1075 | assert(link); | |
1076 | assert(link->network); | |
1077 | assert(link->manager); | |
1078 | ||
370e9930 | 1079 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
c07aeadf TG |
1080 | return; |
1081 | ||
1082 | switch (event) { | |
c07aeadf TG |
1083 | case DHCP_EVENT_EXPIRED: |
1084 | case DHCP_EVENT_STOP: | |
1085 | case DHCP_EVENT_IP_CHANGE: | |
1086 | if (link->network->dhcp_critical) { | |
1087 | log_error_link(link, "DHCPv4 connection considered system critical, " | |
1088 | "ignoring request to reconfigure it."); | |
1089 | return; | |
4f882b2a | 1090 | } |
4f882b2a | 1091 | |
17256461 UTL |
1092 | if (link->dhcp_lease) { |
1093 | r = dhcp_lease_lost(link); | |
1094 | if (r < 0) { | |
1095 | link_enter_failed(link); | |
1096 | return; | |
1097 | } | |
c07aeadf | 1098 | } |
1346b1f0 | 1099 | |
c07aeadf TG |
1100 | if (event == DHCP_EVENT_IP_CHANGE) { |
1101 | r = dhcp_lease_acquired(client, link); | |
1102 | if (r < 0) { | |
1103 | link_enter_failed(link); | |
1104 | return; | |
1105 | } | |
1346b1f0 | 1106 | } |
1346b1f0 | 1107 | |
68ceb9df PF |
1108 | break; |
1109 | case DHCP_EVENT_RENEW: | |
1110 | r = dhcp_lease_renew(client, link); | |
1111 | if (r < 0) { | |
1112 | link_enter_failed(link); | |
1113 | return; | |
1114 | } | |
c07aeadf TG |
1115 | break; |
1116 | case DHCP_EVENT_IP_ACQUIRE: | |
1117 | r = dhcp_lease_acquired(client, link); | |
1118 | if (r < 0) { | |
1119 | link_enter_failed(link); | |
1120 | return; | |
1121 | } | |
1122 | break; | |
1123 | default: | |
1124 | if (event < 0) | |
b1666580 | 1125 | log_warning_link(link, "DHCP error: client failed: %s", strerror(-event)); |
c07aeadf TG |
1126 | else |
1127 | log_warning_link(link, "DHCP unknown event: %d", event); | |
c07aeadf | 1128 | break; |
ff254138 TG |
1129 | } |
1130 | ||
1131 | return; | |
1132 | } | |
1133 | ||
4138fb2c PF |
1134 | static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) { |
1135 | Link *link = userdata; | |
1136 | ||
1137 | assert(link); | |
1138 | assert(link->network); | |
1139 | assert(link->manager); | |
1140 | ||
1141 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1142 | return; | |
1143 | ||
1144 | switch(event) { | |
1145 | case DHCP6_EVENT_STOP: | |
1146 | case DHCP6_EVENT_RESEND_EXPIRE: | |
1147 | case DHCP6_EVENT_RETRANS_MAX: | |
1148 | case DHCP6_EVENT_IP_ACQUIRE: | |
1149 | log_debug_link(link, "DHCPv6 event %d", event); | |
1150 | ||
1151 | break; | |
1152 | ||
1153 | default: | |
1154 | if (event < 0) | |
1155 | log_warning_link(link, "DHCPv6 error: %s", | |
1156 | strerror(-event)); | |
1157 | else | |
1158 | log_warning_link(link, "DHCPv6 unknown event: %d", | |
1159 | event); | |
1160 | return; | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | static void icmp6_router_handler(sd_icmp6_nd *nd, int event, void *userdata) { | |
1165 | Link *link = userdata; | |
1166 | int r; | |
1167 | ||
1168 | assert(link); | |
1169 | assert(link->network); | |
1170 | assert(link->manager); | |
1171 | ||
1172 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1173 | return; | |
1174 | ||
1175 | switch(event) { | |
1176 | case ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE: | |
1177 | case ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER: | |
1178 | return; | |
1179 | ||
1180 | case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT: | |
1181 | case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED: | |
1182 | break; | |
1183 | ||
1184 | default: | |
1185 | if (event < 0) | |
1186 | log_warning_link(link, "ICMPv6 error: %s", | |
1187 | strerror(-event)); | |
1188 | else | |
1189 | log_warning_link(link, "ICMPv6 unknown event: %d", | |
1190 | event); | |
1191 | ||
1192 | return; | |
1193 | } | |
1194 | ||
1195 | if (link->dhcp6_client) | |
1196 | return; | |
1197 | ||
1198 | r = sd_dhcp6_client_new(&link->dhcp6_client); | |
1199 | if (r < 0) | |
1200 | return; | |
1201 | ||
1202 | r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0); | |
1203 | if (r < 0) { | |
1204 | link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client); | |
1205 | return; | |
1206 | } | |
1207 | ||
1208 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, &link->mac); | |
1209 | if (r < 0) { | |
1210 | link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client); | |
1211 | return; | |
1212 | } | |
1213 | ||
1214 | r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex); | |
1215 | if (r < 0) { | |
1216 | link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client); | |
1217 | return; | |
1218 | } | |
1219 | ||
1220 | r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler, | |
1221 | link); | |
1222 | if (r < 0) { | |
1223 | link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client); | |
1224 | return; | |
1225 | } | |
1226 | ||
1227 | r = sd_dhcp6_client_start(link->dhcp6_client); | |
1228 | if (r < 0) | |
1229 | link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client); | |
1230 | } | |
1231 | ||
ff254138 TG |
1232 | static int link_acquire_conf(Link *link) { |
1233 | int r; | |
1234 | ||
1235 | assert(link); | |
1236 | assert(link->network); | |
ff254138 TG |
1237 | assert(link->manager); |
1238 | assert(link->manager->event); | |
1239 | ||
5c1d3fc9 | 1240 | if (link->network->ipv4ll) { |
eb34d4af | 1241 | assert(link->ipv4ll); |
ff254138 | 1242 | |
5c1d3fc9 UTL |
1243 | log_debug_link(link, "acquiring IPv4 link-local address"); |
1244 | ||
1245 | r = sd_ipv4ll_start(link->ipv4ll); | |
124fa2c6 TG |
1246 | if (r < 0) { |
1247 | log_warning_link(link, "could not acquire IPv4 " | |
1248 | "link-local address"); | |
ff254138 | 1249 | return r; |
124fa2c6 | 1250 | } |
5c1d3fc9 UTL |
1251 | } |
1252 | ||
ed942a9e | 1253 | if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)) { |
eb34d4af | 1254 | assert(link->dhcp_client); |
ff254138 | 1255 | |
5c1d3fc9 | 1256 | log_debug_link(link, "acquiring DHCPv4 lease"); |
ab47d620 | 1257 | |
5c1d3fc9 | 1258 | r = sd_dhcp_client_start(link->dhcp_client); |
124fa2c6 TG |
1259 | if (r < 0) { |
1260 | log_warning_link(link, "could not acquire DHCPv4 " | |
1261 | "lease"); | |
5c1d3fc9 | 1262 | return r; |
124fa2c6 | 1263 | } |
5c1d3fc9 | 1264 | } |
ff254138 | 1265 | |
ed942a9e | 1266 | if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) { |
4138fb2c PF |
1267 | assert(link->icmp6_router_discovery); |
1268 | ||
1269 | log_debug_link(link, "discovering IPv6 routers"); | |
1270 | ||
1271 | r = sd_icmp6_router_solicitation_start(link->icmp6_router_discovery); | |
1272 | if (r < 0) { | |
1273 | log_warning_link(link, "could not start IPv6 router discovery"); | |
1274 | return r; | |
1275 | } | |
1276 | } | |
1277 | ||
ff254138 TG |
1278 | return 0; |
1279 | } | |
1280 | ||
bbf7c048 | 1281 | bool link_has_carrier(unsigned flags, uint8_t operstate) { |
deb2e523 TG |
1282 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1283 | ||
1284 | if (operstate == IF_OPER_UP) | |
1285 | return true; | |
1286 | ||
1287 | if (operstate == IF_OPER_UNKNOWN) | |
1288 | /* operstate may not be implemented, so fall back to flags */ | |
1289 | if ((flags & IFF_LOWER_UP) && !(flags & IFF_DORMANT)) | |
1290 | return true; | |
1291 | ||
1292 | return false; | |
1293 | } | |
1294 | ||
389cc5f7 TG |
1295 | #define FLAG_STRING(string, flag, old, new) \ |
1296 | (((old ^ new) & flag) \ | |
1297 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
1298 | : "") | |
1299 | ||
1e9be60b | 1300 | static int link_update_flags(Link *link, sd_rtnl_message *m) { |
389cc5f7 | 1301 | unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags; |
1e9be60b TG |
1302 | uint8_t operstate; |
1303 | bool carrier_gained = false, carrier_lost = false; | |
ff254138 TG |
1304 | int r; |
1305 | ||
1306 | assert(link); | |
ff254138 | 1307 | |
1e9be60b TG |
1308 | r = sd_rtnl_message_link_get_flags(m, &flags); |
1309 | if (r < 0) { | |
1310 | log_warning_link(link, "Could not get link flags"); | |
1311 | return r; | |
1312 | } | |
1313 | ||
1314 | r = sd_rtnl_message_read_u8(m, IFLA_OPERSTATE, &operstate); | |
1315 | if (r < 0) | |
1316 | /* if we got a message without operstate, take it to mean | |
1317 | the state was unchanged */ | |
e375dcde | 1318 | operstate = link->kernel_operstate; |
1e9be60b | 1319 | |
e375dcde | 1320 | if ((link->flags == flags) && (link->kernel_operstate == operstate)) |
efbc88b8 | 1321 | return 0; |
efbc88b8 | 1322 | |
389cc5f7 TG |
1323 | if (link->flags != flags) { |
1324 | log_debug_link(link, "flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", | |
1325 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), | |
1326 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
1327 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
1328 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
1329 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
1330 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
1331 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
1332 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
1333 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
1334 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
1335 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
1336 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
1337 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
1338 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
1339 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
1340 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
1341 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
1342 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
1343 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
1344 | ||
1345 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | | |
1346 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
1347 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
1348 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
1349 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
1350 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
1351 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
1352 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
1353 | ||
1354 | /* link flags are currently at most 18 bits, let's align to printing 20 */ | |
1355 | if (unknown_flags_added) | |
1356 | log_debug_link(link, "unknown link flags gained: %#.5x (ignoring)", | |
1357 | unknown_flags_added); | |
1358 | ||
1359 | if (unknown_flags_removed) | |
1360 | log_debug_link(link, "unknown link flags lost: %#.5x (ignoring)", | |
1361 | unknown_flags_removed); | |
1362 | } | |
505f8da7 | 1363 | |
e375dcde | 1364 | carrier_gained = !link_has_carrier(link->flags, link->kernel_operstate) && |
1e9be60b | 1365 | link_has_carrier(flags, operstate); |
e375dcde | 1366 | carrier_lost = link_has_carrier(link->flags, link->kernel_operstate) && |
1e9be60b TG |
1367 | !link_has_carrier(flags, operstate); |
1368 | ||
1369 | link->flags = flags; | |
e375dcde | 1370 | link->kernel_operstate = operstate; |
1e9be60b | 1371 | |
deb2e523 TG |
1372 | link_save(link); |
1373 | ||
99b4cc3e TG |
1374 | if (link->state == LINK_STATE_FAILED || |
1375 | link->state == LINK_STATE_UNMANAGED) | |
1376 | return 0; | |
1377 | ||
7cc832b9 TG |
1378 | if (carrier_gained) { |
1379 | log_info_link(link, "gained carrier"); | |
ffba6166 | 1380 | |
1e9be60b | 1381 | if (link->network) { |
ffba6166 TG |
1382 | r = link_acquire_conf(link); |
1383 | if (r < 0) { | |
ffba6166 TG |
1384 | link_enter_failed(link); |
1385 | return r; | |
ff254138 | 1386 | } |
ffba6166 | 1387 | } |
7cc832b9 TG |
1388 | } else if (carrier_lost) { |
1389 | log_info_link(link, "lost carrier"); | |
efbc88b8 | 1390 | |
1e9be60b TG |
1391 | r = link_stop_clients(link); |
1392 | if (r < 0) { | |
1393 | link_enter_failed(link); | |
1394 | return r; | |
ff254138 TG |
1395 | } |
1396 | } | |
1397 | ||
ff254138 TG |
1398 | return 0; |
1399 | } | |
1400 | ||
dd3efc09 | 1401 | static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 1402 | _cleanup_link_unref_ Link *link = userdata; |
dd3efc09 TG |
1403 | int r; |
1404 | ||
1746cf2a TG |
1405 | assert(link); |
1406 | ||
5da8149f | 1407 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1408 | return 1; |
1409 | ||
dd3efc09 | 1410 | r = sd_rtnl_message_get_errno(m); |
45ad2c13 | 1411 | if (r < 0) { |
9b86b393 TG |
1412 | /* we warn but don't fail the link, as it may |
1413 | be brought up later */ | |
76800848 | 1414 | log_struct_link(LOG_WARNING, link, |
97578344 | 1415 | "MESSAGE=%-*s: could not bring up interface: %s", |
987efa17 | 1416 | IFNAMSIZ, |
c9ccc19f TG |
1417 | link->ifname, strerror(-r), |
1418 | "ERRNO=%d", -r, | |
1419 | NULL); | |
45ad2c13 TG |
1420 | } |
1421 | ||
f882c247 TG |
1422 | return 1; |
1423 | } | |
1424 | ||
1425 | static int link_up(Link *link) { | |
cf6a8911 | 1426 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; |
f579559b TG |
1427 | int r; |
1428 | ||
f882c247 TG |
1429 | assert(link); |
1430 | assert(link->manager); | |
1431 | assert(link->manager->rtnl); | |
1432 | ||
39032b87 | 1433 | log_debug_link(link, "bringing link up"); |
449f7554 | 1434 | |
151b9b96 LP |
1435 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, |
1436 | RTM_SETLINK, link->ifindex); | |
f579559b | 1437 | if (r < 0) { |
39032b87 | 1438 | log_error_link(link, "Could not allocate RTM_SETLINK message"); |
f579559b TG |
1439 | return r; |
1440 | } | |
1441 | ||
5d4795f3 | 1442 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
fc25d7f8 | 1443 | if (r < 0) { |
3333d748 | 1444 | log_error_link(link, "Could not set link flags: %s", strerror(-r)); |
fc25d7f8 TG |
1445 | return r; |
1446 | } | |
1447 | ||
dd3efc09 | 1448 | r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL); |
f579559b | 1449 | if (r < 0) { |
3333d748 ZJS |
1450 | log_error_link(link, |
1451 | "Could not send rtnetlink message: %s", strerror(-r)); | |
f579559b TG |
1452 | return r; |
1453 | } | |
1454 | ||
b226d99b TG |
1455 | link_ref(link); |
1456 | ||
f882c247 TG |
1457 | return 0; |
1458 | } | |
1459 | ||
3f265037 | 1460 | static int link_joined(Link *link) { |
f882c247 TG |
1461 | int r; |
1462 | ||
ef1ba606 | 1463 | assert(link); |
52433f6b | 1464 | assert(link->state == LINK_STATE_ENSLAVING); |
f5be5601 | 1465 | assert(link->network); |
dd3efc09 | 1466 | |
505f8da7 TG |
1467 | if (!(link->flags & IFF_UP)) { |
1468 | r = link_up(link); | |
1469 | if (r < 0) { | |
1470 | link_enter_failed(link); | |
1471 | return r; | |
1472 | } | |
ef1ba606 | 1473 | } |
f882c247 | 1474 | |
fb6730c4 | 1475 | return link_enter_set_addresses(link); |
02b59d57 TG |
1476 | } |
1477 | ||
3f265037 | 1478 | static int netdev_join_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 1479 | _cleanup_link_unref_ Link *link = userdata; |
02b59d57 TG |
1480 | int r; |
1481 | ||
1746cf2a | 1482 | assert(link); |
370e9930 TG |
1483 | assert(IN_SET(link->state, LINK_STATE_ENSLAVING, LINK_STATE_FAILED, |
1484 | LINK_STATE_LINGER)); | |
ef1ba606 | 1485 | assert(link->network); |
02b59d57 | 1486 | |
52433f6b TG |
1487 | link->enslaving --; |
1488 | ||
5da8149f | 1489 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
1490 | return 1; |
1491 | ||
1492 | r = sd_rtnl_message_get_errno(m); | |
856f962c | 1493 | if (r < 0 && r != -EEXIST) { |
c9ccc19f | 1494 | log_struct_link(LOG_ERR, link, |
3f265037 | 1495 | "MESSAGE=%-*s: could not join netdev: %s", |
987efa17 | 1496 | IFNAMSIZ, |
c9ccc19f TG |
1497 | link->ifname, strerror(-r), |
1498 | "ERRNO=%d", -r, | |
1499 | NULL); | |
ef1ba606 TG |
1500 | link_enter_failed(link); |
1501 | return 1; | |
ba179154 TG |
1502 | } else |
1503 | log_debug_link(link, "joined netdev"); | |
02b59d57 | 1504 | |
856f962c | 1505 | if (link->enslaving <= 0) |
3f265037 | 1506 | link_joined(link); |
02b59d57 TG |
1507 | |
1508 | return 1; | |
1509 | } | |
1510 | ||
3f265037 | 1511 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 1512 | NetDev *netdev; |
672682a6 | 1513 | Iterator i; |
02b59d57 TG |
1514 | int r; |
1515 | ||
1516 | assert(link); | |
1517 | assert(link->network); | |
505f8da7 | 1518 | assert(link->state == LINK_STATE_INITIALIZING); |
02b59d57 | 1519 | |
52433f6b | 1520 | link->state = LINK_STATE_ENSLAVING; |
02b59d57 | 1521 | |
fe8db0c5 TG |
1522 | link_save(link); |
1523 | ||
7951dea2 SS |
1524 | if (!link->network->bridge && |
1525 | !link->network->bond && | |
6a0a2f86 | 1526 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 1527 | return link_joined(link); |
02b59d57 | 1528 | |
d9c67ea1 | 1529 | if (link->network->bond) { |
52433f6b | 1530 | log_struct_link(LOG_DEBUG, link, |
97578344 | 1531 | "MESSAGE=%-*s: enslaving by '%s'", |
987efa17 | 1532 | IFNAMSIZ, |
af4e9e2c | 1533 | link->ifname, link->network->bond->ifname, |
aa9f1140 | 1534 | NETDEVIF(link->network->bond), |
52433f6b | 1535 | NULL); |
449f7554 | 1536 | |
3f265037 | 1537 | r = netdev_join(link->network->bond, link, &netdev_join_handler); |
52433f6b TG |
1538 | if (r < 0) { |
1539 | log_struct_link(LOG_WARNING, link, | |
3f265037 | 1540 | "MESSAGE=%-*s: could not join netdev '%s': %s", |
987efa17 | 1541 | IFNAMSIZ, |
af4e9e2c | 1542 | link->ifname, link->network->bond->ifname, strerror(-r), |
aa9f1140 | 1543 | NETDEVIF(link->network->bond), |
52433f6b TG |
1544 | NULL); |
1545 | link_enter_failed(link); | |
1546 | return r; | |
1547 | } | |
1548 | ||
0ad6148e MO |
1549 | link->enslaving ++; |
1550 | } | |
1551 | ||
d9c67ea1 | 1552 | if (link->network->bridge) { |
0ad6148e | 1553 | log_struct_link(LOG_DEBUG, link, |
97578344 | 1554 | "MESSAGE=%-*s: enslaving by '%s'", |
987efa17 | 1555 | IFNAMSIZ, |
af4e9e2c | 1556 | link->ifname, link->network->bridge->ifname, |
aa9f1140 | 1557 | NETDEVIF(link->network->bridge), |
0ad6148e MO |
1558 | NULL); |
1559 | ||
3f265037 | 1560 | r = netdev_join(link->network->bridge, link, &netdev_join_handler); |
0ad6148e MO |
1561 | if (r < 0) { |
1562 | log_struct_link(LOG_WARNING, link, | |
3f265037 | 1563 | "MESSAGE=%-*s: could not join netdev '%s': %s", |
987efa17 | 1564 | IFNAMSIZ, |
af4e9e2c | 1565 | link->ifname, link->network->bridge->ifname, strerror(-r), |
aa9f1140 | 1566 | NETDEVIF(link->network->bridge), |
0ad6148e MO |
1567 | NULL); |
1568 | link_enter_failed(link); | |
1569 | return r; | |
1570 | } | |
1571 | ||
52433f6b TG |
1572 | link->enslaving ++; |
1573 | } | |
1574 | ||
6a0a2f86 | 1575 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 1576 | log_struct_link(LOG_DEBUG, link, |
97578344 | 1577 | "MESSAGE=%-*s: enslaving by '%s'", |
987efa17 | 1578 | IFNAMSIZ, |
6a0a2f86 | 1579 | link->ifname, netdev->ifname, NETDEVIF(netdev), NULL); |
7951dea2 | 1580 | |
6a0a2f86 | 1581 | r = netdev_join(netdev, link, &netdev_join_handler); |
7951dea2 SS |
1582 | if (r < 0) { |
1583 | log_struct_link(LOG_WARNING, link, | |
3f265037 | 1584 | "MESSAGE=%-*s: could not join netdev '%s': %s", |
987efa17 | 1585 | IFNAMSIZ, |
6a0a2f86 TG |
1586 | link->ifname, netdev->ifname, strerror(-r), |
1587 | NETDEVIF(netdev), NULL); | |
326cb406 SS |
1588 | link_enter_failed(link); |
1589 | return r; | |
1590 | } | |
1591 | ||
326cb406 SS |
1592 | link->enslaving ++; |
1593 | } | |
1594 | ||
ef1ba606 TG |
1595 | return 0; |
1596 | } | |
1597 | ||
a748b692 | 1598 | static int link_configure(Link *link) { |
02b59d57 TG |
1599 | int r; |
1600 | ||
ef1ba606 | 1601 | assert(link); |
b22d8a00 | 1602 | assert(link->network); |
505f8da7 | 1603 | assert(link->state == LINK_STATE_INITIALIZING); |
a748b692 | 1604 | |
eb34d4af | 1605 | if (link->network->ipv4ll) { |
b22d8a00 | 1606 | r = ipv4ll_configure(link); |
eb34d4af TG |
1607 | if (r < 0) |
1608 | return r; | |
1609 | } | |
1610 | ||
ed942a9e | 1611 | if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)) { |
eb34d4af TG |
1612 | r = sd_dhcp_client_new(&link->dhcp_client); |
1613 | if (r < 0) | |
1614 | return r; | |
1615 | ||
1616 | r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0); | |
1617 | if (r < 0) | |
1618 | return r; | |
1619 | ||
4bb40e81 TG |
1620 | r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac); |
1621 | if (r < 0) | |
1622 | return r; | |
1623 | ||
eb34d4af TG |
1624 | r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex); |
1625 | if (r < 0) | |
1626 | return r; | |
1627 | ||
1628 | r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp_handler, link); | |
1629 | if (r < 0) | |
1630 | return r; | |
1631 | ||
f5de5b00 TG |
1632 | r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link->network->dhcp_broadcast); |
1633 | if (r < 0) | |
1634 | return r; | |
1635 | ||
afe7fd56 TG |
1636 | if (link->mtu) { |
1637 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); | |
1638 | if (r < 0) | |
1639 | return r; | |
1640 | } | |
1641 | ||
eb34d4af | 1642 | if (link->network->dhcp_mtu) { |
ba179154 | 1643 | r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_INTERFACE_MTU); |
eb34d4af TG |
1644 | if (r < 0) |
1645 | return r; | |
1646 | } | |
4cc7a82c | 1647 | |
e1ea665e EY |
1648 | if (link->network->dhcp_routes) { |
1649 | r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_STATIC_ROUTE); | |
1650 | if (r < 0) | |
1651 | return r; | |
1652 | r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_CLASSLESS_STATIC_ROUTE); | |
1653 | if (r < 0) | |
1654 | return r; | |
1655 | } | |
4cc7a82c EY |
1656 | |
1657 | if (link->network->dhcp_sendhost) { | |
1658 | _cleanup_free_ char *hostname = gethostname_malloc(); | |
1659 | if (!hostname) | |
1660 | return -ENOMEM; | |
1661 | ||
1662 | if (!is_localhost(hostname)) { | |
1663 | r = sd_dhcp_client_set_hostname(link->dhcp_client, hostname); | |
1664 | if (r < 0) | |
1665 | return r; | |
1666 | } | |
1667 | } | |
edb85f0d SS |
1668 | |
1669 | if (link->network->dhcp_vendor_class_identifier) { | |
1670 | r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client, | |
1671 | link->network->dhcp_vendor_class_identifier); | |
1672 | if (r < 0) | |
1673 | return r; | |
1674 | } | |
eb34d4af TG |
1675 | } |
1676 | ||
dd43110f | 1677 | if (link->network->dhcp_server) { |
dd43110f TG |
1678 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
1679 | if (r < 0) | |
1680 | return r; | |
1681 | ||
1682 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
1683 | if (r < 0) | |
1684 | return r; | |
dd43110f TG |
1685 | } |
1686 | ||
ed942a9e | 1687 | if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) { |
4138fb2c PF |
1688 | r = sd_icmp6_nd_new(&link->icmp6_router_discovery); |
1689 | if (r < 0) | |
1690 | return r; | |
1691 | ||
1692 | r = sd_icmp6_nd_attach_event(link->icmp6_router_discovery, | |
1693 | NULL, 0); | |
1694 | if (r < 0) | |
1695 | return r; | |
1696 | ||
1697 | r = sd_icmp6_nd_set_mac(link->icmp6_router_discovery, | |
1698 | &link->mac); | |
1699 | if (r < 0) | |
1700 | return r; | |
1701 | ||
1702 | r = sd_icmp6_nd_set_index(link->icmp6_router_discovery, | |
1703 | link->ifindex); | |
1704 | if (r < 0) | |
1705 | return r; | |
1706 | ||
1707 | r = sd_icmp6_nd_set_callback(link->icmp6_router_discovery, | |
1708 | icmp6_router_handler, link); | |
1709 | if (r < 0) | |
1710 | return r; | |
1711 | } | |
1712 | ||
bf0308cb | 1713 | if (link_has_carrier(link->flags, link->kernel_operstate)) { |
1e9be60b TG |
1714 | r = link_acquire_conf(link); |
1715 | if (r < 0) | |
1716 | return r; | |
cc544d5f | 1717 | } |
1e9be60b | 1718 | |
3f265037 | 1719 | return link_enter_join_netdev(link); |
505f8da7 TG |
1720 | } |
1721 | ||
4f561e8e | 1722 | static int link_initialized_and_synced(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 1723 | _cleanup_link_unref_ Link *link = userdata; |
505f8da7 | 1724 | Network *network; |
505f8da7 TG |
1725 | int r; |
1726 | ||
1727 | assert(link); | |
1728 | assert(link->ifname); | |
1729 | assert(link->manager); | |
1730 | ||
1731 | if (link->state != LINK_STATE_INITIALIZING) | |
5da8149f | 1732 | return 1; |
505f8da7 | 1733 | |
4f561e8e | 1734 | log_debug_link(link, "link state is up-to-date"); |
505f8da7 | 1735 | |
4f561e8e | 1736 | r = network_get(link->manager, link->udev_device, link->ifname, &link->mac, &network); |
57bd6899 TG |
1737 | if (r == -ENOENT) { |
1738 | link_enter_unmanaged(link); | |
5da8149f | 1739 | return 1; |
57bd6899 TG |
1740 | } else if (r < 0) |
1741 | return r; | |
505f8da7 TG |
1742 | |
1743 | r = network_apply(link->manager, network, link); | |
1744 | if (r < 0) | |
1745 | return r; | |
1746 | ||
a748b692 TG |
1747 | r = link_configure(link); |
1748 | if (r < 0) | |
1749 | return r; | |
1750 | ||
5da8149f | 1751 | return 1; |
505f8da7 TG |
1752 | } |
1753 | ||
4f561e8e TG |
1754 | int link_initialized(Link *link, struct udev_device *device) { |
1755 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; | |
1756 | int r; | |
1757 | ||
1758 | assert(link); | |
1759 | assert(link->manager); | |
1760 | assert(link->manager->rtnl); | |
1761 | assert(device); | |
1762 | ||
1763 | if (link->state != LINK_STATE_INITIALIZING) | |
1764 | return 0; | |
1765 | ||
679b3605 TG |
1766 | if (link->udev_device) |
1767 | return 0; | |
1768 | ||
4f561e8e TG |
1769 | log_debug_link(link, "udev initialized link"); |
1770 | ||
1771 | link->udev_device = udev_device_ref(device); | |
1772 | ||
1773 | /* udev has initialized the link, but we don't know if we have yet processed | |
1774 | the NEWLINK messages with the latest state. Do a GETLINK, when it returns | |
1775 | we know that the pending NEWLINKs have already been processed and that we | |
1776 | are up-to-date */ | |
1777 | ||
1778 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex); | |
1779 | if (r < 0) | |
1780 | return r; | |
1781 | ||
1782 | r = sd_rtnl_call_async(link->manager->rtnl, req, link_initialized_and_synced, link, 0, NULL); | |
1783 | if (r < 0) | |
1784 | return r; | |
1785 | ||
5da8149f TG |
1786 | link_ref(link); |
1787 | ||
4f561e8e TG |
1788 | return 0; |
1789 | } | |
1790 | ||
fbbeb65a TG |
1791 | int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) { |
1792 | Manager *m = userdata; | |
1793 | Link *link = NULL; | |
1794 | uint16_t type; | |
1795 | _cleanup_address_free_ Address *address = NULL; | |
428fd0a7 | 1796 | Address *ad; |
fbbeb65a | 1797 | char buf[INET6_ADDRSTRLEN]; |
428fd0a7 | 1798 | bool address_dropped = false; |
fbbeb65a TG |
1799 | int r, ifindex; |
1800 | ||
1801 | assert(rtnl); | |
1802 | assert(message); | |
1803 | assert(m); | |
1804 | ||
1805 | r = sd_rtnl_message_get_type(message, &type); | |
1806 | if (r < 0) { | |
1807 | log_warning("rtnl: could not get message type"); | |
1808 | return 0; | |
1809 | } | |
1810 | ||
1811 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
1812 | if (r < 0 || ifindex <= 0) { | |
5ea846cc | 1813 | log_warning("rtnl: received address message without valid ifindex, ignoring"); |
fbbeb65a TG |
1814 | return 0; |
1815 | } else { | |
1816 | r = link_get(m, ifindex, &link); | |
1817 | if (r < 0 || !link) { | |
5ea846cc | 1818 | log_warning("rtnl: received address for a nonexistent link, ignoring"); |
fbbeb65a TG |
1819 | return 0; |
1820 | } | |
1821 | } | |
1822 | ||
1823 | r = address_new_dynamic(&address); | |
1824 | if (r < 0) | |
393c0c5e | 1825 | return r; |
fbbeb65a TG |
1826 | |
1827 | r = sd_rtnl_message_addr_get_family(message, &address->family); | |
1828 | if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) { | |
987efa17 | 1829 | log_warning_link(link, "rtnl: received address with invalid family, ignoring"); |
fbbeb65a TG |
1830 | return 0; |
1831 | } | |
1832 | ||
1833 | r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen); | |
1834 | if (r < 0) { | |
e375dcde TG |
1835 | log_warning_link(link, "rtnl: received address with invalid prefixlen, ignoring"); |
1836 | return 0; | |
1837 | } | |
1838 | ||
1839 | r = sd_rtnl_message_addr_get_scope(message, &address->scope); | |
1840 | if (r < 0) { | |
1841 | log_warning_link(link, "rtnl: received address with invalid scope, ignoring"); | |
fbbeb65a TG |
1842 | return 0; |
1843 | } | |
1844 | ||
1845 | switch (address->family) { | |
1846 | case AF_INET: | |
1847 | r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL, &address->in_addr.in); | |
1848 | if (r < 0) { | |
987efa17 | 1849 | log_warning_link(link, "rtnl: received address without valid address, ignoring"); |
fbbeb65a TG |
1850 | return 0; |
1851 | } | |
1852 | ||
1853 | break; | |
1854 | ||
1855 | case AF_INET6: | |
1856 | r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS, &address->in_addr.in6); | |
1857 | if (r < 0) { | |
987efa17 | 1858 | log_warning_link(link, "rtnl: received address without valid address, ignoring"); |
fbbeb65a TG |
1859 | return 0; |
1860 | } | |
1861 | ||
1862 | break; | |
1863 | ||
1864 | default: | |
1865 | assert_not_reached("invalid address family"); | |
1866 | } | |
1867 | ||
1868 | if (!inet_ntop(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN)) { | |
987efa17 | 1869 | log_warning_link(link, "could not print address"); |
fbbeb65a TG |
1870 | return 0; |
1871 | } | |
1872 | ||
428fd0a7 TG |
1873 | LIST_FOREACH(addresses, ad, link->addresses) { |
1874 | if (address_equal(ad, address)) { | |
1875 | LIST_REMOVE(addresses, link->addresses, ad); | |
1876 | ||
1877 | address_free(ad); | |
1878 | ||
1879 | address_dropped = true; | |
1880 | ||
1881 | break; | |
1882 | } | |
1883 | } | |
1884 | ||
fbbeb65a TG |
1885 | switch (type) { |
1886 | case RTM_NEWADDR: | |
428fd0a7 TG |
1887 | if (!address_dropped) |
1888 | log_debug_link(link, "added address: %s/%u", buf, | |
393c0c5e TG |
1889 | address->prefixlen); |
1890 | else | |
1891 | log_debug_link(link, "updated address: %s/%u", buf, | |
1892 | address->prefixlen); | |
fbbeb65a | 1893 | |
428fd0a7 TG |
1894 | LIST_PREPEND(addresses, link->addresses, address); |
1895 | address = NULL; | |
1896 | ||
f5602be9 TG |
1897 | link_save(link); |
1898 | ||
428fd0a7 | 1899 | break; |
fbbeb65a | 1900 | case RTM_DELADDR: |
f5602be9 | 1901 | if (address_dropped) { |
428fd0a7 | 1902 | log_debug_link(link, "removed address: %s/%u", buf, |
393c0c5e | 1903 | address->prefixlen); |
428fd0a7 | 1904 | |
f5602be9 | 1905 | link_save(link); |
393c0c5e TG |
1906 | } else |
1907 | log_warning_link(link, "removing non-existent address: %s/%u", | |
1908 | buf, address->prefixlen); | |
f5602be9 | 1909 | |
fbbeb65a TG |
1910 | break; |
1911 | default: | |
1912 | assert_not_reached("Received invalid RTNL message type"); | |
1913 | } | |
1914 | ||
1915 | return 1; | |
1916 | } | |
1917 | ||
505f8da7 TG |
1918 | int link_add(Manager *m, sd_rtnl_message *message, Link **ret) { |
1919 | Link *link; | |
fbbeb65a | 1920 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; |
505f8da7 TG |
1921 | _cleanup_udev_device_unref_ struct udev_device *device = NULL; |
1922 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; | |
1923 | int r; | |
1924 | ||
1925 | assert(m); | |
fbbeb65a | 1926 | assert(m->rtnl); |
505f8da7 TG |
1927 | assert(message); |
1928 | assert(ret); | |
1929 | ||
1930 | r = link_new(m, message, ret); | |
1931 | if (r < 0) | |
1932 | return r; | |
1933 | ||
1934 | link = *ret; | |
1935 | ||
ae06ab10 | 1936 | log_debug_link(link, "link %d added", link->ifindex); |
505f8da7 | 1937 | |
fbbeb65a TG |
1938 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, link->ifindex, 0); |
1939 | if (r < 0) | |
1940 | return r; | |
1941 | ||
1942 | r = sd_rtnl_call_async(m->rtnl, req, link_get_address_handler, link, 0, NULL); | |
1943 | if (r < 0) | |
1944 | return r; | |
1945 | ||
5da8149f TG |
1946 | link_ref(link); |
1947 | ||
505f8da7 TG |
1948 | if (detect_container(NULL) <= 0) { |
1949 | /* not in a container, udev will be around */ | |
ae06ab10 | 1950 | sprintf(ifindex_str, "n%d", link->ifindex); |
505f8da7 TG |
1951 | device = udev_device_new_from_device_id(m->udev, ifindex_str); |
1952 | if (!device) { | |
9fecce80 TG |
1953 | log_warning_link(link, "could not find udev device: %m"); |
1954 | return -errno; | |
505f8da7 TG |
1955 | } |
1956 | ||
3c4cb064 | 1957 | if (udev_device_get_is_initialized(device) <= 0) { |
505f8da7 | 1958 | /* not yet ready */ |
16cd414b | 1959 | log_debug_link(link, "udev initializing link..."); |
505f8da7 | 1960 | return 0; |
3c4cb064 | 1961 | } |
505f8da7 | 1962 | |
4f561e8e TG |
1963 | r = link_initialized(link, device); |
1964 | if (r < 0) | |
1965 | return r; | |
1966 | } else { | |
5da8149f TG |
1967 | /* we are calling a callback directly, so must take a ref */ |
1968 | link_ref(link); | |
1969 | ||
4f561e8e TG |
1970 | r = link_initialized_and_synced(m->rtnl, NULL, link); |
1971 | if (r < 0) | |
1972 | return r; | |
1973 | } | |
505f8da7 | 1974 | |
a748b692 TG |
1975 | return 0; |
1976 | } | |
1977 | ||
22936833 | 1978 | int link_update(Link *link, sd_rtnl_message *m) { |
c49b33ac | 1979 | struct ether_addr mac; |
ca4e095a | 1980 | const char *ifname; |
afe7fd56 | 1981 | uint32_t mtu; |
22936833 TG |
1982 | int r; |
1983 | ||
dd3efc09 | 1984 | assert(link); |
b8941f74 | 1985 | assert(link->ifname); |
22936833 TG |
1986 | assert(m); |
1987 | ||
7619683b TG |
1988 | if (link->state == LINK_STATE_LINGER) { |
1989 | link_ref(link); | |
1990 | log_info_link(link, "link readded"); | |
1991 | link->state = LINK_STATE_ENSLAVING; | |
1992 | } | |
1993 | ||
b8941f74 TG |
1994 | r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname); |
1995 | if (r >= 0 && !streq(ifname, link->ifname)) { | |
1996 | log_info_link(link, "renamed to %s", ifname); | |
1997 | ||
1998 | free(link->ifname); | |
1999 | link->ifname = strdup(ifname); | |
2000 | if (!link->ifname) | |
2001 | return -ENOMEM; | |
2002 | } | |
2003 | ||
afe7fd56 TG |
2004 | r = sd_rtnl_message_read_u32(m, IFLA_MTU, &mtu); |
2005 | if (r >= 0 && mtu > 0) { | |
2006 | link->mtu = mtu; | |
2007 | if (!link->original_mtu) { | |
2008 | link->original_mtu = mtu; | |
9842de0d | 2009 | log_debug_link(link, "saved original MTU: %" |
afe7fd56 TG |
2010 | PRIu32, link->original_mtu); |
2011 | } | |
2012 | ||
2013 | if (link->dhcp_client) { | |
2014 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); | |
2015 | if (r < 0) { | |
2016 | log_warning_link(link, "Could not update MTU in DHCP client: %s", | |
2017 | strerror(-r)); | |
2018 | return r; | |
2019 | } | |
2020 | } | |
9842de0d | 2021 | } |
69629de9 | 2022 | |
e9189a1f TG |
2023 | /* The kernel may broadcast NEWLINK messages without the MAC address |
2024 | set, simply ignore them. */ | |
c49b33ac | 2025 | r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 2026 | if (r >= 0) { |
20861203 | 2027 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN)) { |
c49b33ac | 2028 | |
20861203 | 2029 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN); |
c49b33ac | 2030 | |
20861203 TG |
2031 | log_debug_link(link, "MAC address: " |
2032 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", | |
2033 | mac.ether_addr_octet[0], | |
2034 | mac.ether_addr_octet[1], | |
2035 | mac.ether_addr_octet[2], | |
2036 | mac.ether_addr_octet[3], | |
2037 | mac.ether_addr_octet[4], | |
2038 | mac.ether_addr_octet[5]); | |
c49b33ac | 2039 | |
20861203 TG |
2040 | if (link->ipv4ll) { |
2041 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
2042 | if (r < 0) { | |
2043 | log_warning_link(link, "Could not update MAC " | |
2044 | "address in IPv4LL client: %s", | |
2045 | strerror(-r)); | |
2046 | return r; | |
2047 | } | |
c49b33ac | 2048 | } |
c49b33ac | 2049 | |
20861203 TG |
2050 | if (link->dhcp_client) { |
2051 | r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac); | |
2052 | if (r < 0) { | |
2053 | log_warning_link(link, "Could not update MAC " | |
2054 | "address in DHCP client: %s", | |
2055 | strerror(-r)); | |
2056 | return r; | |
2057 | } | |
c49b33ac | 2058 | } |
4138fb2c PF |
2059 | |
2060 | if (link->dhcp6_client) { | |
2061 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, | |
2062 | &link->mac); | |
2063 | if (r < 0) { | |
2064 | log_warning_link(link, "Could not update MAC address in DHCPv6 client: %s", | |
2065 | strerror(-r)); | |
2066 | return r; | |
2067 | } | |
2068 | } | |
c49b33ac | 2069 | } |
4f882b2a TG |
2070 | } |
2071 | ||
1e9be60b | 2072 | return link_update_flags(link, m); |
dd3efc09 | 2073 | } |
fe8db0c5 | 2074 | |
e375dcde TG |
2075 | static void link_update_operstate(Link *link) { |
2076 | ||
2077 | assert(link); | |
2078 | ||
2079 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
2080 | link->operstate = LINK_OPERSTATE_DORMANT; | |
2081 | else if (link_has_carrier(link->flags, link->kernel_operstate)) { | |
2082 | Address *address; | |
2083 | uint8_t scope = RT_SCOPE_NOWHERE; | |
2084 | ||
2085 | /* if we have carrier, check what addresses we have */ | |
2086 | LIST_FOREACH(addresses, address, link->addresses) { | |
2087 | if (address->scope < scope) | |
2088 | scope = address->scope; | |
2089 | } | |
2090 | ||
2091 | if (scope < RT_SCOPE_SITE) | |
2092 | /* universally accessible addresses found */ | |
2093 | link->operstate = LINK_OPERSTATE_ROUTABLE; | |
2094 | else if (scope < RT_SCOPE_HOST) | |
2095 | /* only link or site local addresses found */ | |
2096 | link->operstate = LINK_OPERSTATE_DEGRADED; | |
2097 | else | |
2098 | /* no useful addresses found */ | |
2099 | link->operstate = LINK_OPERSTATE_CARRIER; | |
2100 | } else | |
2101 | link->operstate = LINK_OPERSTATE_UNKNOWN; | |
2102 | } | |
2103 | ||
fe8db0c5 | 2104 | int link_save(Link *link) { |
68a8723c | 2105 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 2106 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 2107 | const char *admin_state, *oper_state; |
fe8db0c5 TG |
2108 | int r; |
2109 | ||
2110 | assert(link); | |
2111 | assert(link->state_file); | |
68a8723c | 2112 | assert(link->lease_file); |
bbf7c048 TG |
2113 | assert(link->manager); |
2114 | ||
e375dcde TG |
2115 | link_update_operstate(link); |
2116 | ||
bbf7c048 TG |
2117 | r = manager_save(link->manager); |
2118 | if (r < 0) | |
2119 | return r; | |
fe8db0c5 | 2120 | |
370e9930 TG |
2121 | if (link->state == LINK_STATE_LINGER) { |
2122 | unlink(link->state_file); | |
2123 | return 0; | |
2124 | } | |
2125 | ||
deb2e523 TG |
2126 | admin_state = link_state_to_string(link->state); |
2127 | assert(admin_state); | |
2128 | ||
e375dcde TG |
2129 | oper_state = link_operstate_to_string(link->operstate); |
2130 | assert(oper_state); | |
deb2e523 | 2131 | |
fe8db0c5 TG |
2132 | r = fopen_temporary(link->state_file, &f, &temp_path); |
2133 | if (r < 0) | |
c2d6bd61 | 2134 | return r; |
fe8db0c5 TG |
2135 | |
2136 | fchmod(fileno(f), 0644); | |
2137 | ||
2138 | fprintf(f, | |
2139 | "# This is private data. Do not parse.\n" | |
deb2e523 | 2140 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
2141 | "OPER_STATE=%s\n", |
2142 | admin_state, oper_state); | |
fe8db0c5 | 2143 | |
bcb7a07e | 2144 | if (link->network) { |
b0e39c82 TG |
2145 | char **address; |
2146 | ||
2147 | fputs("DNS=", f); | |
2148 | ||
2149 | if (link->network->dhcp_dns && | |
2150 | link->dhcp_lease) { | |
2151 | const struct in_addr *addresses; | |
2152 | ||
2153 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
2154 | if (r > 0) { | |
2155 | serialize_in_addrs(f, addresses, r); | |
2156 | if (link->network->dns) | |
2157 | fputs(" ", f); | |
2158 | } | |
2159 | } | |
2160 | ||
2161 | STRV_FOREACH(address, link->network->dns) | |
2162 | fprintf(f, "%s%s", *address, | |
2163 | (address + 1 ? " " : "")); | |
2164 | ||
2165 | fputs("\n", f); | |
2166 | ||
2167 | fprintf(f, "NTP="); | |
2168 | ||
2169 | if (link->network->dhcp_ntp && | |
2170 | link->dhcp_lease) { | |
2171 | const struct in_addr *addresses; | |
2172 | ||
2173 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
2174 | if (r > 0) { | |
2175 | serialize_in_addrs(f, addresses, r); | |
2176 | if (link->network->ntp) | |
2177 | fputs(" ", f); | |
2178 | } | |
2179 | } | |
2180 | ||
2181 | STRV_FOREACH(address, link->network->ntp) | |
2182 | fprintf(f, "%s%s", *address, | |
2183 | (address + 1 ? " " : "")); | |
2184 | ||
2185 | fputs("\n", f); | |
bd8f6538 TG |
2186 | |
2187 | fprintf(f, "LLMNR=%s\n", llmnr_support_to_string(link->network->llmnr)); | |
bcb7a07e | 2188 | } |
7374f9d8 | 2189 | |
fe8db0c5 | 2190 | if (link->dhcp_lease) { |
d9876a52 TG |
2191 | assert(link->network); |
2192 | ||
68a8723c | 2193 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 2194 | if (r < 0) |
c2d6bd61 | 2195 | goto fail; |
fe8db0c5 | 2196 | |
7374f9d8 | 2197 | fprintf(f, |
b0e39c82 TG |
2198 | "DHCP_LEASE=%s\n", |
2199 | link->lease_file); | |
deb2e523 | 2200 | } else |
68a8723c | 2201 | unlink(link->lease_file); |
fe8db0c5 | 2202 | |
c2d6bd61 LP |
2203 | r = fflush_and_check(f); |
2204 | if (r < 0) | |
2205 | goto fail; | |
fe8db0c5 | 2206 | |
c2d6bd61 | 2207 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 2208 | r = -errno; |
c2d6bd61 | 2209 | goto fail; |
fe8db0c5 TG |
2210 | } |
2211 | ||
c2d6bd61 | 2212 | return 0; |
fe8db0c5 | 2213 | |
c2d6bd61 LP |
2214 | fail: |
2215 | log_error_link(link, "Failed to save link data to %s: %s", link->state_file, strerror(-r)); | |
2216 | unlink(link->state_file); | |
2217 | unlink(temp_path); | |
fe8db0c5 TG |
2218 | return r; |
2219 | } | |
2220 | ||
2221 | static const char* const link_state_table[_LINK_STATE_MAX] = { | |
deb2e523 | 2222 | [LINK_STATE_INITIALIZING] = "initializing", |
fe8db0c5 TG |
2223 | [LINK_STATE_ENSLAVING] = "configuring", |
2224 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
2225 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
2226 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 2227 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 2228 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 2229 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
2230 | }; |
2231 | ||
2232 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
2233 | |
2234 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
2235 | [LINK_OPERSTATE_UNKNOWN] = "unknown", | |
2236 | [LINK_OPERSTATE_DORMANT] = "dormant", | |
2237 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
2238 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
2239 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
2240 | }; | |
2241 | ||
2242 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |