]>
Commit | Line | Data |
---|---|---|
f579559b TG |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
091a364c | 20 | #include <sys/socket.h> |
bbf7c048 | 21 | #include <linux/if.h> |
3bef724f | 22 | |
fc2f9534 | 23 | #include "sd-daemon.h" |
07630cea | 24 | #include "sd-netlink.h" |
fc2f9534 | 25 | |
b5efdb8a | 26 | #include "alloc-util.h" |
a97dcc12 | 27 | #include "bus-util.h" |
07630cea | 28 | #include "conf-parser.h" |
a97dcc12 | 29 | #include "def.h" |
482d1aeb | 30 | #include "dns-domain.h" |
3ffd4af2 | 31 | #include "fd-util.h" |
0d39fa9c | 32 | #include "fileio.h" |
07630cea | 33 | #include "libudev-private.h" |
4f5f911e | 34 | #include "local-addresses.h" |
07630cea | 35 | #include "netlink-util.h" |
fc2f9534 | 36 | #include "networkd.h" |
00616955 | 37 | #include "ordered-set.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; |
a0db8e46 | 662 | } |
f579559b | 663 | |
1c4baffc | 664 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name); |
45af44d4 | 665 | if (r < 0) { |
2f3cf1f9 | 666 | log_warning_errno(r, "rtnl: Received link message without ifname: %m"); |
4d473d5d | 667 | return 0; |
a0db8e46 ZJS |
668 | } |
669 | ||
670 | (void) link_get(m, ifindex, &link); | |
671 | (void) netdev_get(m, name, &netdev); | |
4d473d5d TG |
672 | |
673 | switch (type) { | |
674 | case RTM_NEWLINK: | |
675 | if (!link) { | |
676 | /* link is new, so add it */ | |
677 | r = link_add(m, message, &link); | |
678 | if (r < 0) { | |
2f3cf1f9 | 679 | log_warning_errno(r, "Could not add new link: %m"); |
4d473d5d TG |
680 | return 0; |
681 | } | |
682 | } | |
683 | ||
684 | if (netdev) { | |
685 | /* netdev exists, so make sure the ifindex matches */ | |
505f8da7 TG |
686 | r = netdev_set_ifindex(netdev, message); |
687 | if (r < 0) { | |
2f3cf1f9 | 688 | log_warning_errno(r, "Could not set ifindex on netdev: %m"); |
505f8da7 TG |
689 | return 0; |
690 | } | |
691 | } | |
e1202047 | 692 | |
f2236469 TG |
693 | r = link_update(link, message); |
694 | if (r < 0) | |
695 | return 0; | |
4d473d5d TG |
696 | |
697 | break; | |
698 | ||
699 | case RTM_DELLINK: | |
700 | link_drop(link); | |
701 | netdev_drop(netdev); | |
702 | ||
703 | break; | |
704 | ||
705 | default: | |
706 | assert_not_reached("Received invalid RTNL message type."); | |
f2236469 | 707 | } |
505f8da7 TG |
708 | |
709 | return 1; | |
710 | } | |
711 | ||
5fae368b TG |
712 | static int systemd_netlink_fd(void) { |
713 | int n, fd, rtnl_fd = -EINVAL; | |
714 | ||
715 | n = sd_listen_fds(true); | |
716 | if (n <= 0) | |
717 | return -EINVAL; | |
718 | ||
719 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
720 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { | |
721 | if (rtnl_fd >= 0) | |
722 | return -EINVAL; | |
723 | ||
724 | rtnl_fd = fd; | |
725 | } | |
726 | } | |
727 | ||
728 | return rtnl_fd; | |
729 | } | |
730 | ||
731 | static int manager_connect_rtnl(Manager *m) { | |
732 | int fd, r; | |
505f8da7 TG |
733 | |
734 | assert(m); | |
505f8da7 | 735 | |
5fae368b TG |
736 | fd = systemd_netlink_fd(); |
737 | if (fd < 0) | |
1c4baffc | 738 | r = sd_netlink_open(&m->rtnl); |
5fae368b | 739 | else |
1c4baffc | 740 | r = sd_netlink_open_fd(&m->rtnl, fd); |
505f8da7 TG |
741 | if (r < 0) |
742 | return r; | |
743 | ||
1c4baffc | 744 | r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE); |
f579559b | 745 | if (r < 0) |
bf5332d2 | 746 | return r; |
f579559b | 747 | |
1c4baffc | 748 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
505f8da7 TG |
749 | if (r < 0) |
750 | return r; | |
f579559b | 751 | |
1c4baffc | 752 | r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
753 | if (r < 0) |
754 | return r; | |
505f8da7 | 755 | |
1c4baffc | 756 | r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
757 | if (r < 0) |
758 | return r; | |
45af44d4 | 759 | |
200a0868 | 760 | r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
761 | if (r < 0) |
762 | return r; | |
763 | ||
200a0868 | 764 | r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
765 | if (r < 0) |
766 | return r; | |
767 | ||
1c8e710c TG |
768 | r = sd_netlink_add_match(m->rtnl, RTM_NEWROUTE, &manager_rtnl_process_route, m); |
769 | if (r < 0) | |
770 | return r; | |
771 | ||
772 | r = sd_netlink_add_match(m->rtnl, RTM_DELROUTE, &manager_rtnl_process_route, m); | |
773 | if (r < 0) | |
774 | return r; | |
775 | ||
5fae368b | 776 | return 0; |
45af44d4 | 777 | } |
505f8da7 | 778 | |
00616955 | 779 | static int ordered_set_put_in_addr(OrderedSet *s, const struct in_addr *address) { |
84de38c5 TG |
780 | char *p; |
781 | int r; | |
782 | ||
783 | assert(s); | |
784 | ||
785 | r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p); | |
786 | if (r < 0) | |
787 | return r; | |
788 | ||
00616955 | 789 | r = ordered_set_consume(s, p); |
84de38c5 TG |
790 | if (r == -EEXIST) |
791 | return 0; | |
792 | ||
793 | return r; | |
794 | } | |
795 | ||
00616955 | 796 | static int ordered_set_put_in_addrv(OrderedSet *s, const struct in_addr *addresses, int n) { |
84de38c5 TG |
797 | int r, i, c = 0; |
798 | ||
799 | assert(s); | |
800 | assert(n <= 0 || addresses); | |
801 | ||
802 | for (i = 0; i < n; i++) { | |
00616955 | 803 | r = ordered_set_put_in_addr(s, addresses+i); |
84de38c5 TG |
804 | if (r < 0) |
805 | return r; | |
806 | ||
807 | c += r; | |
808 | } | |
809 | ||
810 | return c; | |
811 | } | |
812 | ||
00616955 | 813 | static void print_string_set(FILE *f, const char *field, OrderedSet *s) { |
84de38c5 TG |
814 | bool space = false; |
815 | Iterator i; | |
816 | char *p; | |
817 | ||
00616955 | 818 | if (ordered_set_isempty(s)) |
84de38c5 TG |
819 | return; |
820 | ||
821 | fputs(field, f); | |
822 | ||
d390f8ef LP |
823 | ORDERED_SET_FOREACH(p, s, i) |
824 | fputs_with_space(f, p, NULL, &space); | |
825 | ||
84de38c5 TG |
826 | fputc('\n', f); |
827 | } | |
828 | ||
829 | static int manager_save(Manager *m) { | |
00616955 | 830 | _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL; |
84de38c5 TG |
831 | Link *link; |
832 | Iterator i; | |
833 | _cleanup_free_ char *temp_path = NULL; | |
834 | _cleanup_fclose_ FILE *f = NULL; | |
835 | LinkOperationalState operstate = LINK_OPERSTATE_OFF; | |
836 | const char *operstate_str; | |
837 | int r; | |
838 | ||
839 | assert(m); | |
840 | assert(m->state_file); | |
841 | ||
842 | /* We add all NTP and DNS server to a set, to filter out duplicates */ | |
00616955 | 843 | dns = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
844 | if (!dns) |
845 | return -ENOMEM; | |
846 | ||
00616955 | 847 | ntp = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
848 | if (!ntp) |
849 | return -ENOMEM; | |
850 | ||
482d1aeb | 851 | search_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 LP |
852 | if (!search_domains) |
853 | return -ENOMEM; | |
854 | ||
482d1aeb | 855 | route_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 | 856 | if (!route_domains) |
84de38c5 TG |
857 | return -ENOMEM; |
858 | ||
859 | HASHMAP_FOREACH(link, m->links, i) { | |
860 | if (link->flags & IFF_LOOPBACK) | |
861 | continue; | |
862 | ||
863 | if (link->operstate > operstate) | |
864 | operstate = link->operstate; | |
865 | ||
866 | if (!link->network) | |
867 | continue; | |
868 | ||
869 | /* First add the static configured entries */ | |
00616955 | 870 | r = ordered_set_put_strdupv(dns, link->network->dns); |
84de38c5 TG |
871 | if (r < 0) |
872 | return r; | |
873 | ||
00616955 | 874 | r = ordered_set_put_strdupv(ntp, link->network->ntp); |
84de38c5 TG |
875 | if (r < 0) |
876 | return r; | |
877 | ||
00616955 | 878 | r = ordered_set_put_strdupv(search_domains, link->network->search_domains); |
3df9bec5 LP |
879 | if (r < 0) |
880 | return r; | |
881 | ||
00616955 | 882 | r = ordered_set_put_strdupv(route_domains, link->network->route_domains); |
84de38c5 TG |
883 | if (r < 0) |
884 | return r; | |
885 | ||
886 | if (!link->dhcp_lease) | |
887 | continue; | |
888 | ||
889 | /* Secondly, add the entries acquired via DHCP */ | |
27cb34f5 | 890 | if (link->network->dhcp_use_dns) { |
84de38c5 TG |
891 | const struct in_addr *addresses; |
892 | ||
893 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
894 | if (r > 0) { | |
00616955 | 895 | r = ordered_set_put_in_addrv(dns, addresses, r); |
84de38c5 TG |
896 | if (r < 0) |
897 | return r; | |
898 | } else if (r < 0 && r != -ENODATA) | |
899 | return r; | |
900 | } | |
901 | ||
27cb34f5 | 902 | if (link->network->dhcp_use_ntp) { |
84de38c5 TG |
903 | const struct in_addr *addresses; |
904 | ||
905 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
906 | if (r > 0) { | |
00616955 | 907 | r = ordered_set_put_in_addrv(ntp, addresses, r); |
84de38c5 TG |
908 | if (r < 0) |
909 | return r; | |
910 | } else if (r < 0 && r != -ENODATA) | |
911 | return r; | |
912 | } | |
913 | ||
b2a81c0b | 914 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
84de38c5 TG |
915 | const char *domainname; |
916 | ||
917 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); | |
918 | if (r >= 0) { | |
b2a81c0b LP |
919 | |
920 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) | |
921 | r = ordered_set_put_strdup(search_domains, domainname); | |
922 | else | |
923 | r = ordered_set_put_strdup(route_domains, domainname); | |
924 | ||
84de38c5 TG |
925 | if (r < 0) |
926 | return r; | |
927 | } else if (r != -ENODATA) | |
928 | return r; | |
929 | } | |
930 | } | |
931 | ||
932 | operstate_str = link_operstate_to_string(operstate); | |
933 | assert(operstate_str); | |
934 | ||
935 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
936 | if (r < 0) | |
937 | return r; | |
938 | ||
939 | fchmod(fileno(f), 0644); | |
940 | ||
941 | fprintf(f, | |
942 | "# This is private data. Do not parse.\n" | |
943 | "OPER_STATE=%s\n", operstate_str); | |
944 | ||
945 | print_string_set(f, "DNS=", dns); | |
946 | print_string_set(f, "NTP=", ntp); | |
3df9bec5 LP |
947 | print_string_set(f, "DOMAINS=", search_domains); |
948 | print_string_set(f, "ROUTE_DOMAINS=", route_domains); | |
84de38c5 TG |
949 | |
950 | r = fflush_and_check(f); | |
951 | if (r < 0) | |
952 | goto fail; | |
953 | ||
954 | if (rename(temp_path, m->state_file) < 0) { | |
955 | r = -errno; | |
956 | goto fail; | |
957 | } | |
958 | ||
959 | if (m->operational_state != operstate) { | |
960 | m->operational_state = operstate; | |
961 | r = manager_send_changed(m, "OperationalState", NULL); | |
962 | if (r < 0) | |
963 | log_error_errno(r, "Could not emit changed OperationalState: %m"); | |
964 | } | |
965 | ||
966 | m->dirty = false; | |
967 | ||
968 | return 0; | |
969 | ||
970 | fail: | |
971 | (void) unlink(m->state_file); | |
972 | (void) unlink(temp_path); | |
973 | ||
974 | return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file); | |
975 | } | |
976 | ||
977 | static int manager_dirty_handler(sd_event_source *s, void *userdata) { | |
978 | Manager *m = userdata; | |
979 | Link *link; | |
980 | Iterator i; | |
981 | int r; | |
982 | ||
983 | assert(m); | |
984 | ||
985 | if (m->dirty) | |
986 | manager_save(m); | |
987 | ||
988 | SET_FOREACH(link, m->dirty_links, i) { | |
989 | r = link_save(link); | |
990 | if (r >= 0) | |
991 | link_clean(link); | |
992 | } | |
993 | ||
994 | return 1; | |
995 | } | |
996 | ||
5fae368b TG |
997 | int manager_new(Manager **ret) { |
998 | _cleanup_manager_free_ Manager *m = NULL; | |
45af44d4 | 999 | int r; |
f579559b | 1000 | |
5fae368b TG |
1001 | m = new0(Manager, 1); |
1002 | if (!m) | |
1003 | return -ENOMEM; | |
45af44d4 | 1004 | |
5fae368b TG |
1005 | m->state_file = strdup("/run/systemd/netif/state"); |
1006 | if (!m->state_file) | |
1007 | return -ENOMEM; | |
1008 | ||
1009 | r = sd_event_default(&m->event); | |
45af44d4 TG |
1010 | if (r < 0) |
1011 | return r; | |
1012 | ||
5fae368b TG |
1013 | sd_event_set_watchdog(m->event, true); |
1014 | ||
1015 | sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); | |
1016 | sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
1017 | ||
84de38c5 TG |
1018 | r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m); |
1019 | if (r < 0) | |
1020 | return r; | |
1021 | ||
5fae368b | 1022 | r = manager_connect_rtnl(m); |
45af44d4 TG |
1023 | if (r < 0) |
1024 | return r; | |
1025 | ||
5fae368b TG |
1026 | r = manager_connect_udev(m); |
1027 | if (r < 0) | |
1028 | return r; | |
45af44d4 | 1029 | |
5fae368b TG |
1030 | m->netdevs = hashmap_new(&string_hash_ops); |
1031 | if (!m->netdevs) | |
1032 | return -ENOMEM; | |
f579559b | 1033 | |
5fae368b | 1034 | LIST_HEAD_INIT(m->networks); |
f579559b | 1035 | |
5fae368b TG |
1036 | r = setup_default_address_pool(m); |
1037 | if (r < 0) | |
1038 | return r; | |
f579559b | 1039 | |
d7df2fd3 | 1040 | m->duid_type = DUID_TYPE_EN; |
413708d1 | 1041 | |
5fae368b TG |
1042 | *ret = m; |
1043 | m = NULL; | |
f579559b | 1044 | |
f579559b TG |
1045 | return 0; |
1046 | } | |
1047 | ||
5fae368b TG |
1048 | void manager_free(Manager *m) { |
1049 | Network *network; | |
1050 | NetDev *netdev; | |
1051 | Link *link; | |
1052 | AddressPool *pool; | |
f579559b | 1053 | |
5fae368b TG |
1054 | if (!m) |
1055 | return; | |
505f8da7 | 1056 | |
5fae368b | 1057 | free(m->state_file); |
505f8da7 | 1058 | |
5fae368b TG |
1059 | while ((link = hashmap_first(m->links))) |
1060 | link_unref(link); | |
1061 | hashmap_free(m->links); | |
f579559b | 1062 | |
5fae368b TG |
1063 | while ((network = m->networks)) |
1064 | network_free(network); | |
1065 | ||
dbffab87 TG |
1066 | hashmap_free(m->networks_by_name); |
1067 | ||
5fae368b TG |
1068 | while ((netdev = hashmap_first(m->netdevs))) |
1069 | netdev_unref(netdev); | |
1070 | hashmap_free(m->netdevs); | |
1071 | ||
1072 | while ((pool = m->address_pools)) | |
1073 | address_pool_free(pool); | |
1074 | ||
1c4baffc | 1075 | sd_netlink_unref(m->rtnl); |
2f5b4a77 | 1076 | sd_event_unref(m->event); |
5fae368b | 1077 | |
7d20d375 TG |
1078 | sd_event_source_unref(m->udev_event_source); |
1079 | udev_monitor_unref(m->udev_monitor); | |
1080 | udev_unref(m->udev); | |
1081 | ||
1082 | sd_bus_unref(m->bus); | |
1083 | sd_bus_slot_unref(m->prepare_for_sleep_slot); | |
1084 | sd_event_source_unref(m->bus_retry_event_source); | |
1085 | ||
5fae368b TG |
1086 | free(m); |
1087 | } | |
1088 | ||
a97dcc12 TG |
1089 | static bool manager_check_idle(void *userdata) { |
1090 | Manager *m = userdata; | |
1091 | Link *link; | |
1092 | Iterator i; | |
1093 | ||
1094 | assert(m); | |
1095 | ||
9dc907f9 LP |
1096 | /* Check whether we are idle now. The only case when we decide to be idle is when there's only a loopback |
1097 | * device around, for which we have no configuration, and which already left the PENDING state. In all other | |
1098 | * cases we are not idle. */ | |
1099 | ||
a97dcc12 | 1100 | HASHMAP_FOREACH(link, m->links, i) { |
9dc907f9 | 1101 | /* We are not woken on udev activity, so let's just wait for the pending udev event */ |
a97dcc12 TG |
1102 | if (link->state == LINK_STATE_PENDING) |
1103 | return false; | |
1104 | ||
9dc907f9 LP |
1105 | if ((link->flags & IFF_LOOPBACK) == 0) |
1106 | return false; | |
a97dcc12 | 1107 | |
9dc907f9 | 1108 | if (link->network) |
a97dcc12 TG |
1109 | return false; |
1110 | } | |
1111 | ||
1112 | return true; | |
1113 | } | |
1114 | ||
1115 | int manager_run(Manager *m) { | |
84de38c5 TG |
1116 | Link *link; |
1117 | Iterator i; | |
1118 | ||
a97dcc12 TG |
1119 | assert(m); |
1120 | ||
84de38c5 TG |
1121 | /* The dirty handler will deal with future serialization, but the first one |
1122 | must be done explicitly. */ | |
1123 | ||
1124 | manager_save(m); | |
1125 | ||
1126 | HASHMAP_FOREACH(link, m->links, i) | |
1127 | link_save(link); | |
1128 | ||
f806f64c TG |
1129 | if (m->bus) |
1130 | return bus_event_loop_with_idle( | |
1131 | m->event, | |
1132 | m->bus, | |
1133 | "org.freedesktop.network1", | |
1134 | DEFAULT_EXIT_USEC, | |
1135 | manager_check_idle, | |
1136 | m); | |
1137 | else | |
1138 | /* failed to connect to the bus, so we lose exit-on-idle logic, | |
1139 | this should not happen except if dbus is not around at all */ | |
1140 | return sd_event_loop(m->event); | |
a97dcc12 TG |
1141 | } |
1142 | ||
5fae368b TG |
1143 | int manager_load_config(Manager *m) { |
1144 | int r; | |
1145 | ||
1146 | /* update timestamp */ | |
1147 | paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true); | |
1148 | ||
1149 | r = netdev_load(m); | |
f579559b TG |
1150 | if (r < 0) |
1151 | return r; | |
1152 | ||
5fae368b | 1153 | r = network_load(m); |
9021bb9f TG |
1154 | if (r < 0) |
1155 | return r; | |
1156 | ||
f579559b TG |
1157 | return 0; |
1158 | } | |
f882c247 | 1159 | |
5fae368b TG |
1160 | bool manager_should_reload(Manager *m) { |
1161 | return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false); | |
1162 | } | |
1163 | ||
1164 | int manager_rtnl_enumerate_links(Manager *m) { | |
4afd3348 | 1165 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1166 | sd_netlink_message *link; |
f882c247 TG |
1167 | int r; |
1168 | ||
5da8149f | 1169 | assert(m); |
5fae368b | 1170 | assert(m->rtnl); |
5da8149f | 1171 | |
5fae368b | 1172 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
f882c247 TG |
1173 | if (r < 0) |
1174 | return r; | |
1175 | ||
1c4baffc | 1176 | r = sd_netlink_message_request_dump(req, true); |
dd3efc09 TG |
1177 | if (r < 0) |
1178 | return r; | |
1179 | ||
1c4baffc | 1180 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
f2236469 TG |
1181 | if (r < 0) |
1182 | return r; | |
1183 | ||
1c4baffc | 1184 | for (link = reply; link; link = sd_netlink_message_next(link)) { |
5fae368b | 1185 | int k; |
2e9f08ea | 1186 | |
6a24f148 TG |
1187 | m->enumerating = true; |
1188 | ||
5fae368b TG |
1189 | k = manager_rtnl_process_link(m->rtnl, link, m); |
1190 | if (k < 0) | |
1191 | r = k; | |
6a24f148 TG |
1192 | |
1193 | m->enumerating = false; | |
5fae368b | 1194 | } |
2e9f08ea | 1195 | |
5fae368b | 1196 | return r; |
f882c247 | 1197 | } |
3bef724f | 1198 | |
5fae368b | 1199 | int manager_rtnl_enumerate_addresses(Manager *m) { |
4afd3348 | 1200 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1201 | sd_netlink_message *addr; |
1346b1f0 TG |
1202 | int r; |
1203 | ||
5fae368b TG |
1204 | assert(m); |
1205 | assert(m->rtnl); | |
bcbca829 | 1206 | |
5fae368b TG |
1207 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); |
1208 | if (r < 0) | |
1209 | return r; | |
bcbca829 | 1210 | |
1c4baffc | 1211 | r = sd_netlink_message_request_dump(req, true); |
1346b1f0 TG |
1212 | if (r < 0) |
1213 | return r; | |
1214 | ||
1c4baffc | 1215 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
5fae368b TG |
1216 | if (r < 0) |
1217 | return r; | |
1218 | ||
1c4baffc | 1219 | for (addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
5fae368b TG |
1220 | int k; |
1221 | ||
6a24f148 TG |
1222 | m->enumerating = true; |
1223 | ||
200a0868 | 1224 | k = manager_rtnl_process_address(m->rtnl, addr, m); |
5fae368b TG |
1225 | if (k < 0) |
1226 | r = k; | |
6a24f148 TG |
1227 | |
1228 | m->enumerating = false; | |
5fae368b TG |
1229 | } |
1230 | ||
1231 | return r; | |
1346b1f0 TG |
1232 | } |
1233 | ||
1c8e710c | 1234 | int manager_rtnl_enumerate_routes(Manager *m) { |
4afd3348 | 1235 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c8e710c TG |
1236 | sd_netlink_message *route; |
1237 | int r; | |
1238 | ||
1239 | assert(m); | |
1240 | assert(m->rtnl); | |
1241 | ||
1242 | r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0); | |
1243 | if (r < 0) | |
1244 | return r; | |
1245 | ||
1246 | r = sd_netlink_message_request_dump(req, true); | |
1247 | if (r < 0) | |
1248 | return r; | |
1249 | ||
1250 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
1251 | if (r < 0) | |
1252 | return r; | |
1253 | ||
1254 | for (route = reply; route; route = sd_netlink_message_next(route)) { | |
1255 | int k; | |
1256 | ||
1257 | m->enumerating = true; | |
1258 | ||
1259 | k = manager_rtnl_process_route(m->rtnl, route, m); | |
1260 | if (k < 0) | |
1261 | r = k; | |
1262 | ||
1263 | m->enumerating = false; | |
1264 | } | |
1265 | ||
1266 | return r; | |
1267 | } | |
1268 | ||
0dd25fb9 | 1269 | int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) { |
11bf3cce LP |
1270 | AddressPool *p; |
1271 | int r; | |
1272 | ||
1273 | assert(m); | |
1274 | assert(prefixlen > 0); | |
1275 | assert(found); | |
1276 | ||
1277 | LIST_FOREACH(address_pools, p, m->address_pools) { | |
1278 | if (p->family != family) | |
1279 | continue; | |
1280 | ||
1281 | r = address_pool_acquire(p, prefixlen, found); | |
1282 | if (r != 0) | |
1283 | return r; | |
1284 | } | |
1285 | ||
1286 | return 0; | |
1287 | } | |
4f5f911e LP |
1288 | |
1289 | Link* manager_find_uplink(Manager *m, Link *exclude) { | |
1290 | _cleanup_free_ struct local_address *gateways = NULL; | |
1291 | int n, i; | |
1292 | ||
1293 | assert(m); | |
1294 | ||
1295 | /* Looks for a suitable "uplink", via black magic: an | |
1296 | * interface that is up and where the default route with the | |
1297 | * highest priority points to. */ | |
1298 | ||
1299 | n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways); | |
1300 | if (n < 0) { | |
1301 | log_warning_errno(n, "Failed to determine list of default gateways: %m"); | |
1302 | return NULL; | |
1303 | } | |
1304 | ||
1305 | for (i = 0; i < n; i++) { | |
1306 | Link *link; | |
1307 | ||
1308 | link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex)); | |
1309 | if (!link) { | |
c2c940bd | 1310 | log_debug("Weird, found a gateway for a link we don't know. Ignoring."); |
4f5f911e LP |
1311 | continue; |
1312 | } | |
1313 | ||
1314 | if (link == exclude) | |
1315 | continue; | |
1316 | ||
1317 | if (link->operstate < LINK_OPERSTATE_ROUTABLE) | |
1318 | continue; | |
1319 | ||
1320 | return link; | |
1321 | } | |
1322 | ||
1323 | return NULL; | |
1324 | } | |
84de38c5 TG |
1325 | |
1326 | void manager_dirty(Manager *manager) { | |
1327 | assert(manager); | |
1328 | ||
1329 | /* the serialized state in /run is no longer up-to-date */ | |
1330 | manager->dirty = true; | |
1331 | } |