]>
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 | ||
091a364c | 22 | #include <sys/socket.h> |
bbf7c048 | 23 | #include <linux/if.h> |
3bef724f | 24 | |
fc2f9534 | 25 | #include "sd-daemon.h" |
07630cea | 26 | #include "sd-netlink.h" |
fc2f9534 | 27 | |
b5efdb8a | 28 | #include "alloc-util.h" |
a97dcc12 | 29 | #include "bus-util.h" |
07630cea | 30 | #include "conf-parser.h" |
a97dcc12 | 31 | #include "def.h" |
3ffd4af2 | 32 | #include "fd-util.h" |
0d39fa9c | 33 | #include "fileio.h" |
07630cea | 34 | #include "libudev-private.h" |
4f5f911e | 35 | #include "local-addresses.h" |
07630cea | 36 | #include "netlink-util.h" |
fc2f9534 | 37 | #include "networkd.h" |
07630cea LP |
38 | #include "path-util.h" |
39 | #include "set.h" | |
40 | #include "udev-util.h" | |
41 | #include "virt.h" | |
505f8da7 | 42 | |
be660c37 AR |
43 | /* use 8 MB for receive socket kernel queue. */ |
44 | #define RCVBUF_SIZE (8*1024*1024) | |
45 | ||
2ad8416d ZJS |
46 | const char* const network_dirs[] = { |
47 | "/etc/systemd/network", | |
48 | "/run/systemd/network", | |
49 | "/usr/lib/systemd/network", | |
eed0eee8 | 50 | #ifdef HAVE_SPLIT_USR |
2ad8416d ZJS |
51 | "/lib/systemd/network", |
52 | #endif | |
53 | NULL}; | |
54 | ||
11bf3cce LP |
55 | static int setup_default_address_pool(Manager *m) { |
56 | AddressPool *p; | |
57 | int r; | |
58 | ||
59 | assert(m); | |
60 | ||
61 | /* Add in the well-known private address ranges. */ | |
62 | ||
63 | r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7); | |
64 | if (r < 0) | |
65 | return r; | |
66 | ||
67 | r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16); | |
68 | if (r < 0) | |
69 | return r; | |
70 | ||
71 | r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12); | |
72 | if (r < 0) | |
73 | return r; | |
74 | ||
75 | r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8); | |
76 | if (r < 0) | |
77 | return r; | |
78 | ||
79 | return 0; | |
80 | } | |
81 | ||
9c0a72f9 TG |
82 | static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) { |
83 | Manager *m = userdata; | |
84 | ||
85 | assert(s); | |
86 | assert(m); | |
87 | ||
88 | m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source); | |
89 | ||
90 | manager_connect_bus(m); | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
95 | static int manager_reset_all(Manager *m) { | |
96 | Link *link; | |
97 | Iterator i; | |
98 | int r; | |
99 | ||
100 | assert(m); | |
101 | ||
102 | HASHMAP_FOREACH(link, m->links, i) { | |
103 | r = link_carrier_reset(link); | |
104 | if (r < 0) | |
2f3cf1f9 | 105 | log_link_warning_errno(link, r, "Could not reset carrier: %m"); |
9c0a72f9 TG |
106 | } |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
19070062 | 111 | static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
9c0a72f9 TG |
112 | Manager *m = userdata; |
113 | int b, r; | |
114 | ||
19070062 | 115 | assert(message); |
9c0a72f9 TG |
116 | |
117 | r = sd_bus_message_read(message, "b", &b); | |
118 | if (r < 0) { | |
119 | log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m"); | |
120 | return 0; | |
121 | } | |
122 | ||
123 | if (b) | |
124 | return 0; | |
125 | ||
126 | log_debug("Coming back from suspend, resetting all connections..."); | |
127 | ||
128 | manager_reset_all(m); | |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
133 | int manager_connect_bus(Manager *m) { | |
134 | int r; | |
135 | ||
136 | assert(m); | |
137 | ||
138 | r = sd_bus_default_system(&m->bus); | |
139 | if (r == -ENOENT) { | |
140 | /* We failed to connect? Yuck, we must be in early | |
141 | * boot. Let's try in 5s again. As soon as we have | |
142 | * kdbus we can stop doing this... */ | |
143 | ||
144 | log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m"); | |
145 | ||
146 | r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m); | |
147 | if (r < 0) | |
148 | return log_error_errno(r, "Failed to install bus reconnect time event: %m"); | |
149 | ||
150 | return 0; | |
7d6884b6 TA |
151 | } |
152 | ||
153 | if (r < 0) | |
9c0a72f9 TG |
154 | return r; |
155 | ||
9c0a72f9 TG |
156 | r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot, |
157 | "type='signal'," | |
158 | "sender='org.freedesktop.login1'," | |
159 | "interface='org.freedesktop.login1.Manager'," | |
160 | "member='PrepareForSleep'," | |
161 | "path='/org/freedesktop/login1'", | |
162 | match_prepare_for_sleep, | |
163 | m); | |
164 | if (r < 0) | |
165 | return log_error_errno(r, "Failed to add match for PrepareForSleep: %m"); | |
166 | ||
e331e246 TG |
167 | r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m); |
168 | if (r < 0) | |
169 | return log_error_errno(r, "Failed to add manager object vtable: %m"); | |
170 | ||
171 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m); | |
172 | if (r < 0) | |
173 | return log_error_errno(r, "Failed to add link object vtable: %m"); | |
174 | ||
175 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m); | |
176 | if (r < 0) | |
177 | return log_error_errno(r, "Failed to add link enumerator: %m"); | |
3175fcde TG |
178 | |
179 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m); | |
180 | if (r < 0) | |
181 | return log_error_errno(r, "Failed to add network object vtable: %m"); | |
182 | ||
183 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m); | |
184 | if (r < 0) | |
185 | return log_error_errno(r, "Failed to add network enumerator: %m"); | |
e331e246 TG |
186 | |
187 | r = sd_bus_request_name(m->bus, "org.freedesktop.network1", 0); | |
188 | if (r < 0) | |
189 | return log_error_errno(r, "Failed to register name: %m"); | |
190 | ||
191 | r = sd_bus_attach_event(m->bus, m->event, 0); | |
192 | if (r < 0) | |
193 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
194 | ||
9c0a72f9 TG |
195 | return 0; |
196 | } | |
197 | ||
5fae368b TG |
198 | static int manager_udev_process_link(Manager *m, struct udev_device *device) { |
199 | Link *link = NULL; | |
200 | int r, ifindex; | |
5544ee85 | 201 | |
5fae368b TG |
202 | assert(m); |
203 | assert(device); | |
5544ee85 | 204 | |
5fae368b TG |
205 | if (!streq_ptr(udev_device_get_action(device), "add")) |
206 | return 0; | |
5544ee85 | 207 | |
5fae368b TG |
208 | ifindex = udev_device_get_ifindex(device); |
209 | if (ifindex <= 0) { | |
2f3cf1f9 | 210 | log_debug("Ignoring udev ADD event for device with invalid ifindex"); |
5fae368b | 211 | return 0; |
5544ee85 TG |
212 | } |
213 | ||
5fae368b TG |
214 | r = link_get(m, ifindex, &link); |
215 | if (r == -ENODEV) | |
216 | return 0; | |
217 | else if (r < 0) | |
f579559b TG |
218 | return r; |
219 | ||
5fae368b | 220 | r = link_initialized(link, device); |
44de0efc LP |
221 | if (r < 0) |
222 | return r; | |
223 | ||
5fae368b TG |
224 | return 0; |
225 | } | |
be660c37 | 226 | |
5fae368b TG |
227 | static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
228 | Manager *m = userdata; | |
229 | struct udev_monitor *monitor = m->udev_monitor; | |
230 | _cleanup_udev_device_unref_ struct udev_device *device = NULL; | |
f579559b | 231 | |
5fae368b TG |
232 | device = udev_monitor_receive_device(monitor); |
233 | if (!device) | |
02b59d57 TG |
234 | return -ENOMEM; |
235 | ||
5fae368b | 236 | manager_udev_process_link(m, device); |
f579559b TG |
237 | return 0; |
238 | } | |
239 | ||
5fae368b TG |
240 | static int manager_connect_udev(Manager *m) { |
241 | int r; | |
f579559b | 242 | |
5fae368b TG |
243 | /* udev does not initialize devices inside containers, |
244 | * so we rely on them being already initialized before | |
245 | * entering the container */ | |
75f86906 | 246 | if (detect_container() > 0) |
5fae368b | 247 | return 0; |
f579559b | 248 | |
5fae368b TG |
249 | m->udev = udev_new(); |
250 | if (!m->udev) | |
251 | return -ENOMEM; | |
02b59d57 | 252 | |
5fae368b TG |
253 | m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
254 | if (!m->udev_monitor) | |
255 | return -ENOMEM; | |
02b59d57 | 256 | |
5fae368b | 257 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL); |
02b59d57 | 258 | if (r < 0) |
5fae368b | 259 | return log_error_errno(r, "Could not add udev monitor filter: %m"); |
02b59d57 | 260 | |
5fae368b TG |
261 | r = udev_monitor_enable_receiving(m->udev_monitor); |
262 | if (r < 0) { | |
263 | log_error("Could not enable udev monitor"); | |
02b59d57 | 264 | return r; |
667fcc6d | 265 | } |
505f8da7 | 266 | |
5fae368b TG |
267 | r = sd_event_add_io(m->event, |
268 | &m->udev_event_source, | |
269 | udev_monitor_get_fd(m->udev_monitor), | |
270 | EPOLLIN, manager_dispatch_link_udev, | |
271 | m); | |
272 | if (r < 0) | |
667fcc6d | 273 | return r; |
505f8da7 | 274 | |
5fae368b | 275 | r = sd_event_source_set_description(m->udev_event_source, "networkd-udev"); |
505f8da7 TG |
276 | if (r < 0) |
277 | return r; | |
11a7f229 | 278 | |
505f8da7 TG |
279 | return 0; |
280 | } | |
f579559b | 281 | |
1c8e710c TG |
282 | int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
283 | Manager *m = userdata; | |
284 | Link *link = NULL; | |
285 | uint16_t type; | |
286 | uint32_t ifindex, priority = 0; | |
287 | unsigned char protocol, scope, tos, table; | |
288 | int family; | |
289 | unsigned char dst_prefixlen, src_prefixlen; | |
290 | union in_addr_union dst = {}, gw = {}, src = {}, prefsrc = {}; | |
291 | Route *route = NULL; | |
292 | int r; | |
293 | ||
294 | assert(rtnl); | |
295 | assert(message); | |
296 | assert(m); | |
297 | ||
298 | if (sd_netlink_message_is_error(message)) { | |
299 | r = sd_netlink_message_get_errno(message); | |
300 | if (r < 0) | |
301 | log_warning_errno(r, "rtnl: failed to receive route: %m"); | |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
306 | r = sd_netlink_message_get_type(message, &type); | |
307 | if (r < 0) { | |
308 | log_warning_errno(r, "rtnl: could not get message type: %m"); | |
309 | return 0; | |
310 | } else if (type != RTM_NEWROUTE && type != RTM_DELROUTE) { | |
311 | log_warning("rtnl: received unexpected message type when processing route"); | |
312 | return 0; | |
313 | } | |
314 | ||
315 | r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex); | |
316 | if (r == -ENODATA) { | |
317 | log_debug("rtnl: received route without ifindex, ignoring"); | |
318 | return 0; | |
319 | } else if (r < 0) { | |
320 | log_warning_errno(r, "rtnl: could not get ifindex from route, ignoring: %m"); | |
321 | return 0; | |
322 | } else if (ifindex <= 0) { | |
323 | log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex); | |
324 | return 0; | |
325 | } else { | |
326 | r = link_get(m, ifindex, &link); | |
327 | if (r < 0 || !link) { | |
328 | /* when enumerating we might be out of sync, but we will | |
329 | * get the route again, so just ignore it */ | |
330 | if (!m->enumerating) | |
331 | log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex); | |
332 | return 0; | |
333 | } | |
334 | } | |
335 | ||
336 | r = sd_rtnl_message_route_get_family(message, &family); | |
337 | if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) { | |
338 | log_link_warning(link, "rtnl: received address with invalid family, ignoring."); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | r = sd_rtnl_message_route_get_protocol(message, &protocol); | |
343 | if (r < 0) { | |
344 | log_warning_errno(r, "rtnl: could not get route protocol: %m"); | |
345 | return 0; | |
346 | } | |
347 | ||
348 | switch (family) { | |
349 | case AF_INET: | |
350 | r = sd_netlink_message_read_in_addr(message, RTA_DST, &dst.in); | |
351 | if (r < 0 && r != -ENODATA) { | |
352 | log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m"); | |
353 | return 0; | |
354 | } | |
355 | ||
356 | r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &gw.in); | |
357 | if (r < 0 && r != -ENODATA) { | |
358 | log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m"); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | r = sd_netlink_message_read_in_addr(message, RTA_SRC, &src.in); | |
363 | if (r < 0 && r != -ENODATA) { | |
364 | log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m"); | |
365 | return 0; | |
366 | } | |
367 | ||
368 | r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &prefsrc.in); | |
369 | if (r < 0 && r != -ENODATA) { | |
370 | log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m"); | |
371 | return 0; | |
372 | } | |
373 | ||
374 | break; | |
375 | ||
376 | case AF_INET6: | |
377 | r = sd_netlink_message_read_in6_addr(message, RTA_DST, &dst.in6); | |
378 | if (r < 0 && r != -ENODATA) { | |
379 | log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m"); | |
380 | return 0; | |
381 | } | |
382 | ||
383 | r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &gw.in6); | |
384 | if (r < 0 && r != -ENODATA) { | |
385 | log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m"); | |
386 | return 0; | |
387 | } | |
388 | ||
389 | r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &src.in6); | |
390 | if (r < 0 && r != -ENODATA) { | |
391 | log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m"); | |
392 | return 0; | |
393 | } | |
394 | ||
395 | r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &prefsrc.in6); | |
396 | if (r < 0 && r != -ENODATA) { | |
397 | log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m"); | |
398 | return 0; | |
399 | } | |
400 | ||
401 | break; | |
402 | ||
403 | default: | |
404 | log_link_debug(link, "rtnl: ignoring unsupported address family: %d", family); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | r = sd_rtnl_message_route_get_dst_prefixlen(message, &dst_prefixlen); | |
409 | if (r < 0) { | |
410 | log_link_warning_errno(link, r, "rtnl: received route with invalid destination prefixlen, ignoring: %m"); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | r = sd_rtnl_message_route_get_src_prefixlen(message, &src_prefixlen); | |
415 | if (r < 0) { | |
416 | log_link_warning_errno(link, r, "rtnl: received route with invalid source prefixlen, ignoring: %m"); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | r = sd_rtnl_message_route_get_scope(message, &scope); | |
421 | if (r < 0) { | |
422 | log_link_warning_errno(link, r, "rtnl: received route with invalid scope, ignoring: %m"); | |
423 | return 0; | |
424 | } | |
425 | ||
426 | r = sd_rtnl_message_route_get_tos(message, &tos); | |
427 | if (r < 0) { | |
428 | log_link_warning_errno(link, r, "rtnl: received route with invalid tos, ignoring: %m"); | |
429 | return 0; | |
430 | } | |
431 | ||
432 | r = sd_rtnl_message_route_get_table(message, &table); | |
433 | if (r < 0) { | |
434 | log_link_warning_errno(link, r, "rtnl: received route with invalid table, ignoring: %m"); | |
435 | return 0; | |
436 | } | |
437 | ||
438 | r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &priority); | |
439 | if (r < 0 && r != -ENODATA) { | |
440 | log_link_warning_errno(link, r, "rtnl: received route with invalid priority, ignoring: %m"); | |
441 | return 0; | |
442 | } | |
443 | ||
444 | route_get(link, family, &dst, dst_prefixlen, tos, priority, table, &route); | |
445 | ||
446 | switch (type) { | |
447 | case RTM_NEWROUTE: | |
448 | if (!route) { | |
449 | /* A route appeared that we did not request */ | |
450 | r = route_add_foreign(link, family, &dst, dst_prefixlen, tos, priority, table, &route); | |
451 | if (r < 0) | |
452 | return 0; | |
453 | } | |
454 | ||
455 | route_update(route, &src, src_prefixlen, &gw, &prefsrc, scope, protocol); | |
456 | ||
457 | break; | |
458 | ||
459 | case RTM_DELROUTE: | |
460 | ||
461 | if (route) | |
462 | route_drop(route); | |
463 | ||
464 | break; | |
465 | default: | |
466 | assert_not_reached("Received invalid RTNL message type"); | |
467 | } | |
468 | ||
469 | return 1; | |
470 | } | |
471 | ||
200a0868 TG |
472 | int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
473 | Manager *m = userdata; | |
474 | Link *link = NULL; | |
475 | uint16_t type; | |
200a0868 | 476 | unsigned char flags; |
054f0db4 TG |
477 | int family; |
478 | unsigned char prefixlen; | |
479 | unsigned char scope; | |
480 | union in_addr_union in_addr; | |
481 | struct ifa_cacheinfo cinfo; | |
482 | Address *address = NULL; | |
200a0868 TG |
483 | char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX]; |
484 | const char *valid_str = NULL; | |
485 | int r, ifindex; | |
486 | ||
487 | assert(rtnl); | |
488 | assert(message); | |
489 | assert(m); | |
490 | ||
491 | if (sd_netlink_message_is_error(message)) { | |
492 | r = sd_netlink_message_get_errno(message); | |
493 | if (r < 0) | |
494 | log_warning_errno(r, "rtnl: failed to receive address: %m"); | |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
499 | r = sd_netlink_message_get_type(message, &type); | |
500 | if (r < 0) { | |
501 | log_warning_errno(r, "rtnl: could not get message type: %m"); | |
502 | return 0; | |
503 | } else if (type != RTM_NEWADDR && type != RTM_DELADDR) { | |
504 | log_warning("rtnl: received unexpected message type when processing address"); | |
505 | return 0; | |
506 | } | |
507 | ||
508 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
509 | if (r < 0) { | |
510 | log_warning_errno(r, "rtnl: could not get ifindex from address: %m"); | |
511 | return 0; | |
512 | } else if (ifindex <= 0) { | |
513 | log_warning("rtnl: received address message with invalid ifindex: %d", ifindex); | |
514 | return 0; | |
515 | } else { | |
516 | r = link_get(m, ifindex, &link); | |
517 | if (r < 0 || !link) { | |
518 | /* when enumerating we might be out of sync, but we will | |
519 | * get the address again, so just ignore it */ | |
520 | if (!m->enumerating) | |
521 | log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex); | |
522 | return 0; | |
523 | } | |
524 | } | |
525 | ||
054f0db4 TG |
526 | r = sd_rtnl_message_addr_get_family(message, &family); |
527 | if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) { | |
200a0868 TG |
528 | log_link_warning(link, "rtnl: received address with invalid family, ignoring."); |
529 | return 0; | |
530 | } | |
531 | ||
054f0db4 | 532 | r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen); |
200a0868 TG |
533 | if (r < 0) { |
534 | log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m"); | |
535 | return 0; | |
536 | } | |
537 | ||
054f0db4 | 538 | r = sd_rtnl_message_addr_get_scope(message, &scope); |
200a0868 TG |
539 | if (r < 0) { |
540 | log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m"); | |
541 | return 0; | |
542 | } | |
543 | ||
544 | r = sd_rtnl_message_addr_get_flags(message, &flags); | |
545 | if (r < 0) { | |
546 | log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m"); | |
547 | return 0; | |
548 | } | |
200a0868 | 549 | |
054f0db4 | 550 | switch (family) { |
200a0868 | 551 | case AF_INET: |
054f0db4 | 552 | r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in); |
200a0868 TG |
553 | if (r < 0) { |
554 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); | |
555 | return 0; | |
556 | } | |
557 | ||
558 | break; | |
559 | ||
560 | case AF_INET6: | |
054f0db4 | 561 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6); |
200a0868 TG |
562 | if (r < 0) { |
563 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); | |
564 | return 0; | |
565 | } | |
566 | ||
567 | break; | |
568 | ||
569 | default: | |
1c8e710c | 570 | log_link_debug(link, "rtnl: ignoring unsupported address family: %d", family); |
200a0868 TG |
571 | } |
572 | ||
054f0db4 | 573 | if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) { |
200a0868 TG |
574 | log_link_warning(link, "Could not print address"); |
575 | return 0; | |
576 | } | |
577 | ||
054f0db4 | 578 | r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo); |
200a0868 | 579 | if (r >= 0) { |
4058d339 | 580 | if (cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME) |
200a0868 | 581 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, |
054f0db4 | 582 | cinfo.ifa_valid * USEC_PER_SEC, |
200a0868 TG |
583 | USEC_PER_SEC); |
584 | } | |
585 | ||
054f0db4 | 586 | address_get(link, family, &in_addr, prefixlen, &address); |
200a0868 TG |
587 | |
588 | switch (type) { | |
589 | case RTM_NEWADDR: | |
36c32f61 | 590 | if (address) |
4058d339 TG |
591 | log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen, |
592 | valid_str ? "for " : "forever", valid_str ?: ""); | |
36c32f61 | 593 | else { |
adda1ed9 TG |
594 | /* An address appeared that we did not request */ |
595 | r = address_add_foreign(link, family, &in_addr, prefixlen, &address); | |
cf1d700d | 596 | if (r < 0) { |
054f0db4 | 597 | log_link_warning_errno(link, r, "Failed to add address %s/%u: %m", buf, prefixlen); |
cf1d700d TG |
598 | return 0; |
599 | } else | |
4058d339 TG |
600 | log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen, |
601 | valid_str ? "for " : "forever", valid_str ?: ""); | |
200a0868 TG |
602 | } |
603 | ||
58fda79c | 604 | address_update(address, flags, scope, &cinfo); |
36c32f61 | 605 | |
200a0868 TG |
606 | break; |
607 | ||
608 | case RTM_DELADDR: | |
609 | ||
054f0db4 | 610 | if (address) { |
4058d339 TG |
611 | log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen, |
612 | valid_str ? "for " : "forever", valid_str ?: ""); | |
91b5f997 | 613 | address_drop(address); |
200a0868 | 614 | } else |
4058d339 TG |
615 | log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s)", buf, prefixlen, |
616 | valid_str ? "for " : "forever", valid_str ?: ""); | |
200a0868 TG |
617 | |
618 | break; | |
619 | default: | |
620 | assert_not_reached("Received invalid RTNL message type"); | |
621 | } | |
622 | ||
623 | return 1; | |
624 | } | |
625 | ||
1c4baffc | 626 | static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
505f8da7 TG |
627 | Manager *m = userdata; |
628 | Link *link = NULL; | |
4d473d5d | 629 | NetDev *netdev = NULL; |
f2236469 | 630 | uint16_t type; |
ca4e095a | 631 | const char *name; |
505f8da7 | 632 | int r, ifindex; |
f579559b | 633 | |
505f8da7 TG |
634 | assert(rtnl); |
635 | assert(message); | |
f579559b TG |
636 | assert(m); |
637 | ||
1c4baffc TG |
638 | if (sd_netlink_message_is_error(message)) { |
639 | r = sd_netlink_message_get_errno(message); | |
45af44d4 | 640 | if (r < 0) |
2f3cf1f9 | 641 | log_warning_errno(r, "rtnl: Could not receive link: %m"); |
45af44d4 TG |
642 | |
643 | return 0; | |
644 | } | |
645 | ||
1c4baffc | 646 | r = sd_netlink_message_get_type(message, &type); |
f2236469 | 647 | if (r < 0) { |
2f3cf1f9 | 648 | log_warning_errno(r, "rtnl: Could not get message type: %m"); |
f2236469 | 649 | return 0; |
cdfee943 | 650 | } else if (type != RTM_NEWLINK && type != RTM_DELLINK) { |
2f3cf1f9 | 651 | log_warning("rtnl: Received unexpected message type when processing link"); |
cdfee943 | 652 | return 0; |
f2236469 TG |
653 | } |
654 | ||
505f8da7 | 655 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
45af44d4 | 656 | if (r < 0) { |
2f3cf1f9 | 657 | log_warning_errno(r, "rtnl: Could not get ifindex from link: %m"); |
45af44d4 TG |
658 | return 0; |
659 | } else if (ifindex <= 0) { | |
660 | log_warning("rtnl: received link message with invalid ifindex: %d", ifindex); | |
505f8da7 | 661 | return 0; |
4d473d5d TG |
662 | } else |
663 | link_get(m, ifindex, &link); | |
f579559b | 664 | |
1c4baffc | 665 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name); |
45af44d4 | 666 | if (r < 0) { |
2f3cf1f9 | 667 | log_warning_errno(r, "rtnl: Received link message without ifname: %m"); |
4d473d5d TG |
668 | return 0; |
669 | } else | |
f2236469 | 670 | netdev_get(m, name, &netdev); |
4d473d5d TG |
671 | |
672 | switch (type) { | |
673 | case RTM_NEWLINK: | |
674 | if (!link) { | |
675 | /* link is new, so add it */ | |
676 | r = link_add(m, message, &link); | |
677 | if (r < 0) { | |
2f3cf1f9 | 678 | log_warning_errno(r, "Could not add new link: %m"); |
4d473d5d TG |
679 | return 0; |
680 | } | |
681 | } | |
682 | ||
683 | if (netdev) { | |
684 | /* netdev exists, so make sure the ifindex matches */ | |
505f8da7 TG |
685 | r = netdev_set_ifindex(netdev, message); |
686 | if (r < 0) { | |
2f3cf1f9 | 687 | log_warning_errno(r, "Could not set ifindex on netdev: %m"); |
505f8da7 TG |
688 | return 0; |
689 | } | |
690 | } | |
e1202047 | 691 | |
f2236469 TG |
692 | r = link_update(link, message); |
693 | if (r < 0) | |
694 | return 0; | |
4d473d5d TG |
695 | |
696 | break; | |
697 | ||
698 | case RTM_DELLINK: | |
699 | link_drop(link); | |
700 | netdev_drop(netdev); | |
701 | ||
702 | break; | |
703 | ||
704 | default: | |
705 | assert_not_reached("Received invalid RTNL message type."); | |
f2236469 | 706 | } |
505f8da7 TG |
707 | |
708 | return 1; | |
709 | } | |
710 | ||
5fae368b TG |
711 | static int systemd_netlink_fd(void) { |
712 | int n, fd, rtnl_fd = -EINVAL; | |
713 | ||
714 | n = sd_listen_fds(true); | |
715 | if (n <= 0) | |
716 | return -EINVAL; | |
717 | ||
718 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
719 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { | |
720 | if (rtnl_fd >= 0) | |
721 | return -EINVAL; | |
722 | ||
723 | rtnl_fd = fd; | |
724 | } | |
725 | } | |
726 | ||
727 | return rtnl_fd; | |
728 | } | |
729 | ||
730 | static int manager_connect_rtnl(Manager *m) { | |
731 | int fd, r; | |
505f8da7 TG |
732 | |
733 | assert(m); | |
505f8da7 | 734 | |
5fae368b TG |
735 | fd = systemd_netlink_fd(); |
736 | if (fd < 0) | |
1c4baffc | 737 | r = sd_netlink_open(&m->rtnl); |
5fae368b | 738 | else |
1c4baffc | 739 | r = sd_netlink_open_fd(&m->rtnl, fd); |
505f8da7 TG |
740 | if (r < 0) |
741 | return r; | |
742 | ||
1c4baffc | 743 | r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE); |
f579559b | 744 | if (r < 0) |
bf5332d2 | 745 | return r; |
f579559b | 746 | |
1c4baffc | 747 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
505f8da7 TG |
748 | if (r < 0) |
749 | return r; | |
f579559b | 750 | |
1c4baffc | 751 | r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
752 | if (r < 0) |
753 | return r; | |
505f8da7 | 754 | |
1c4baffc | 755 | r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
756 | if (r < 0) |
757 | return r; | |
45af44d4 | 758 | |
200a0868 | 759 | r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
760 | if (r < 0) |
761 | return r; | |
762 | ||
200a0868 | 763 | r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
764 | if (r < 0) |
765 | return r; | |
766 | ||
1c8e710c TG |
767 | r = sd_netlink_add_match(m->rtnl, RTM_NEWROUTE, &manager_rtnl_process_route, m); |
768 | if (r < 0) | |
769 | return r; | |
770 | ||
771 | r = sd_netlink_add_match(m->rtnl, RTM_DELROUTE, &manager_rtnl_process_route, m); | |
772 | if (r < 0) | |
773 | return r; | |
774 | ||
5fae368b | 775 | return 0; |
45af44d4 | 776 | } |
505f8da7 | 777 | |
84de38c5 TG |
778 | static int set_put_in_addr(Set *s, const struct in_addr *address) { |
779 | char *p; | |
780 | int r; | |
781 | ||
782 | assert(s); | |
783 | ||
784 | r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p); | |
785 | if (r < 0) | |
786 | return r; | |
787 | ||
788 | r = set_consume(s, p); | |
789 | if (r == -EEXIST) | |
790 | return 0; | |
791 | ||
792 | return r; | |
793 | } | |
794 | ||
795 | static int set_put_in_addrv(Set *s, const struct in_addr *addresses, int n) { | |
796 | int r, i, c = 0; | |
797 | ||
798 | assert(s); | |
799 | assert(n <= 0 || addresses); | |
800 | ||
801 | for (i = 0; i < n; i++) { | |
802 | r = set_put_in_addr(s, addresses+i); | |
803 | if (r < 0) | |
804 | return r; | |
805 | ||
806 | c += r; | |
807 | } | |
808 | ||
809 | return c; | |
810 | } | |
811 | ||
812 | static void print_string_set(FILE *f, const char *field, Set *s) { | |
813 | bool space = false; | |
814 | Iterator i; | |
815 | char *p; | |
816 | ||
817 | if (set_isempty(s)) | |
818 | return; | |
819 | ||
820 | fputs(field, f); | |
821 | ||
822 | SET_FOREACH(p, s, i) { | |
823 | if (space) | |
824 | fputc(' ', f); | |
825 | fputs(p, f); | |
826 | space = true; | |
827 | } | |
828 | fputc('\n', f); | |
829 | } | |
830 | ||
831 | static int manager_save(Manager *m) { | |
832 | _cleanup_set_free_free_ Set *dns = NULL, *ntp = NULL, *domains = NULL; | |
833 | Link *link; | |
834 | Iterator i; | |
835 | _cleanup_free_ char *temp_path = NULL; | |
836 | _cleanup_fclose_ FILE *f = NULL; | |
837 | LinkOperationalState operstate = LINK_OPERSTATE_OFF; | |
838 | const char *operstate_str; | |
839 | int r; | |
840 | ||
841 | assert(m); | |
842 | assert(m->state_file); | |
843 | ||
844 | /* We add all NTP and DNS server to a set, to filter out duplicates */ | |
845 | dns = set_new(&string_hash_ops); | |
846 | if (!dns) | |
847 | return -ENOMEM; | |
848 | ||
849 | ntp = set_new(&string_hash_ops); | |
850 | if (!ntp) | |
851 | return -ENOMEM; | |
852 | ||
853 | domains = set_new(&string_hash_ops); | |
854 | if (!domains) | |
855 | return -ENOMEM; | |
856 | ||
857 | HASHMAP_FOREACH(link, m->links, i) { | |
858 | if (link->flags & IFF_LOOPBACK) | |
859 | continue; | |
860 | ||
861 | if (link->operstate > operstate) | |
862 | operstate = link->operstate; | |
863 | ||
864 | if (!link->network) | |
865 | continue; | |
866 | ||
867 | /* First add the static configured entries */ | |
868 | r = set_put_strdupv(dns, link->network->dns); | |
869 | if (r < 0) | |
870 | return r; | |
871 | ||
872 | r = set_put_strdupv(ntp, link->network->ntp); | |
873 | if (r < 0) | |
874 | return r; | |
875 | ||
876 | r = set_put_strdupv(domains, link->network->domains); | |
877 | if (r < 0) | |
878 | return r; | |
879 | ||
880 | if (!link->dhcp_lease) | |
881 | continue; | |
882 | ||
883 | /* Secondly, add the entries acquired via DHCP */ | |
884 | if (link->network->dhcp_dns) { | |
885 | const struct in_addr *addresses; | |
886 | ||
887 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
888 | if (r > 0) { | |
889 | r = set_put_in_addrv(dns, addresses, r); | |
890 | if (r < 0) | |
891 | return r; | |
892 | } else if (r < 0 && r != -ENODATA) | |
893 | return r; | |
894 | } | |
895 | ||
896 | if (link->network->dhcp_ntp) { | |
897 | const struct in_addr *addresses; | |
898 | ||
899 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
900 | if (r > 0) { | |
901 | r = set_put_in_addrv(ntp, addresses, r); | |
902 | if (r < 0) | |
903 | return r; | |
904 | } else if (r < 0 && r != -ENODATA) | |
905 | return r; | |
906 | } | |
907 | ||
908 | if (link->network->dhcp_domains) { | |
909 | const char *domainname; | |
910 | ||
911 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); | |
912 | if (r >= 0) { | |
913 | r = set_put_strdup(domains, domainname); | |
914 | if (r < 0) | |
915 | return r; | |
916 | } else if (r != -ENODATA) | |
917 | return r; | |
918 | } | |
919 | } | |
920 | ||
921 | operstate_str = link_operstate_to_string(operstate); | |
922 | assert(operstate_str); | |
923 | ||
924 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
925 | if (r < 0) | |
926 | return r; | |
927 | ||
928 | fchmod(fileno(f), 0644); | |
929 | ||
930 | fprintf(f, | |
931 | "# This is private data. Do not parse.\n" | |
932 | "OPER_STATE=%s\n", operstate_str); | |
933 | ||
934 | print_string_set(f, "DNS=", dns); | |
935 | print_string_set(f, "NTP=", ntp); | |
936 | print_string_set(f, "DOMAINS=", domains); | |
937 | ||
938 | r = fflush_and_check(f); | |
939 | if (r < 0) | |
940 | goto fail; | |
941 | ||
942 | if (rename(temp_path, m->state_file) < 0) { | |
943 | r = -errno; | |
944 | goto fail; | |
945 | } | |
946 | ||
947 | if (m->operational_state != operstate) { | |
948 | m->operational_state = operstate; | |
949 | r = manager_send_changed(m, "OperationalState", NULL); | |
950 | if (r < 0) | |
951 | log_error_errno(r, "Could not emit changed OperationalState: %m"); | |
952 | } | |
953 | ||
954 | m->dirty = false; | |
955 | ||
956 | return 0; | |
957 | ||
958 | fail: | |
959 | (void) unlink(m->state_file); | |
960 | (void) unlink(temp_path); | |
961 | ||
962 | return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file); | |
963 | } | |
964 | ||
965 | static int manager_dirty_handler(sd_event_source *s, void *userdata) { | |
966 | Manager *m = userdata; | |
967 | Link *link; | |
968 | Iterator i; | |
969 | int r; | |
970 | ||
971 | assert(m); | |
972 | ||
973 | if (m->dirty) | |
974 | manager_save(m); | |
975 | ||
976 | SET_FOREACH(link, m->dirty_links, i) { | |
977 | r = link_save(link); | |
978 | if (r >= 0) | |
979 | link_clean(link); | |
980 | } | |
981 | ||
982 | return 1; | |
983 | } | |
984 | ||
5fae368b TG |
985 | int manager_new(Manager **ret) { |
986 | _cleanup_manager_free_ Manager *m = NULL; | |
45af44d4 | 987 | int r; |
f579559b | 988 | |
5fae368b TG |
989 | m = new0(Manager, 1); |
990 | if (!m) | |
991 | return -ENOMEM; | |
45af44d4 | 992 | |
5fae368b TG |
993 | m->state_file = strdup("/run/systemd/netif/state"); |
994 | if (!m->state_file) | |
995 | return -ENOMEM; | |
996 | ||
997 | r = sd_event_default(&m->event); | |
45af44d4 TG |
998 | if (r < 0) |
999 | return r; | |
1000 | ||
5fae368b TG |
1001 | sd_event_set_watchdog(m->event, true); |
1002 | ||
1003 | sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); | |
1004 | sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
1005 | ||
84de38c5 TG |
1006 | r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m); |
1007 | if (r < 0) | |
1008 | return r; | |
1009 | ||
5fae368b | 1010 | r = manager_connect_rtnl(m); |
45af44d4 TG |
1011 | if (r < 0) |
1012 | return r; | |
1013 | ||
5fae368b TG |
1014 | r = manager_connect_udev(m); |
1015 | if (r < 0) | |
1016 | return r; | |
45af44d4 | 1017 | |
5fae368b TG |
1018 | m->netdevs = hashmap_new(&string_hash_ops); |
1019 | if (!m->netdevs) | |
1020 | return -ENOMEM; | |
f579559b | 1021 | |
5fae368b | 1022 | LIST_HEAD_INIT(m->networks); |
f579559b | 1023 | |
5fae368b TG |
1024 | r = setup_default_address_pool(m); |
1025 | if (r < 0) | |
1026 | return r; | |
f579559b | 1027 | |
5fae368b TG |
1028 | *ret = m; |
1029 | m = NULL; | |
f579559b | 1030 | |
f579559b TG |
1031 | return 0; |
1032 | } | |
1033 | ||
5fae368b TG |
1034 | void manager_free(Manager *m) { |
1035 | Network *network; | |
1036 | NetDev *netdev; | |
1037 | Link *link; | |
1038 | AddressPool *pool; | |
f579559b | 1039 | |
5fae368b TG |
1040 | if (!m) |
1041 | return; | |
505f8da7 | 1042 | |
5fae368b | 1043 | free(m->state_file); |
505f8da7 | 1044 | |
5fae368b TG |
1045 | while ((link = hashmap_first(m->links))) |
1046 | link_unref(link); | |
1047 | hashmap_free(m->links); | |
f579559b | 1048 | |
5fae368b TG |
1049 | while ((network = m->networks)) |
1050 | network_free(network); | |
1051 | ||
dbffab87 TG |
1052 | hashmap_free(m->networks_by_name); |
1053 | ||
5fae368b TG |
1054 | while ((netdev = hashmap_first(m->netdevs))) |
1055 | netdev_unref(netdev); | |
1056 | hashmap_free(m->netdevs); | |
1057 | ||
1058 | while ((pool = m->address_pools)) | |
1059 | address_pool_free(pool); | |
1060 | ||
1c4baffc | 1061 | sd_netlink_unref(m->rtnl); |
2f5b4a77 | 1062 | sd_event_unref(m->event); |
5fae368b | 1063 | |
7d20d375 TG |
1064 | sd_event_source_unref(m->udev_event_source); |
1065 | udev_monitor_unref(m->udev_monitor); | |
1066 | udev_unref(m->udev); | |
1067 | ||
1068 | sd_bus_unref(m->bus); | |
1069 | sd_bus_slot_unref(m->prepare_for_sleep_slot); | |
1070 | sd_event_source_unref(m->bus_retry_event_source); | |
1071 | ||
5fae368b TG |
1072 | free(m); |
1073 | } | |
1074 | ||
a97dcc12 TG |
1075 | static bool manager_check_idle(void *userdata) { |
1076 | Manager *m = userdata; | |
1077 | Link *link; | |
1078 | Iterator i; | |
1079 | ||
1080 | assert(m); | |
1081 | ||
1082 | HASHMAP_FOREACH(link, m->links, i) { | |
1083 | /* we are not woken on udev activity, so let's just wait for the | |
1084 | * pending udev event */ | |
1085 | if (link->state == LINK_STATE_PENDING) | |
1086 | return false; | |
1087 | ||
1088 | if (!link->network) | |
1089 | continue; | |
1090 | ||
1091 | /* we are not woken on netork activity, so let's stay around */ | |
1092 | if (link_lldp_enabled(link) || | |
1093 | link_ipv4ll_enabled(link) || | |
1094 | link_dhcp4_server_enabled(link) || | |
1095 | link_dhcp4_enabled(link) || | |
f5a8c43f TG |
1096 | link_dhcp6_enabled(link) || |
1097 | link_ipv6_accept_ra_enabled(link)) | |
a97dcc12 TG |
1098 | return false; |
1099 | } | |
1100 | ||
1101 | return true; | |
1102 | } | |
1103 | ||
1104 | int manager_run(Manager *m) { | |
84de38c5 TG |
1105 | Link *link; |
1106 | Iterator i; | |
1107 | ||
a97dcc12 TG |
1108 | assert(m); |
1109 | ||
84de38c5 TG |
1110 | /* The dirty handler will deal with future serialization, but the first one |
1111 | must be done explicitly. */ | |
1112 | ||
1113 | manager_save(m); | |
1114 | ||
1115 | HASHMAP_FOREACH(link, m->links, i) | |
1116 | link_save(link); | |
1117 | ||
f806f64c TG |
1118 | if (m->bus) |
1119 | return bus_event_loop_with_idle( | |
1120 | m->event, | |
1121 | m->bus, | |
1122 | "org.freedesktop.network1", | |
1123 | DEFAULT_EXIT_USEC, | |
1124 | manager_check_idle, | |
1125 | m); | |
1126 | else | |
1127 | /* failed to connect to the bus, so we lose exit-on-idle logic, | |
1128 | this should not happen except if dbus is not around at all */ | |
1129 | return sd_event_loop(m->event); | |
a97dcc12 TG |
1130 | } |
1131 | ||
5fae368b TG |
1132 | int manager_load_config(Manager *m) { |
1133 | int r; | |
1134 | ||
1135 | /* update timestamp */ | |
1136 | paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true); | |
1137 | ||
1138 | r = netdev_load(m); | |
f579559b TG |
1139 | if (r < 0) |
1140 | return r; | |
1141 | ||
5fae368b | 1142 | r = network_load(m); |
9021bb9f TG |
1143 | if (r < 0) |
1144 | return r; | |
1145 | ||
f579559b TG |
1146 | return 0; |
1147 | } | |
f882c247 | 1148 | |
5fae368b TG |
1149 | bool manager_should_reload(Manager *m) { |
1150 | return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false); | |
1151 | } | |
1152 | ||
1153 | int manager_rtnl_enumerate_links(Manager *m) { | |
4afd3348 | 1154 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1155 | sd_netlink_message *link; |
f882c247 TG |
1156 | int r; |
1157 | ||
5da8149f | 1158 | assert(m); |
5fae368b | 1159 | assert(m->rtnl); |
5da8149f | 1160 | |
5fae368b | 1161 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
f882c247 TG |
1162 | if (r < 0) |
1163 | return r; | |
1164 | ||
1c4baffc | 1165 | r = sd_netlink_message_request_dump(req, true); |
dd3efc09 TG |
1166 | if (r < 0) |
1167 | return r; | |
1168 | ||
1c4baffc | 1169 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
f2236469 TG |
1170 | if (r < 0) |
1171 | return r; | |
1172 | ||
1c4baffc | 1173 | for (link = reply; link; link = sd_netlink_message_next(link)) { |
5fae368b | 1174 | int k; |
2e9f08ea | 1175 | |
6a24f148 TG |
1176 | m->enumerating = true; |
1177 | ||
5fae368b TG |
1178 | k = manager_rtnl_process_link(m->rtnl, link, m); |
1179 | if (k < 0) | |
1180 | r = k; | |
6a24f148 TG |
1181 | |
1182 | m->enumerating = false; | |
5fae368b | 1183 | } |
2e9f08ea | 1184 | |
5fae368b | 1185 | return r; |
f882c247 | 1186 | } |
3bef724f | 1187 | |
5fae368b | 1188 | int manager_rtnl_enumerate_addresses(Manager *m) { |
4afd3348 | 1189 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1190 | sd_netlink_message *addr; |
1346b1f0 TG |
1191 | int r; |
1192 | ||
5fae368b TG |
1193 | assert(m); |
1194 | assert(m->rtnl); | |
bcbca829 | 1195 | |
5fae368b TG |
1196 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); |
1197 | if (r < 0) | |
1198 | return r; | |
bcbca829 | 1199 | |
1c4baffc | 1200 | r = sd_netlink_message_request_dump(req, true); |
1346b1f0 TG |
1201 | if (r < 0) |
1202 | return r; | |
1203 | ||
1c4baffc | 1204 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
5fae368b TG |
1205 | if (r < 0) |
1206 | return r; | |
1207 | ||
1c4baffc | 1208 | for (addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
5fae368b TG |
1209 | int k; |
1210 | ||
6a24f148 TG |
1211 | m->enumerating = true; |
1212 | ||
200a0868 | 1213 | k = manager_rtnl_process_address(m->rtnl, addr, m); |
5fae368b TG |
1214 | if (k < 0) |
1215 | r = k; | |
6a24f148 TG |
1216 | |
1217 | m->enumerating = false; | |
5fae368b TG |
1218 | } |
1219 | ||
1220 | return r; | |
1346b1f0 TG |
1221 | } |
1222 | ||
1c8e710c | 1223 | int manager_rtnl_enumerate_routes(Manager *m) { |
4afd3348 | 1224 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c8e710c TG |
1225 | sd_netlink_message *route; |
1226 | int r; | |
1227 | ||
1228 | assert(m); | |
1229 | assert(m->rtnl); | |
1230 | ||
1231 | r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0); | |
1232 | if (r < 0) | |
1233 | return r; | |
1234 | ||
1235 | r = sd_netlink_message_request_dump(req, true); | |
1236 | if (r < 0) | |
1237 | return r; | |
1238 | ||
1239 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
1240 | if (r < 0) | |
1241 | return r; | |
1242 | ||
1243 | for (route = reply; route; route = sd_netlink_message_next(route)) { | |
1244 | int k; | |
1245 | ||
1246 | m->enumerating = true; | |
1247 | ||
1248 | k = manager_rtnl_process_route(m->rtnl, route, m); | |
1249 | if (k < 0) | |
1250 | r = k; | |
1251 | ||
1252 | m->enumerating = false; | |
1253 | } | |
1254 | ||
1255 | return r; | |
1256 | } | |
1257 | ||
0dd25fb9 | 1258 | int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) { |
11bf3cce LP |
1259 | AddressPool *p; |
1260 | int r; | |
1261 | ||
1262 | assert(m); | |
1263 | assert(prefixlen > 0); | |
1264 | assert(found); | |
1265 | ||
1266 | LIST_FOREACH(address_pools, p, m->address_pools) { | |
1267 | if (p->family != family) | |
1268 | continue; | |
1269 | ||
1270 | r = address_pool_acquire(p, prefixlen, found); | |
1271 | if (r != 0) | |
1272 | return r; | |
1273 | } | |
1274 | ||
1275 | return 0; | |
1276 | } | |
4f5f911e LP |
1277 | |
1278 | Link* manager_find_uplink(Manager *m, Link *exclude) { | |
1279 | _cleanup_free_ struct local_address *gateways = NULL; | |
1280 | int n, i; | |
1281 | ||
1282 | assert(m); | |
1283 | ||
1284 | /* Looks for a suitable "uplink", via black magic: an | |
1285 | * interface that is up and where the default route with the | |
1286 | * highest priority points to. */ | |
1287 | ||
1288 | n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways); | |
1289 | if (n < 0) { | |
1290 | log_warning_errno(n, "Failed to determine list of default gateways: %m"); | |
1291 | return NULL; | |
1292 | } | |
1293 | ||
1294 | for (i = 0; i < n; i++) { | |
1295 | Link *link; | |
1296 | ||
1297 | link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex)); | |
1298 | if (!link) { | |
c2c940bd | 1299 | log_debug("Weird, found a gateway for a link we don't know. Ignoring."); |
4f5f911e LP |
1300 | continue; |
1301 | } | |
1302 | ||
1303 | if (link == exclude) | |
1304 | continue; | |
1305 | ||
1306 | if (link->operstate < LINK_OPERSTATE_ROUTABLE) | |
1307 | continue; | |
1308 | ||
1309 | return link; | |
1310 | } | |
1311 | ||
1312 | return NULL; | |
1313 | } | |
84de38c5 TG |
1314 | |
1315 | void manager_dirty(Manager *manager) { | |
1316 | assert(manager); | |
1317 | ||
1318 | /* the serialized state in /run is no longer up-to-date */ | |
1319 | manager->dirty = true; | |
1320 | } |