]>
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" |
23f53b99 | 36 | #include "networkd-manager.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: | |
b9642f41 | 460 | route_free(route); |
1c8e710c | 461 | break; |
b9642f41 | 462 | |
1c8e710c TG |
463 | default: |
464 | assert_not_reached("Received invalid RTNL message type"); | |
465 | } | |
466 | ||
467 | return 1; | |
468 | } | |
469 | ||
200a0868 TG |
470 | int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
471 | Manager *m = userdata; | |
472 | Link *link = NULL; | |
473 | uint16_t type; | |
200a0868 | 474 | unsigned char flags; |
054f0db4 TG |
475 | int family; |
476 | unsigned char prefixlen; | |
477 | unsigned char scope; | |
478 | union in_addr_union in_addr; | |
479 | struct ifa_cacheinfo cinfo; | |
480 | Address *address = NULL; | |
200a0868 TG |
481 | char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX]; |
482 | const char *valid_str = NULL; | |
483 | int r, ifindex; | |
484 | ||
485 | assert(rtnl); | |
486 | assert(message); | |
487 | assert(m); | |
488 | ||
489 | if (sd_netlink_message_is_error(message)) { | |
490 | r = sd_netlink_message_get_errno(message); | |
491 | if (r < 0) | |
492 | log_warning_errno(r, "rtnl: failed to receive address: %m"); | |
493 | ||
494 | return 0; | |
495 | } | |
496 | ||
497 | r = sd_netlink_message_get_type(message, &type); | |
498 | if (r < 0) { | |
499 | log_warning_errno(r, "rtnl: could not get message type: %m"); | |
500 | return 0; | |
501 | } else if (type != RTM_NEWADDR && type != RTM_DELADDR) { | |
502 | log_warning("rtnl: received unexpected message type when processing address"); | |
503 | return 0; | |
504 | } | |
505 | ||
506 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
507 | if (r < 0) { | |
508 | log_warning_errno(r, "rtnl: could not get ifindex from address: %m"); | |
509 | return 0; | |
510 | } else if (ifindex <= 0) { | |
511 | log_warning("rtnl: received address message with invalid ifindex: %d", ifindex); | |
512 | return 0; | |
513 | } else { | |
514 | r = link_get(m, ifindex, &link); | |
515 | if (r < 0 || !link) { | |
516 | /* when enumerating we might be out of sync, but we will | |
517 | * get the address again, so just ignore it */ | |
518 | if (!m->enumerating) | |
519 | log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex); | |
520 | return 0; | |
521 | } | |
522 | } | |
523 | ||
054f0db4 TG |
524 | r = sd_rtnl_message_addr_get_family(message, &family); |
525 | if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) { | |
200a0868 TG |
526 | log_link_warning(link, "rtnl: received address with invalid family, ignoring."); |
527 | return 0; | |
528 | } | |
529 | ||
054f0db4 | 530 | r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen); |
200a0868 TG |
531 | if (r < 0) { |
532 | log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m"); | |
533 | return 0; | |
534 | } | |
535 | ||
054f0db4 | 536 | r = sd_rtnl_message_addr_get_scope(message, &scope); |
200a0868 TG |
537 | if (r < 0) { |
538 | log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m"); | |
539 | return 0; | |
540 | } | |
541 | ||
542 | r = sd_rtnl_message_addr_get_flags(message, &flags); | |
543 | if (r < 0) { | |
544 | log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m"); | |
545 | return 0; | |
546 | } | |
200a0868 | 547 | |
054f0db4 | 548 | switch (family) { |
200a0868 | 549 | case AF_INET: |
054f0db4 | 550 | r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in); |
200a0868 TG |
551 | if (r < 0) { |
552 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); | |
553 | return 0; | |
554 | } | |
555 | ||
556 | break; | |
557 | ||
558 | case AF_INET6: | |
054f0db4 | 559 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6); |
200a0868 TG |
560 | if (r < 0) { |
561 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); | |
562 | return 0; | |
563 | } | |
564 | ||
565 | break; | |
566 | ||
567 | default: | |
1c8e710c | 568 | log_link_debug(link, "rtnl: ignoring unsupported address family: %d", family); |
200a0868 TG |
569 | } |
570 | ||
054f0db4 | 571 | if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) { |
200a0868 TG |
572 | log_link_warning(link, "Could not print address"); |
573 | return 0; | |
574 | } | |
575 | ||
054f0db4 | 576 | r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo); |
200a0868 | 577 | if (r >= 0) { |
4058d339 | 578 | if (cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME) |
200a0868 | 579 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, |
054f0db4 | 580 | cinfo.ifa_valid * USEC_PER_SEC, |
200a0868 TG |
581 | USEC_PER_SEC); |
582 | } | |
583 | ||
054f0db4 | 584 | address_get(link, family, &in_addr, prefixlen, &address); |
200a0868 TG |
585 | |
586 | switch (type) { | |
587 | case RTM_NEWADDR: | |
36c32f61 | 588 | if (address) |
4058d339 TG |
589 | log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen, |
590 | valid_str ? "for " : "forever", valid_str ?: ""); | |
36c32f61 | 591 | else { |
adda1ed9 TG |
592 | /* An address appeared that we did not request */ |
593 | r = address_add_foreign(link, family, &in_addr, prefixlen, &address); | |
cf1d700d | 594 | if (r < 0) { |
054f0db4 | 595 | log_link_warning_errno(link, r, "Failed to add address %s/%u: %m", buf, prefixlen); |
cf1d700d TG |
596 | return 0; |
597 | } else | |
4058d339 TG |
598 | log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen, |
599 | valid_str ? "for " : "forever", valid_str ?: ""); | |
200a0868 TG |
600 | } |
601 | ||
58fda79c | 602 | address_update(address, flags, scope, &cinfo); |
36c32f61 | 603 | |
200a0868 TG |
604 | break; |
605 | ||
606 | case RTM_DELADDR: | |
607 | ||
054f0db4 | 608 | if (address) { |
4058d339 TG |
609 | log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen, |
610 | valid_str ? "for " : "forever", valid_str ?: ""); | |
91b5f997 | 611 | address_drop(address); |
200a0868 | 612 | } else |
4058d339 TG |
613 | log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s)", buf, prefixlen, |
614 | valid_str ? "for " : "forever", valid_str ?: ""); | |
200a0868 TG |
615 | |
616 | break; | |
617 | default: | |
618 | assert_not_reached("Received invalid RTNL message type"); | |
619 | } | |
620 | ||
621 | return 1; | |
622 | } | |
623 | ||
1c4baffc | 624 | static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
505f8da7 TG |
625 | Manager *m = userdata; |
626 | Link *link = NULL; | |
4d473d5d | 627 | NetDev *netdev = NULL; |
f2236469 | 628 | uint16_t type; |
ca4e095a | 629 | const char *name; |
505f8da7 | 630 | int r, ifindex; |
f579559b | 631 | |
505f8da7 TG |
632 | assert(rtnl); |
633 | assert(message); | |
f579559b TG |
634 | assert(m); |
635 | ||
1c4baffc TG |
636 | if (sd_netlink_message_is_error(message)) { |
637 | r = sd_netlink_message_get_errno(message); | |
45af44d4 | 638 | if (r < 0) |
2f3cf1f9 | 639 | log_warning_errno(r, "rtnl: Could not receive link: %m"); |
45af44d4 TG |
640 | |
641 | return 0; | |
642 | } | |
643 | ||
1c4baffc | 644 | r = sd_netlink_message_get_type(message, &type); |
f2236469 | 645 | if (r < 0) { |
2f3cf1f9 | 646 | log_warning_errno(r, "rtnl: Could not get message type: %m"); |
f2236469 | 647 | return 0; |
cdfee943 | 648 | } else if (type != RTM_NEWLINK && type != RTM_DELLINK) { |
2f3cf1f9 | 649 | log_warning("rtnl: Received unexpected message type when processing link"); |
cdfee943 | 650 | return 0; |
f2236469 TG |
651 | } |
652 | ||
505f8da7 | 653 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
45af44d4 | 654 | if (r < 0) { |
2f3cf1f9 | 655 | log_warning_errno(r, "rtnl: Could not get ifindex from link: %m"); |
45af44d4 TG |
656 | return 0; |
657 | } else if (ifindex <= 0) { | |
658 | log_warning("rtnl: received link message with invalid ifindex: %d", ifindex); | |
505f8da7 | 659 | return 0; |
a0db8e46 | 660 | } |
f579559b | 661 | |
1c4baffc | 662 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name); |
45af44d4 | 663 | if (r < 0) { |
2f3cf1f9 | 664 | log_warning_errno(r, "rtnl: Received link message without ifname: %m"); |
4d473d5d | 665 | return 0; |
a0db8e46 ZJS |
666 | } |
667 | ||
668 | (void) link_get(m, ifindex, &link); | |
669 | (void) netdev_get(m, name, &netdev); | |
4d473d5d TG |
670 | |
671 | switch (type) { | |
672 | case RTM_NEWLINK: | |
673 | if (!link) { | |
674 | /* link is new, so add it */ | |
675 | r = link_add(m, message, &link); | |
676 | if (r < 0) { | |
2f3cf1f9 | 677 | log_warning_errno(r, "Could not add new link: %m"); |
4d473d5d TG |
678 | return 0; |
679 | } | |
680 | } | |
681 | ||
682 | if (netdev) { | |
683 | /* netdev exists, so make sure the ifindex matches */ | |
505f8da7 TG |
684 | r = netdev_set_ifindex(netdev, message); |
685 | if (r < 0) { | |
2f3cf1f9 | 686 | log_warning_errno(r, "Could not set ifindex on netdev: %m"); |
505f8da7 TG |
687 | return 0; |
688 | } | |
689 | } | |
e1202047 | 690 | |
f2236469 TG |
691 | r = link_update(link, message); |
692 | if (r < 0) | |
693 | return 0; | |
4d473d5d TG |
694 | |
695 | break; | |
696 | ||
697 | case RTM_DELLINK: | |
698 | link_drop(link); | |
699 | netdev_drop(netdev); | |
700 | ||
701 | break; | |
702 | ||
703 | default: | |
704 | assert_not_reached("Received invalid RTNL message type."); | |
f2236469 | 705 | } |
505f8da7 TG |
706 | |
707 | return 1; | |
708 | } | |
709 | ||
5fae368b TG |
710 | static int systemd_netlink_fd(void) { |
711 | int n, fd, rtnl_fd = -EINVAL; | |
712 | ||
713 | n = sd_listen_fds(true); | |
714 | if (n <= 0) | |
715 | return -EINVAL; | |
716 | ||
717 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
718 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { | |
719 | if (rtnl_fd >= 0) | |
720 | return -EINVAL; | |
721 | ||
722 | rtnl_fd = fd; | |
723 | } | |
724 | } | |
725 | ||
726 | return rtnl_fd; | |
727 | } | |
728 | ||
729 | static int manager_connect_rtnl(Manager *m) { | |
730 | int fd, r; | |
505f8da7 TG |
731 | |
732 | assert(m); | |
505f8da7 | 733 | |
5fae368b TG |
734 | fd = systemd_netlink_fd(); |
735 | if (fd < 0) | |
1c4baffc | 736 | r = sd_netlink_open(&m->rtnl); |
5fae368b | 737 | else |
1c4baffc | 738 | r = sd_netlink_open_fd(&m->rtnl, fd); |
505f8da7 TG |
739 | if (r < 0) |
740 | return r; | |
741 | ||
1c4baffc | 742 | r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE); |
f579559b | 743 | if (r < 0) |
bf5332d2 | 744 | return r; |
f579559b | 745 | |
1c4baffc | 746 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
505f8da7 TG |
747 | if (r < 0) |
748 | return r; | |
f579559b | 749 | |
1c4baffc | 750 | r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
751 | if (r < 0) |
752 | return r; | |
505f8da7 | 753 | |
1c4baffc | 754 | r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
755 | if (r < 0) |
756 | return r; | |
45af44d4 | 757 | |
200a0868 | 758 | r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
759 | if (r < 0) |
760 | return r; | |
761 | ||
200a0868 | 762 | r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
763 | if (r < 0) |
764 | return r; | |
765 | ||
1c8e710c TG |
766 | r = sd_netlink_add_match(m->rtnl, RTM_NEWROUTE, &manager_rtnl_process_route, m); |
767 | if (r < 0) | |
768 | return r; | |
769 | ||
770 | r = sd_netlink_add_match(m->rtnl, RTM_DELROUTE, &manager_rtnl_process_route, m); | |
771 | if (r < 0) | |
772 | return r; | |
773 | ||
5fae368b | 774 | return 0; |
45af44d4 | 775 | } |
505f8da7 | 776 | |
5512a963 | 777 | static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) { |
84de38c5 TG |
778 | char *p; |
779 | int r; | |
780 | ||
781 | assert(s); | |
5512a963 LP |
782 | assert(address); |
783 | ||
784 | r = in_addr_to_string(address->family, &address->address, &p); | |
785 | if (r < 0) | |
786 | return r; | |
787 | ||
788 | r = ordered_set_consume(s, p); | |
789 | if (r == -EEXIST) | |
790 | return 0; | |
791 | ||
792 | return r; | |
793 | } | |
794 | ||
795 | static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) { | |
796 | int r, c = 0; | |
797 | unsigned i; | |
798 | ||
799 | assert(s); | |
800 | assert(addresses || n == 0); | |
801 | ||
802 | for (i = 0; i < n; i++) { | |
803 | r = ordered_set_put_in_addr_data(s, addresses+i); | |
804 | if (r < 0) | |
805 | return r; | |
806 | ||
807 | c += r; | |
808 | } | |
809 | ||
810 | return c; | |
811 | } | |
812 | ||
813 | static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) { | |
814 | char *p; | |
815 | int r; | |
816 | ||
817 | assert(s); | |
818 | assert(address); | |
84de38c5 TG |
819 | |
820 | r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p); | |
821 | if (r < 0) | |
822 | return r; | |
823 | ||
00616955 | 824 | r = ordered_set_consume(s, p); |
84de38c5 TG |
825 | if (r == -EEXIST) |
826 | return 0; | |
827 | ||
828 | return r; | |
829 | } | |
830 | ||
5512a963 LP |
831 | static int ordered_set_put_in4_addrv(OrderedSet *s, const struct in_addr *addresses, unsigned n) { |
832 | int r, c = 0; | |
833 | unsigned i; | |
84de38c5 TG |
834 | |
835 | assert(s); | |
5512a963 | 836 | assert(n == 0 || addresses); |
84de38c5 TG |
837 | |
838 | for (i = 0; i < n; i++) { | |
5512a963 | 839 | r = ordered_set_put_in4_addr(s, addresses+i); |
84de38c5 TG |
840 | if (r < 0) |
841 | return r; | |
842 | ||
843 | c += r; | |
844 | } | |
845 | ||
846 | return c; | |
847 | } | |
848 | ||
00616955 | 849 | static void print_string_set(FILE *f, const char *field, OrderedSet *s) { |
84de38c5 TG |
850 | bool space = false; |
851 | Iterator i; | |
852 | char *p; | |
853 | ||
00616955 | 854 | if (ordered_set_isempty(s)) |
84de38c5 TG |
855 | return; |
856 | ||
857 | fputs(field, f); | |
858 | ||
d390f8ef LP |
859 | ORDERED_SET_FOREACH(p, s, i) |
860 | fputs_with_space(f, p, NULL, &space); | |
861 | ||
84de38c5 TG |
862 | fputc('\n', f); |
863 | } | |
864 | ||
865 | static int manager_save(Manager *m) { | |
00616955 | 866 | _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL; |
84de38c5 TG |
867 | Link *link; |
868 | Iterator i; | |
869 | _cleanup_free_ char *temp_path = NULL; | |
870 | _cleanup_fclose_ FILE *f = NULL; | |
871 | LinkOperationalState operstate = LINK_OPERSTATE_OFF; | |
872 | const char *operstate_str; | |
873 | int r; | |
874 | ||
875 | assert(m); | |
876 | assert(m->state_file); | |
877 | ||
878 | /* We add all NTP and DNS server to a set, to filter out duplicates */ | |
00616955 | 879 | dns = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
880 | if (!dns) |
881 | return -ENOMEM; | |
882 | ||
00616955 | 883 | ntp = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
884 | if (!ntp) |
885 | return -ENOMEM; | |
886 | ||
482d1aeb | 887 | search_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 LP |
888 | if (!search_domains) |
889 | return -ENOMEM; | |
890 | ||
482d1aeb | 891 | route_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 | 892 | if (!route_domains) |
84de38c5 TG |
893 | return -ENOMEM; |
894 | ||
895 | HASHMAP_FOREACH(link, m->links, i) { | |
896 | if (link->flags & IFF_LOOPBACK) | |
897 | continue; | |
898 | ||
899 | if (link->operstate > operstate) | |
900 | operstate = link->operstate; | |
901 | ||
902 | if (!link->network) | |
903 | continue; | |
904 | ||
905 | /* First add the static configured entries */ | |
5512a963 | 906 | r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns); |
84de38c5 TG |
907 | if (r < 0) |
908 | return r; | |
909 | ||
00616955 | 910 | r = ordered_set_put_strdupv(ntp, link->network->ntp); |
84de38c5 TG |
911 | if (r < 0) |
912 | return r; | |
913 | ||
00616955 | 914 | r = ordered_set_put_strdupv(search_domains, link->network->search_domains); |
3df9bec5 LP |
915 | if (r < 0) |
916 | return r; | |
917 | ||
00616955 | 918 | r = ordered_set_put_strdupv(route_domains, link->network->route_domains); |
84de38c5 TG |
919 | if (r < 0) |
920 | return r; | |
921 | ||
922 | if (!link->dhcp_lease) | |
923 | continue; | |
924 | ||
925 | /* Secondly, add the entries acquired via DHCP */ | |
27cb34f5 | 926 | if (link->network->dhcp_use_dns) { |
84de38c5 TG |
927 | const struct in_addr *addresses; |
928 | ||
929 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
930 | if (r > 0) { | |
5512a963 | 931 | r = ordered_set_put_in4_addrv(dns, addresses, r); |
84de38c5 TG |
932 | if (r < 0) |
933 | return r; | |
934 | } else if (r < 0 && r != -ENODATA) | |
935 | return r; | |
936 | } | |
937 | ||
27cb34f5 | 938 | if (link->network->dhcp_use_ntp) { |
84de38c5 TG |
939 | const struct in_addr *addresses; |
940 | ||
941 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
942 | if (r > 0) { | |
5512a963 | 943 | r = ordered_set_put_in4_addrv(ntp, addresses, r); |
84de38c5 TG |
944 | if (r < 0) |
945 | return r; | |
946 | } else if (r < 0 && r != -ENODATA) | |
947 | return r; | |
948 | } | |
949 | ||
b2a81c0b | 950 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
84de38c5 TG |
951 | const char *domainname; |
952 | ||
953 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); | |
954 | if (r >= 0) { | |
b2a81c0b LP |
955 | |
956 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) | |
957 | r = ordered_set_put_strdup(search_domains, domainname); | |
958 | else | |
959 | r = ordered_set_put_strdup(route_domains, domainname); | |
960 | ||
84de38c5 TG |
961 | if (r < 0) |
962 | return r; | |
963 | } else if (r != -ENODATA) | |
964 | return r; | |
965 | } | |
966 | } | |
967 | ||
968 | operstate_str = link_operstate_to_string(operstate); | |
969 | assert(operstate_str); | |
970 | ||
971 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
972 | if (r < 0) | |
973 | return r; | |
974 | ||
5512a963 | 975 | (void) fchmod(fileno(f), 0644); |
84de38c5 TG |
976 | |
977 | fprintf(f, | |
978 | "# This is private data. Do not parse.\n" | |
979 | "OPER_STATE=%s\n", operstate_str); | |
980 | ||
981 | print_string_set(f, "DNS=", dns); | |
982 | print_string_set(f, "NTP=", ntp); | |
3df9bec5 LP |
983 | print_string_set(f, "DOMAINS=", search_domains); |
984 | print_string_set(f, "ROUTE_DOMAINS=", route_domains); | |
84de38c5 TG |
985 | |
986 | r = fflush_and_check(f); | |
987 | if (r < 0) | |
988 | goto fail; | |
989 | ||
990 | if (rename(temp_path, m->state_file) < 0) { | |
991 | r = -errno; | |
992 | goto fail; | |
993 | } | |
994 | ||
995 | if (m->operational_state != operstate) { | |
996 | m->operational_state = operstate; | |
997 | r = manager_send_changed(m, "OperationalState", NULL); | |
998 | if (r < 0) | |
999 | log_error_errno(r, "Could not emit changed OperationalState: %m"); | |
1000 | } | |
1001 | ||
1002 | m->dirty = false; | |
1003 | ||
1004 | return 0; | |
1005 | ||
1006 | fail: | |
1007 | (void) unlink(m->state_file); | |
1008 | (void) unlink(temp_path); | |
1009 | ||
1010 | return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file); | |
1011 | } | |
1012 | ||
1013 | static int manager_dirty_handler(sd_event_source *s, void *userdata) { | |
1014 | Manager *m = userdata; | |
1015 | Link *link; | |
1016 | Iterator i; | |
1017 | int r; | |
1018 | ||
1019 | assert(m); | |
1020 | ||
1021 | if (m->dirty) | |
1022 | manager_save(m); | |
1023 | ||
1024 | SET_FOREACH(link, m->dirty_links, i) { | |
1025 | r = link_save(link); | |
1026 | if (r >= 0) | |
1027 | link_clean(link); | |
1028 | } | |
1029 | ||
1030 | return 1; | |
1031 | } | |
1032 | ||
5fae368b TG |
1033 | int manager_new(Manager **ret) { |
1034 | _cleanup_manager_free_ Manager *m = NULL; | |
45af44d4 | 1035 | int r; |
f579559b | 1036 | |
5fae368b TG |
1037 | m = new0(Manager, 1); |
1038 | if (!m) | |
1039 | return -ENOMEM; | |
45af44d4 | 1040 | |
5fae368b TG |
1041 | m->state_file = strdup("/run/systemd/netif/state"); |
1042 | if (!m->state_file) | |
1043 | return -ENOMEM; | |
1044 | ||
1045 | r = sd_event_default(&m->event); | |
45af44d4 TG |
1046 | if (r < 0) |
1047 | return r; | |
1048 | ||
5fae368b TG |
1049 | sd_event_set_watchdog(m->event, true); |
1050 | ||
1051 | sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); | |
1052 | sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
1053 | ||
84de38c5 TG |
1054 | r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m); |
1055 | if (r < 0) | |
1056 | return r; | |
1057 | ||
5fae368b | 1058 | r = manager_connect_rtnl(m); |
45af44d4 TG |
1059 | if (r < 0) |
1060 | return r; | |
1061 | ||
5fae368b TG |
1062 | r = manager_connect_udev(m); |
1063 | if (r < 0) | |
1064 | return r; | |
45af44d4 | 1065 | |
5fae368b TG |
1066 | m->netdevs = hashmap_new(&string_hash_ops); |
1067 | if (!m->netdevs) | |
1068 | return -ENOMEM; | |
f579559b | 1069 | |
5fae368b | 1070 | LIST_HEAD_INIT(m->networks); |
f579559b | 1071 | |
5fae368b TG |
1072 | r = setup_default_address_pool(m); |
1073 | if (r < 0) | |
1074 | return r; | |
f579559b | 1075 | |
8341a5c3 | 1076 | m->duid.type = DUID_TYPE_EN; |
413708d1 | 1077 | |
5fae368b TG |
1078 | *ret = m; |
1079 | m = NULL; | |
f579559b | 1080 | |
f579559b TG |
1081 | return 0; |
1082 | } | |
1083 | ||
5fae368b TG |
1084 | void manager_free(Manager *m) { |
1085 | Network *network; | |
1086 | NetDev *netdev; | |
1087 | Link *link; | |
1088 | AddressPool *pool; | |
f579559b | 1089 | |
5fae368b TG |
1090 | if (!m) |
1091 | return; | |
505f8da7 | 1092 | |
5fae368b | 1093 | free(m->state_file); |
505f8da7 | 1094 | |
5fae368b TG |
1095 | while ((link = hashmap_first(m->links))) |
1096 | link_unref(link); | |
1097 | hashmap_free(m->links); | |
f579559b | 1098 | |
5fae368b TG |
1099 | while ((network = m->networks)) |
1100 | network_free(network); | |
1101 | ||
dbffab87 TG |
1102 | hashmap_free(m->networks_by_name); |
1103 | ||
5fae368b TG |
1104 | while ((netdev = hashmap_first(m->netdevs))) |
1105 | netdev_unref(netdev); | |
1106 | hashmap_free(m->netdevs); | |
1107 | ||
1108 | while ((pool = m->address_pools)) | |
1109 | address_pool_free(pool); | |
1110 | ||
1c4baffc | 1111 | sd_netlink_unref(m->rtnl); |
2f5b4a77 | 1112 | sd_event_unref(m->event); |
5fae368b | 1113 | |
7d20d375 TG |
1114 | sd_event_source_unref(m->udev_event_source); |
1115 | udev_monitor_unref(m->udev_monitor); | |
1116 | udev_unref(m->udev); | |
1117 | ||
1118 | sd_bus_unref(m->bus); | |
1119 | sd_bus_slot_unref(m->prepare_for_sleep_slot); | |
1120 | sd_event_source_unref(m->bus_retry_event_source); | |
1121 | ||
5fae368b TG |
1122 | free(m); |
1123 | } | |
1124 | ||
a97dcc12 TG |
1125 | static bool manager_check_idle(void *userdata) { |
1126 | Manager *m = userdata; | |
1127 | Link *link; | |
1128 | Iterator i; | |
1129 | ||
1130 | assert(m); | |
1131 | ||
9dc907f9 LP |
1132 | /* Check whether we are idle now. The only case when we decide to be idle is when there's only a loopback |
1133 | * device around, for which we have no configuration, and which already left the PENDING state. In all other | |
1134 | * cases we are not idle. */ | |
1135 | ||
a97dcc12 | 1136 | HASHMAP_FOREACH(link, m->links, i) { |
9dc907f9 | 1137 | /* We are not woken on udev activity, so let's just wait for the pending udev event */ |
a97dcc12 TG |
1138 | if (link->state == LINK_STATE_PENDING) |
1139 | return false; | |
1140 | ||
9dc907f9 LP |
1141 | if ((link->flags & IFF_LOOPBACK) == 0) |
1142 | return false; | |
a97dcc12 | 1143 | |
9dc907f9 | 1144 | if (link->network) |
a97dcc12 TG |
1145 | return false; |
1146 | } | |
1147 | ||
1148 | return true; | |
1149 | } | |
1150 | ||
1151 | int manager_run(Manager *m) { | |
84de38c5 TG |
1152 | Link *link; |
1153 | Iterator i; | |
1154 | ||
a97dcc12 TG |
1155 | assert(m); |
1156 | ||
84de38c5 TG |
1157 | /* The dirty handler will deal with future serialization, but the first one |
1158 | must be done explicitly. */ | |
1159 | ||
1160 | manager_save(m); | |
1161 | ||
1162 | HASHMAP_FOREACH(link, m->links, i) | |
1163 | link_save(link); | |
1164 | ||
f806f64c TG |
1165 | if (m->bus) |
1166 | return bus_event_loop_with_idle( | |
1167 | m->event, | |
1168 | m->bus, | |
1169 | "org.freedesktop.network1", | |
1170 | DEFAULT_EXIT_USEC, | |
1171 | manager_check_idle, | |
1172 | m); | |
1173 | else | |
1174 | /* failed to connect to the bus, so we lose exit-on-idle logic, | |
1175 | this should not happen except if dbus is not around at all */ | |
1176 | return sd_event_loop(m->event); | |
a97dcc12 TG |
1177 | } |
1178 | ||
5fae368b TG |
1179 | int manager_load_config(Manager *m) { |
1180 | int r; | |
1181 | ||
1182 | /* update timestamp */ | |
1183 | paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true); | |
1184 | ||
1185 | r = netdev_load(m); | |
f579559b TG |
1186 | if (r < 0) |
1187 | return r; | |
1188 | ||
5fae368b | 1189 | r = network_load(m); |
9021bb9f TG |
1190 | if (r < 0) |
1191 | return r; | |
1192 | ||
f579559b TG |
1193 | return 0; |
1194 | } | |
f882c247 | 1195 | |
5fae368b TG |
1196 | bool manager_should_reload(Manager *m) { |
1197 | return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false); | |
1198 | } | |
1199 | ||
1200 | int manager_rtnl_enumerate_links(Manager *m) { | |
4afd3348 | 1201 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1202 | sd_netlink_message *link; |
f882c247 TG |
1203 | int r; |
1204 | ||
5da8149f | 1205 | assert(m); |
5fae368b | 1206 | assert(m->rtnl); |
5da8149f | 1207 | |
5fae368b | 1208 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
f882c247 TG |
1209 | if (r < 0) |
1210 | return r; | |
1211 | ||
1c4baffc | 1212 | r = sd_netlink_message_request_dump(req, true); |
dd3efc09 TG |
1213 | if (r < 0) |
1214 | return r; | |
1215 | ||
1c4baffc | 1216 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
f2236469 TG |
1217 | if (r < 0) |
1218 | return r; | |
1219 | ||
1c4baffc | 1220 | for (link = reply; link; link = sd_netlink_message_next(link)) { |
5fae368b | 1221 | int k; |
2e9f08ea | 1222 | |
6a24f148 TG |
1223 | m->enumerating = true; |
1224 | ||
5fae368b TG |
1225 | k = manager_rtnl_process_link(m->rtnl, link, m); |
1226 | if (k < 0) | |
1227 | r = k; | |
6a24f148 TG |
1228 | |
1229 | m->enumerating = false; | |
5fae368b | 1230 | } |
2e9f08ea | 1231 | |
5fae368b | 1232 | return r; |
f882c247 | 1233 | } |
3bef724f | 1234 | |
5fae368b | 1235 | int manager_rtnl_enumerate_addresses(Manager *m) { |
4afd3348 | 1236 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1237 | sd_netlink_message *addr; |
1346b1f0 TG |
1238 | int r; |
1239 | ||
5fae368b TG |
1240 | assert(m); |
1241 | assert(m->rtnl); | |
bcbca829 | 1242 | |
5fae368b TG |
1243 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); |
1244 | if (r < 0) | |
1245 | return r; | |
bcbca829 | 1246 | |
1c4baffc | 1247 | r = sd_netlink_message_request_dump(req, true); |
1346b1f0 TG |
1248 | if (r < 0) |
1249 | return r; | |
1250 | ||
1c4baffc | 1251 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
5fae368b TG |
1252 | if (r < 0) |
1253 | return r; | |
1254 | ||
1c4baffc | 1255 | for (addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
5fae368b TG |
1256 | int k; |
1257 | ||
6a24f148 TG |
1258 | m->enumerating = true; |
1259 | ||
200a0868 | 1260 | k = manager_rtnl_process_address(m->rtnl, addr, m); |
5fae368b TG |
1261 | if (k < 0) |
1262 | r = k; | |
6a24f148 TG |
1263 | |
1264 | m->enumerating = false; | |
5fae368b TG |
1265 | } |
1266 | ||
1267 | return r; | |
1346b1f0 TG |
1268 | } |
1269 | ||
1c8e710c | 1270 | int manager_rtnl_enumerate_routes(Manager *m) { |
4afd3348 | 1271 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c8e710c TG |
1272 | sd_netlink_message *route; |
1273 | int r; | |
1274 | ||
1275 | assert(m); | |
1276 | assert(m->rtnl); | |
1277 | ||
1278 | r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0); | |
1279 | if (r < 0) | |
1280 | return r; | |
1281 | ||
1282 | r = sd_netlink_message_request_dump(req, true); | |
1283 | if (r < 0) | |
1284 | return r; | |
1285 | ||
1286 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
1287 | if (r < 0) | |
1288 | return r; | |
1289 | ||
1290 | for (route = reply; route; route = sd_netlink_message_next(route)) { | |
1291 | int k; | |
1292 | ||
1293 | m->enumerating = true; | |
1294 | ||
1295 | k = manager_rtnl_process_route(m->rtnl, route, m); | |
1296 | if (k < 0) | |
1297 | r = k; | |
1298 | ||
1299 | m->enumerating = false; | |
1300 | } | |
1301 | ||
1302 | return r; | |
1303 | } | |
1304 | ||
0dd25fb9 | 1305 | int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) { |
11bf3cce LP |
1306 | AddressPool *p; |
1307 | int r; | |
1308 | ||
1309 | assert(m); | |
1310 | assert(prefixlen > 0); | |
1311 | assert(found); | |
1312 | ||
1313 | LIST_FOREACH(address_pools, p, m->address_pools) { | |
1314 | if (p->family != family) | |
1315 | continue; | |
1316 | ||
1317 | r = address_pool_acquire(p, prefixlen, found); | |
1318 | if (r != 0) | |
1319 | return r; | |
1320 | } | |
1321 | ||
1322 | return 0; | |
1323 | } | |
4f5f911e LP |
1324 | |
1325 | Link* manager_find_uplink(Manager *m, Link *exclude) { | |
1326 | _cleanup_free_ struct local_address *gateways = NULL; | |
1327 | int n, i; | |
1328 | ||
1329 | assert(m); | |
1330 | ||
1331 | /* Looks for a suitable "uplink", via black magic: an | |
1332 | * interface that is up and where the default route with the | |
1333 | * highest priority points to. */ | |
1334 | ||
1335 | n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways); | |
1336 | if (n < 0) { | |
1337 | log_warning_errno(n, "Failed to determine list of default gateways: %m"); | |
1338 | return NULL; | |
1339 | } | |
1340 | ||
1341 | for (i = 0; i < n; i++) { | |
1342 | Link *link; | |
1343 | ||
1344 | link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex)); | |
1345 | if (!link) { | |
c2c940bd | 1346 | log_debug("Weird, found a gateway for a link we don't know. Ignoring."); |
4f5f911e LP |
1347 | continue; |
1348 | } | |
1349 | ||
1350 | if (link == exclude) | |
1351 | continue; | |
1352 | ||
1353 | if (link->operstate < LINK_OPERSTATE_ROUTABLE) | |
1354 | continue; | |
1355 | ||
1356 | return link; | |
1357 | } | |
1358 | ||
1359 | return NULL; | |
1360 | } | |
84de38c5 TG |
1361 | |
1362 | void manager_dirty(Manager *manager) { | |
1363 | assert(manager); | |
1364 | ||
1365 | /* the serialized state in /run is no longer up-to-date */ | |
1366 | manager->dirty = true; | |
1367 | } | |
59eb33e0 MP |
1368 | |
1369 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
1370 | Manager *manager = userdata; | |
1371 | const sd_bus_error *e; | |
1372 | ||
1373 | assert(m); | |
1374 | assert(manager); | |
1375 | ||
1376 | e = sd_bus_message_get_error(m); | |
1377 | if (e) | |
1378 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message); | |
1379 | ||
1380 | return 1; | |
1381 | } | |
1382 | ||
1383 | int manager_set_hostname(Manager *m, const char *hostname) { | |
1384 | int r; | |
1385 | ||
1386 | log_debug("Setting transient hostname: '%s'", strna(hostname)); | |
1387 | ||
1388 | if (!m->bus) { | |
1389 | /* TODO: replace by assert when we can rely on kdbus */ | |
1390 | log_info("Not connected to system bus, ignoring transient hostname."); | |
1391 | return 0; | |
1392 | } | |
1393 | ||
1394 | r = sd_bus_call_method_async( | |
1395 | m->bus, | |
1396 | NULL, | |
1397 | "org.freedesktop.hostname1", | |
1398 | "/org/freedesktop/hostname1", | |
1399 | "org.freedesktop.hostname1", | |
1400 | "SetHostname", | |
1401 | set_hostname_handler, | |
1402 | m, | |
1403 | "sb", | |
1404 | hostname, | |
1405 | false); | |
1406 | ||
1407 | if (r < 0) | |
1408 | return log_error_errno(r, "Could not set transient hostname: %m"); | |
1409 | ||
1410 | return 0; | |
1411 | } | |
1412 | ||
1413 | static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
1414 | Manager *manager = userdata; | |
1415 | const sd_bus_error *e; | |
1416 | ||
1417 | assert(m); | |
1418 | assert(manager); | |
1419 | ||
1420 | e = sd_bus_message_get_error(m); | |
1421 | if (e) | |
1422 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message); | |
1423 | ||
1424 | return 1; | |
1425 | } | |
1426 | ||
1427 | int manager_set_timezone(Manager *m, const char *tz) { | |
1428 | int r; | |
1429 | ||
1430 | assert(m); | |
1431 | assert(tz); | |
1432 | ||
1433 | log_debug("Setting system timezone: '%s'", tz); | |
1434 | ||
1435 | if (!m->bus) { | |
1436 | log_info("Not connected to system bus, ignoring timezone."); | |
1437 | return 0; | |
1438 | } | |
1439 | ||
1440 | r = sd_bus_call_method_async( | |
1441 | m->bus, | |
1442 | NULL, | |
1443 | "org.freedesktop.timedate1", | |
1444 | "/org/freedesktop/timedate1", | |
1445 | "org.freedesktop.timedate1", | |
1446 | "SetTimezone", | |
1447 | set_timezone_handler, | |
1448 | m, | |
1449 | "sb", | |
1450 | tz, | |
1451 | false); | |
1452 | if (r < 0) | |
1453 | return log_error_errno(r, "Could not set timezone: %m"); | |
1454 | ||
1455 | return 0; | |
1456 | } |