]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b TG |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
f579559b TG |
6 | ***/ |
7 | ||
091a364c | 8 | #include <sys/socket.h> |
bbf7c048 | 9 | #include <linux/if.h> |
bce67bbe | 10 | #include <linux/fib_rules.h> |
0d536673 | 11 | #include <stdio_ext.h> |
3bef724f | 12 | |
fc2f9534 | 13 | #include "sd-daemon.h" |
07630cea | 14 | #include "sd-netlink.h" |
fc2f9534 | 15 | |
b5efdb8a | 16 | #include "alloc-util.h" |
a97dcc12 | 17 | #include "bus-util.h" |
07630cea | 18 | #include "conf-parser.h" |
a97dcc12 | 19 | #include "def.h" |
482d1aeb | 20 | #include "dns-domain.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
0d39fa9c | 22 | #include "fileio.h" |
07630cea | 23 | #include "libudev-private.h" |
4f5f911e | 24 | #include "local-addresses.h" |
07630cea | 25 | #include "netlink-util.h" |
23f53b99 | 26 | #include "networkd-manager.h" |
00616955 | 27 | #include "ordered-set.h" |
07630cea LP |
28 | #include "path-util.h" |
29 | #include "set.h" | |
30 | #include "udev-util.h" | |
31 | #include "virt.h" | |
505f8da7 | 32 | |
be660c37 AR |
33 | /* use 8 MB for receive socket kernel queue. */ |
34 | #define RCVBUF_SIZE (8*1024*1024) | |
35 | ||
2ad8416d ZJS |
36 | const char* const network_dirs[] = { |
37 | "/etc/systemd/network", | |
38 | "/run/systemd/network", | |
39 | "/usr/lib/systemd/network", | |
349cc4a5 | 40 | #if HAVE_SPLIT_USR |
2ad8416d ZJS |
41 | "/lib/systemd/network", |
42 | #endif | |
43 | NULL}; | |
44 | ||
11bf3cce LP |
45 | static int setup_default_address_pool(Manager *m) { |
46 | AddressPool *p; | |
47 | int r; | |
48 | ||
49 | assert(m); | |
50 | ||
51 | /* Add in the well-known private address ranges. */ | |
52 | ||
53 | r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7); | |
54 | if (r < 0) | |
55 | return r; | |
56 | ||
57 | r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16); | |
58 | if (r < 0) | |
59 | return r; | |
60 | ||
61 | r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12); | |
62 | if (r < 0) | |
63 | return r; | |
64 | ||
65 | r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8); | |
66 | if (r < 0) | |
67 | return r; | |
68 | ||
69 | return 0; | |
70 | } | |
71 | ||
9c0a72f9 TG |
72 | static int manager_reset_all(Manager *m) { |
73 | Link *link; | |
74 | Iterator i; | |
75 | int r; | |
76 | ||
77 | assert(m); | |
78 | ||
79 | HASHMAP_FOREACH(link, m->links, i) { | |
80 | r = link_carrier_reset(link); | |
81 | if (r < 0) | |
2f3cf1f9 | 82 | log_link_warning_errno(link, r, "Could not reset carrier: %m"); |
9c0a72f9 TG |
83 | } |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
19070062 | 88 | static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
9c0a72f9 TG |
89 | Manager *m = userdata; |
90 | int b, r; | |
91 | ||
19070062 | 92 | assert(message); |
d7afd945 | 93 | assert(m); |
9c0a72f9 TG |
94 | |
95 | r = sd_bus_message_read(message, "b", &b); | |
96 | if (r < 0) { | |
97 | log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m"); | |
98 | return 0; | |
99 | } | |
100 | ||
101 | if (b) | |
102 | return 0; | |
103 | ||
104 | log_debug("Coming back from suspend, resetting all connections..."); | |
105 | ||
e1694a75 | 106 | (void) manager_reset_all(m); |
9c0a72f9 TG |
107 | |
108 | return 0; | |
109 | } | |
110 | ||
d7afd945 LP |
111 | static int on_connected(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
112 | Manager *m = userdata; | |
9c0a72f9 | 113 | |
d7afd945 | 114 | assert(message); |
9c0a72f9 TG |
115 | assert(m); |
116 | ||
d7afd945 LP |
117 | /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */ |
118 | if (m->dynamic_hostname) | |
119 | (void) manager_set_hostname(m, m->dynamic_hostname); | |
120 | if (m->dynamic_timezone) | |
121 | (void) manager_set_timezone(m, m->dynamic_timezone); | |
9c0a72f9 | 122 | |
d7afd945 LP |
123 | return 0; |
124 | } | |
9c0a72f9 | 125 | |
d7afd945 LP |
126 | int manager_connect_bus(Manager *m) { |
127 | int r; | |
128 | ||
129 | assert(m); | |
9c0a72f9 | 130 | |
d7afd945 | 131 | if (m->bus) |
9c0a72f9 | 132 | return 0; |
7d6884b6 | 133 | |
621e4509 | 134 | r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-network"); |
9c0a72f9 | 135 | if (r < 0) |
d7afd945 | 136 | return log_error_errno(r, "Failed to connect to bus: %m"); |
9c0a72f9 | 137 | |
e331e246 TG |
138 | r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m); |
139 | if (r < 0) | |
140 | return log_error_errno(r, "Failed to add manager object vtable: %m"); | |
141 | ||
142 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m); | |
143 | if (r < 0) | |
144 | return log_error_errno(r, "Failed to add link object vtable: %m"); | |
145 | ||
146 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m); | |
147 | if (r < 0) | |
148 | return log_error_errno(r, "Failed to add link enumerator: %m"); | |
3175fcde TG |
149 | |
150 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m); | |
151 | if (r < 0) | |
152 | return log_error_errno(r, "Failed to add network object vtable: %m"); | |
153 | ||
154 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m); | |
155 | if (r < 0) | |
156 | return log_error_errno(r, "Failed to add network enumerator: %m"); | |
e331e246 | 157 | |
d4e9e574 | 158 | r = bus_request_name_async_may_reload_dbus(m->bus, NULL, "org.freedesktop.network1", 0, NULL); |
e331e246 | 159 | if (r < 0) |
0c0b9306 | 160 | return log_error_errno(r, "Failed to request name: %m"); |
e331e246 TG |
161 | |
162 | r = sd_bus_attach_event(m->bus, m->event, 0); | |
163 | if (r < 0) | |
164 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
165 | ||
d7afd945 LP |
166 | r = sd_bus_match_signal_async( |
167 | m->bus, | |
168 | &m->connected_slot, | |
169 | "org.freedesktop.DBus.Local", | |
170 | NULL, | |
171 | "org.freedesktop.DBus.Local", | |
172 | "Connected", | |
173 | on_connected, NULL, m); | |
174 | if (r < 0) | |
175 | return log_error_errno(r, "Failed to request match on Connected signal: %m"); | |
176 | ||
177 | r = sd_bus_match_signal_async( | |
178 | m->bus, | |
179 | &m->prepare_for_sleep_slot, | |
180 | "org.freedesktop.login1", | |
181 | "/org/freedesktop/login1", | |
182 | "org.freedesktop.login1.Manager", | |
183 | "PrepareForSleep", | |
184 | match_prepare_for_sleep, NULL, m); | |
185 | if (r < 0) | |
186 | log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m"); | |
7901cea1 | 187 | |
9c0a72f9 TG |
188 | return 0; |
189 | } | |
190 | ||
5fae368b TG |
191 | static int manager_udev_process_link(Manager *m, struct udev_device *device) { |
192 | Link *link = NULL; | |
193 | int r, ifindex; | |
5544ee85 | 194 | |
5fae368b TG |
195 | assert(m); |
196 | assert(device); | |
5544ee85 | 197 | |
5fae368b TG |
198 | if (!streq_ptr(udev_device_get_action(device), "add")) |
199 | return 0; | |
5544ee85 | 200 | |
5fae368b TG |
201 | ifindex = udev_device_get_ifindex(device); |
202 | if (ifindex <= 0) { | |
2f3cf1f9 | 203 | log_debug("Ignoring udev ADD event for device with invalid ifindex"); |
5fae368b | 204 | return 0; |
5544ee85 TG |
205 | } |
206 | ||
5fae368b TG |
207 | r = link_get(m, ifindex, &link); |
208 | if (r == -ENODEV) | |
209 | return 0; | |
210 | else if (r < 0) | |
f579559b TG |
211 | return r; |
212 | ||
5fae368b | 213 | r = link_initialized(link, device); |
44de0efc LP |
214 | if (r < 0) |
215 | return r; | |
216 | ||
5fae368b TG |
217 | return 0; |
218 | } | |
be660c37 | 219 | |
5fae368b TG |
220 | static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
221 | Manager *m = userdata; | |
222 | struct udev_monitor *monitor = m->udev_monitor; | |
8e766630 | 223 | _cleanup_(udev_device_unrefp) struct udev_device *device = NULL; |
f579559b | 224 | |
5fae368b TG |
225 | device = udev_monitor_receive_device(monitor); |
226 | if (!device) | |
02b59d57 TG |
227 | return -ENOMEM; |
228 | ||
e1694a75 YW |
229 | (void) manager_udev_process_link(m, device); |
230 | ||
f579559b TG |
231 | return 0; |
232 | } | |
233 | ||
5fae368b TG |
234 | static int manager_connect_udev(Manager *m) { |
235 | int r; | |
f579559b | 236 | |
5fae368b TG |
237 | /* udev does not initialize devices inside containers, |
238 | * so we rely on them being already initialized before | |
239 | * entering the container */ | |
75f86906 | 240 | if (detect_container() > 0) |
5fae368b | 241 | return 0; |
f579559b | 242 | |
5fae368b TG |
243 | m->udev = udev_new(); |
244 | if (!m->udev) | |
245 | return -ENOMEM; | |
02b59d57 | 246 | |
5fae368b TG |
247 | m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
248 | if (!m->udev_monitor) | |
249 | return -ENOMEM; | |
02b59d57 | 250 | |
5fae368b | 251 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL); |
02b59d57 | 252 | if (r < 0) |
5fae368b | 253 | return log_error_errno(r, "Could not add udev monitor filter: %m"); |
02b59d57 | 254 | |
5fae368b TG |
255 | r = udev_monitor_enable_receiving(m->udev_monitor); |
256 | if (r < 0) { | |
257 | log_error("Could not enable udev monitor"); | |
02b59d57 | 258 | return r; |
667fcc6d | 259 | } |
505f8da7 | 260 | |
5fae368b TG |
261 | r = sd_event_add_io(m->event, |
262 | &m->udev_event_source, | |
263 | udev_monitor_get_fd(m->udev_monitor), | |
264 | EPOLLIN, manager_dispatch_link_udev, | |
265 | m); | |
266 | if (r < 0) | |
667fcc6d | 267 | return r; |
505f8da7 | 268 | |
5fae368b | 269 | r = sd_event_source_set_description(m->udev_event_source, "networkd-udev"); |
505f8da7 TG |
270 | if (r < 0) |
271 | return r; | |
11a7f229 | 272 | |
505f8da7 TG |
273 | return 0; |
274 | } | |
f579559b | 275 | |
1c8e710c TG |
276 | int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
277 | Manager *m = userdata; | |
278 | Link *link = NULL; | |
279 | uint16_t type; | |
280 | uint32_t ifindex, priority = 0; | |
983226f3 | 281 | unsigned char protocol, scope, tos, table, rt_type; |
1c8e710c TG |
282 | int family; |
283 | unsigned char dst_prefixlen, src_prefixlen; | |
284 | union in_addr_union dst = {}, gw = {}, src = {}, prefsrc = {}; | |
285 | Route *route = NULL; | |
286 | int r; | |
287 | ||
288 | assert(rtnl); | |
289 | assert(message); | |
290 | assert(m); | |
291 | ||
292 | if (sd_netlink_message_is_error(message)) { | |
293 | r = sd_netlink_message_get_errno(message); | |
294 | if (r < 0) | |
e1694a75 | 295 | log_warning_errno(r, "rtnl: failed to receive route, ignoring: %m"); |
1c8e710c TG |
296 | |
297 | return 0; | |
298 | } | |
299 | ||
300 | r = sd_netlink_message_get_type(message, &type); | |
301 | if (r < 0) { | |
e1694a75 | 302 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
1c8e710c | 303 | return 0; |
25a1bffc | 304 | } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) { |
e1694a75 | 305 | log_warning("rtnl: received unexpected message type when processing route, ignoring"); |
1c8e710c TG |
306 | return 0; |
307 | } | |
308 | ||
309 | r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex); | |
310 | if (r == -ENODATA) { | |
311 | log_debug("rtnl: received route without ifindex, ignoring"); | |
312 | return 0; | |
313 | } else if (r < 0) { | |
314 | log_warning_errno(r, "rtnl: could not get ifindex from route, ignoring: %m"); | |
315 | return 0; | |
316 | } else if (ifindex <= 0) { | |
317 | log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex); | |
318 | return 0; | |
319 | } else { | |
320 | r = link_get(m, ifindex, &link); | |
321 | if (r < 0 || !link) { | |
322 | /* when enumerating we might be out of sync, but we will | |
323 | * get the route again, so just ignore it */ | |
324 | if (!m->enumerating) | |
325 | log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex); | |
326 | return 0; | |
327 | } | |
328 | } | |
329 | ||
330 | r = sd_rtnl_message_route_get_family(message, &family); | |
331 | if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) { | |
e1694a75 | 332 | log_link_warning(link, "rtnl: received address with invalid family, ignoring"); |
1c8e710c TG |
333 | return 0; |
334 | } | |
335 | ||
336 | r = sd_rtnl_message_route_get_protocol(message, &protocol); | |
337 | if (r < 0) { | |
338 | log_warning_errno(r, "rtnl: could not get route protocol: %m"); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | switch (family) { | |
343 | case AF_INET: | |
344 | r = sd_netlink_message_read_in_addr(message, RTA_DST, &dst.in); | |
345 | if (r < 0 && r != -ENODATA) { | |
346 | log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m"); | |
347 | return 0; | |
348 | } | |
349 | ||
350 | r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &gw.in); | |
351 | if (r < 0 && r != -ENODATA) { | |
352 | log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m"); | |
353 | return 0; | |
354 | } | |
355 | ||
356 | r = sd_netlink_message_read_in_addr(message, RTA_SRC, &src.in); | |
357 | if (r < 0 && r != -ENODATA) { | |
358 | log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m"); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &prefsrc.in); | |
363 | if (r < 0 && r != -ENODATA) { | |
364 | log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m"); | |
365 | return 0; | |
366 | } | |
367 | ||
368 | break; | |
369 | ||
370 | case AF_INET6: | |
371 | r = sd_netlink_message_read_in6_addr(message, RTA_DST, &dst.in6); | |
372 | if (r < 0 && r != -ENODATA) { | |
373 | log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m"); | |
374 | return 0; | |
375 | } | |
376 | ||
377 | r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &gw.in6); | |
378 | if (r < 0 && r != -ENODATA) { | |
379 | log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m"); | |
380 | return 0; | |
381 | } | |
382 | ||
383 | r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &src.in6); | |
384 | if (r < 0 && r != -ENODATA) { | |
385 | log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m"); | |
386 | return 0; | |
387 | } | |
388 | ||
389 | r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &prefsrc.in6); | |
390 | if (r < 0 && r != -ENODATA) { | |
391 | log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m"); | |
392 | return 0; | |
393 | } | |
394 | ||
395 | break; | |
396 | ||
397 | default: | |
74dead34 | 398 | assert_not_reached("Received unsupported address family"); |
1c8e710c TG |
399 | return 0; |
400 | } | |
401 | ||
402 | r = sd_rtnl_message_route_get_dst_prefixlen(message, &dst_prefixlen); | |
403 | if (r < 0) { | |
404 | log_link_warning_errno(link, r, "rtnl: received route with invalid destination prefixlen, ignoring: %m"); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | r = sd_rtnl_message_route_get_src_prefixlen(message, &src_prefixlen); | |
409 | if (r < 0) { | |
410 | log_link_warning_errno(link, r, "rtnl: received route with invalid source prefixlen, ignoring: %m"); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | r = sd_rtnl_message_route_get_scope(message, &scope); | |
415 | if (r < 0) { | |
416 | log_link_warning_errno(link, r, "rtnl: received route with invalid scope, ignoring: %m"); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | r = sd_rtnl_message_route_get_tos(message, &tos); | |
421 | if (r < 0) { | |
422 | log_link_warning_errno(link, r, "rtnl: received route with invalid tos, ignoring: %m"); | |
423 | return 0; | |
424 | } | |
425 | ||
983226f3 SS |
426 | r = sd_rtnl_message_route_get_type(message, &rt_type); |
427 | if (r < 0) { | |
428 | log_link_warning_errno(link, r, "rtnl: received route with invalid type, ignoring: %m"); | |
429 | return 0; | |
430 | } | |
431 | ||
1c8e710c TG |
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 | ||
e1694a75 | 444 | (void) route_get(link, family, &dst, dst_prefixlen, tos, priority, table, &route); |
1c8e710c TG |
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); | |
e1694a75 YW |
451 | if (r < 0) { |
452 | log_link_warning_errno(link, r, "Failed to add route, ignoring: %m"); | |
1c8e710c | 453 | return 0; |
e1694a75 | 454 | } |
1c8e710c TG |
455 | } |
456 | ||
bbd15900 | 457 | route_update(route, &src, src_prefixlen, &gw, &prefsrc, scope, protocol, rt_type); |
1c8e710c TG |
458 | |
459 | break; | |
460 | ||
461 | case RTM_DELROUTE: | |
b9642f41 | 462 | route_free(route); |
1c8e710c | 463 | break; |
b9642f41 | 464 | |
1c8e710c TG |
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) | |
e1694a75 | 494 | log_warning_errno(r, "rtnl: failed to receive address, ignoring: %m"); |
200a0868 TG |
495 | |
496 | return 0; | |
497 | } | |
498 | ||
499 | r = sd_netlink_message_get_type(message, &type); | |
500 | if (r < 0) { | |
e1694a75 | 501 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
200a0868 | 502 | return 0; |
25a1bffc | 503 | } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) { |
e1694a75 | 504 | log_warning("rtnl: received unexpected message type when processing address, ignoring"); |
200a0868 TG |
505 | return 0; |
506 | } | |
507 | ||
508 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
509 | if (r < 0) { | |
e1694a75 | 510 | log_warning_errno(r, "rtnl: could not get ifindex from address, ignoring: %m"); |
200a0868 TG |
511 | return 0; |
512 | } else if (ifindex <= 0) { | |
e1694a75 | 513 | log_warning("rtnl: received address message with invalid ifindex, ignoring: %d", ifindex); |
200a0868 TG |
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)) { | |
e1694a75 | 528 | log_link_warning(link, "rtnl: received address with invalid family, ignoring"); |
200a0868 TG |
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: | |
e1694a75 | 570 | assert_not_reached("Received unsupported address family"); |
200a0868 TG |
571 | } |
572 | ||
054f0db4 | 573 | if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) { |
e1694a75 | 574 | log_link_warning(link, "Could not print address, ignoring"); |
200a0868 TG |
575 | return 0; |
576 | } | |
577 | ||
054f0db4 | 578 | r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo); |
e1694a75 YW |
579 | if (r < 0 && r != -ENODATA) { |
580 | log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m"); | |
581 | return 0; | |
582 | } else if (r >= 0) { | |
4058d339 | 583 | if (cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME) |
200a0868 | 584 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, |
054f0db4 | 585 | cinfo.ifa_valid * USEC_PER_SEC, |
200a0868 TG |
586 | USEC_PER_SEC); |
587 | } | |
588 | ||
e1694a75 | 589 | (void) address_get(link, family, &in_addr, prefixlen, &address); |
200a0868 TG |
590 | |
591 | switch (type) { | |
592 | case RTM_NEWADDR: | |
36c32f61 | 593 | if (address) |
4058d339 | 594 | log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 595 | valid_str ? "for " : "forever", strempty(valid_str)); |
36c32f61 | 596 | else { |
adda1ed9 TG |
597 | /* An address appeared that we did not request */ |
598 | r = address_add_foreign(link, family, &in_addr, prefixlen, &address); | |
cf1d700d | 599 | if (r < 0) { |
e1694a75 | 600 | log_link_warning_errno(link, r, "Failed to add address %s/%u, ignoring: %m", buf, prefixlen); |
cf1d700d TG |
601 | return 0; |
602 | } else | |
4058d339 | 603 | log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 604 | valid_str ? "for " : "forever", strempty(valid_str)); |
200a0868 TG |
605 | } |
606 | ||
e1694a75 YW |
607 | r = address_update(address, flags, scope, &cinfo); |
608 | if (r < 0) { | |
609 | log_link_warning_errno(link, r, "Failed to update address %s/%u, ignoring: %m", buf, prefixlen); | |
610 | return 0; | |
611 | } | |
36c32f61 | 612 | |
200a0868 TG |
613 | break; |
614 | ||
615 | case RTM_DELADDR: | |
616 | ||
054f0db4 | 617 | if (address) { |
4058d339 | 618 | log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 619 | valid_str ? "for " : "forever", strempty(valid_str)); |
e1694a75 | 620 | (void) address_drop(address); |
200a0868 | 621 | } else |
e1694a75 | 622 | log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s), ignoring", buf, prefixlen, |
87e4e28d | 623 | valid_str ? "for " : "forever", strempty(valid_str)); |
200a0868 TG |
624 | |
625 | break; | |
626 | default: | |
627 | assert_not_reached("Received invalid RTNL message type"); | |
628 | } | |
629 | ||
630 | return 1; | |
631 | } | |
632 | ||
1c4baffc | 633 | static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
505f8da7 TG |
634 | Manager *m = userdata; |
635 | Link *link = NULL; | |
4d473d5d | 636 | NetDev *netdev = NULL; |
f2236469 | 637 | uint16_t type; |
ca4e095a | 638 | const char *name; |
505f8da7 | 639 | int r, ifindex; |
f579559b | 640 | |
505f8da7 TG |
641 | assert(rtnl); |
642 | assert(message); | |
f579559b TG |
643 | assert(m); |
644 | ||
1c4baffc TG |
645 | if (sd_netlink_message_is_error(message)) { |
646 | r = sd_netlink_message_get_errno(message); | |
45af44d4 | 647 | if (r < 0) |
e1694a75 | 648 | log_warning_errno(r, "rtnl: Could not receive link, ignoring: %m"); |
45af44d4 TG |
649 | |
650 | return 0; | |
651 | } | |
652 | ||
1c4baffc | 653 | r = sd_netlink_message_get_type(message, &type); |
f2236469 | 654 | if (r < 0) { |
e1694a75 | 655 | log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m"); |
f2236469 | 656 | return 0; |
25a1bffc | 657 | } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) { |
e1694a75 | 658 | log_warning("rtnl: Received unexpected message type when processing link, ignoring"); |
cdfee943 | 659 | return 0; |
f2236469 TG |
660 | } |
661 | ||
505f8da7 | 662 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
45af44d4 | 663 | if (r < 0) { |
e1694a75 | 664 | log_warning_errno(r, "rtnl: Could not get ifindex from link, ignoring: %m"); |
45af44d4 TG |
665 | return 0; |
666 | } else if (ifindex <= 0) { | |
e1694a75 | 667 | log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex); |
505f8da7 | 668 | return 0; |
a0db8e46 | 669 | } |
f579559b | 670 | |
1c4baffc | 671 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name); |
45af44d4 | 672 | if (r < 0) { |
e1694a75 | 673 | log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m"); |
4d473d5d | 674 | return 0; |
a0db8e46 ZJS |
675 | } |
676 | ||
677 | (void) link_get(m, ifindex, &link); | |
678 | (void) netdev_get(m, name, &netdev); | |
4d473d5d TG |
679 | |
680 | switch (type) { | |
681 | case RTM_NEWLINK: | |
682 | if (!link) { | |
683 | /* link is new, so add it */ | |
684 | r = link_add(m, message, &link); | |
685 | if (r < 0) { | |
e1694a75 | 686 | log_warning_errno(r, "Could not add new link, ignoring: %m"); |
4d473d5d TG |
687 | return 0; |
688 | } | |
689 | } | |
690 | ||
691 | if (netdev) { | |
692 | /* netdev exists, so make sure the ifindex matches */ | |
505f8da7 TG |
693 | r = netdev_set_ifindex(netdev, message); |
694 | if (r < 0) { | |
e1694a75 | 695 | log_warning_errno(r, "Could not set ifindex on netdev, ignoring: %m"); |
505f8da7 TG |
696 | return 0; |
697 | } | |
698 | } | |
e1202047 | 699 | |
f2236469 | 700 | r = link_update(link, message); |
e1694a75 YW |
701 | if (r < 0) { |
702 | log_warning_errno(r, "Could not update link, ignoring: %m"); | |
f2236469 | 703 | return 0; |
e1694a75 | 704 | } |
4d473d5d TG |
705 | |
706 | break; | |
707 | ||
708 | case RTM_DELLINK: | |
709 | link_drop(link); | |
710 | netdev_drop(netdev); | |
711 | ||
712 | break; | |
713 | ||
714 | default: | |
715 | assert_not_reached("Received invalid RTNL message type."); | |
f2236469 | 716 | } |
505f8da7 TG |
717 | |
718 | return 1; | |
719 | } | |
720 | ||
bce67bbe SS |
721 | int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
722 | uint8_t tos = 0, to_prefixlen = 0, from_prefixlen = 0; | |
36e6e28b | 723 | union in_addr_union to = {}, from = {}; |
bce67bbe | 724 | RoutingPolicyRule *rule = NULL; |
bce67bbe | 725 | uint32_t fwmark = 0, table = 0; |
36e6e28b | 726 | char *iif = NULL, *oif = NULL; |
bce67bbe SS |
727 | Manager *m = userdata; |
728 | uint16_t type; | |
729 | int family; | |
730 | int r; | |
731 | ||
732 | assert(rtnl); | |
733 | assert(message); | |
734 | assert(m); | |
735 | ||
736 | if (sd_netlink_message_is_error(message)) { | |
737 | r = sd_netlink_message_get_errno(message); | |
738 | if (r < 0) | |
e1694a75 | 739 | log_warning_errno(r, "rtnl: failed to receive rule, ignoring: %m"); |
bce67bbe SS |
740 | |
741 | return 0; | |
742 | } | |
743 | ||
744 | r = sd_netlink_message_get_type(message, &type); | |
745 | if (r < 0) { | |
e1694a75 | 746 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
bce67bbe SS |
747 | return 0; |
748 | } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) { | |
e1694a75 | 749 | log_warning("rtnl: received unexpected message type '%u' when processing rule, ignoring", type); |
bce67bbe SS |
750 | return 0; |
751 | } | |
752 | ||
753 | r = sd_rtnl_message_get_family(message, &family); | |
615ded62 | 754 | if (r < 0) { |
e1694a75 | 755 | log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m"); |
615ded62 YW |
756 | return 0; |
757 | } else if (!IN_SET(family, AF_INET, AF_INET6)) { | |
e1694a75 | 758 | log_debug("rtnl: received address with invalid family %u, ignoring", family); |
bce67bbe SS |
759 | return 0; |
760 | } | |
761 | ||
762 | switch (family) { | |
763 | case AF_INET: | |
764 | r = sd_netlink_message_read_in_addr(message, FRA_SRC, &from.in); | |
e1694a75 YW |
765 | if (r < 0 && r != -ENODATA) { |
766 | log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); | |
767 | return 0; | |
768 | } else if (r >= 0) { | |
bce67bbe | 769 | r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen); |
e1694a75 YW |
770 | if (r < 0) { |
771 | log_warning_errno(r, "rtnl: failed to retrive rule from prefix length, ignoring: %m"); | |
772 | return 0; | |
773 | } | |
bce67bbe SS |
774 | } |
775 | ||
776 | r = sd_netlink_message_read_in_addr(message, FRA_DST, &to.in); | |
e1694a75 YW |
777 | if (r < 0 && r != -ENODATA) { |
778 | log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); | |
779 | return 0; | |
780 | } else if (r >= 0) { | |
bce67bbe | 781 | r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen); |
e1694a75 YW |
782 | if (r < 0) { |
783 | log_warning_errno(r, "rtnl: failed to retrive rule to prefix length, ignoring: %m"); | |
784 | return 0; | |
785 | } | |
bce67bbe SS |
786 | } |
787 | ||
788 | break; | |
789 | ||
790 | case AF_INET6: | |
791 | r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &from.in6); | |
e1694a75 YW |
792 | if (r < 0 && r != -ENODATA) { |
793 | log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); | |
794 | return 0; | |
795 | } else if (r >= 0) { | |
bce67bbe | 796 | r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen); |
e1694a75 YW |
797 | if (r < 0) { |
798 | log_warning_errno(r, "rtnl: failed to retrive rule from prefix length, ignoring: %m"); | |
799 | return 0; | |
800 | } | |
bce67bbe SS |
801 | } |
802 | ||
803 | r = sd_netlink_message_read_in6_addr(message, FRA_DST, &to.in6); | |
e1694a75 YW |
804 | if (r < 0 && r != -ENODATA) { |
805 | log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); | |
806 | return 0; | |
807 | } else if (r >= 0) { | |
bce67bbe | 808 | r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen); |
e1694a75 YW |
809 | if (r < 0) { |
810 | log_warning_errno(r, "rtnl: failed to retrive rule to prefix length, ignoring: %m"); | |
811 | return 0; | |
812 | } | |
bce67bbe SS |
813 | } |
814 | ||
815 | break; | |
816 | ||
817 | default: | |
74dead34 | 818 | assert_not_reached("Received unsupported address family"); |
bce67bbe SS |
819 | } |
820 | ||
821 | if (from_prefixlen == 0 && to_prefixlen == 0) | |
822 | return 0; | |
823 | ||
e1694a75 YW |
824 | r = sd_netlink_message_read_u32(message, FRA_FWMARK, &fwmark); |
825 | if (r < 0 && r != -ENODATA) { | |
826 | log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m"); | |
827 | return 0; | |
828 | } | |
829 | ||
830 | r = sd_netlink_message_read_u32(message, FRA_TABLE, &table); | |
831 | if (r < 0 && r != -ENODATA) { | |
832 | log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m"); | |
833 | return 0; | |
834 | } | |
835 | ||
836 | r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tos); | |
837 | if (r < 0 && r != -ENODATA) { | |
838 | log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m"); | |
839 | return 0; | |
840 | } | |
841 | ||
842 | r = sd_netlink_message_read_string(message, FRA_IIFNAME, (const char **) &iif); | |
843 | if (r < 0 && r != -ENODATA) { | |
844 | log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m"); | |
845 | return 0; | |
846 | } | |
847 | ||
848 | r = sd_netlink_message_read_string(message, FRA_OIFNAME, (const char **) &oif); | |
849 | if (r < 0 && r != -ENODATA) { | |
850 | log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m"); | |
851 | return 0; | |
852 | } | |
bce67bbe | 853 | |
762e2659 | 854 | (void) routing_policy_rule_get(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, iif, oif, &rule); |
bce67bbe SS |
855 | |
856 | switch (type) { | |
857 | case RTM_NEWRULE: | |
508f63b4 | 858 | if (!rule) { |
762e2659 | 859 | r = routing_policy_rule_add_foreign(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, iif, oif, &rule); |
bce67bbe | 860 | if (r < 0) { |
e1694a75 | 861 | log_warning_errno(r, "Could not add rule, ignoring: %m"); |
bce67bbe SS |
862 | return 0; |
863 | } | |
864 | } | |
865 | break; | |
866 | case RTM_DELRULE: | |
867 | routing_policy_rule_free(rule); | |
868 | ||
869 | break; | |
870 | ||
871 | default: | |
872 | assert_not_reached("Received invalid RTNL message type"); | |
873 | } | |
874 | ||
875 | return 1; | |
876 | } | |
877 | ||
5fae368b TG |
878 | static int systemd_netlink_fd(void) { |
879 | int n, fd, rtnl_fd = -EINVAL; | |
880 | ||
881 | n = sd_listen_fds(true); | |
882 | if (n <= 0) | |
883 | return -EINVAL; | |
884 | ||
885 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
886 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { | |
887 | if (rtnl_fd >= 0) | |
888 | return -EINVAL; | |
889 | ||
890 | rtnl_fd = fd; | |
891 | } | |
892 | } | |
893 | ||
894 | return rtnl_fd; | |
895 | } | |
896 | ||
05d0c2e3 JT |
897 | static int manager_connect_genl(Manager *m) { |
898 | int r; | |
899 | ||
900 | assert(m); | |
901 | ||
902 | r = sd_genl_socket_open(&m->genl); | |
903 | if (r < 0) | |
904 | return r; | |
905 | ||
906 | r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE); | |
907 | if (r < 0) | |
908 | return r; | |
909 | ||
910 | r = sd_netlink_attach_event(m->genl, m->event, 0); | |
911 | if (r < 0) | |
912 | return r; | |
913 | ||
914 | return 0; | |
915 | } | |
916 | ||
5fae368b TG |
917 | static int manager_connect_rtnl(Manager *m) { |
918 | int fd, r; | |
505f8da7 TG |
919 | |
920 | assert(m); | |
505f8da7 | 921 | |
5fae368b TG |
922 | fd = systemd_netlink_fd(); |
923 | if (fd < 0) | |
1c4baffc | 924 | r = sd_netlink_open(&m->rtnl); |
5fae368b | 925 | else |
1c4baffc | 926 | r = sd_netlink_open_fd(&m->rtnl, fd); |
505f8da7 TG |
927 | if (r < 0) |
928 | return r; | |
929 | ||
1c4baffc | 930 | r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE); |
f579559b | 931 | if (r < 0) |
bf5332d2 | 932 | return r; |
f579559b | 933 | |
1c4baffc | 934 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
505f8da7 TG |
935 | if (r < 0) |
936 | return r; | |
f579559b | 937 | |
1c4baffc | 938 | r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
939 | if (r < 0) |
940 | return r; | |
505f8da7 | 941 | |
1c4baffc | 942 | r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
943 | if (r < 0) |
944 | return r; | |
45af44d4 | 945 | |
200a0868 | 946 | r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
947 | if (r < 0) |
948 | return r; | |
949 | ||
200a0868 | 950 | r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
951 | if (r < 0) |
952 | return r; | |
953 | ||
1c8e710c TG |
954 | r = sd_netlink_add_match(m->rtnl, RTM_NEWROUTE, &manager_rtnl_process_route, m); |
955 | if (r < 0) | |
956 | return r; | |
957 | ||
958 | r = sd_netlink_add_match(m->rtnl, RTM_DELROUTE, &manager_rtnl_process_route, m); | |
959 | if (r < 0) | |
960 | return r; | |
961 | ||
bce67bbe SS |
962 | r = sd_netlink_add_match(m->rtnl, RTM_NEWRULE, &manager_rtnl_process_rule, m); |
963 | if (r < 0) | |
964 | return r; | |
965 | ||
966 | r = sd_netlink_add_match(m->rtnl, RTM_DELRULE, &manager_rtnl_process_rule, m); | |
967 | if (r < 0) | |
968 | return r; | |
969 | ||
5fae368b | 970 | return 0; |
45af44d4 | 971 | } |
505f8da7 | 972 | |
5512a963 | 973 | static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) { |
84de38c5 TG |
974 | char *p; |
975 | int r; | |
976 | ||
977 | assert(s); | |
5512a963 LP |
978 | assert(address); |
979 | ||
980 | r = in_addr_to_string(address->family, &address->address, &p); | |
981 | if (r < 0) | |
982 | return r; | |
983 | ||
984 | r = ordered_set_consume(s, p); | |
985 | if (r == -EEXIST) | |
986 | return 0; | |
987 | ||
988 | return r; | |
989 | } | |
990 | ||
991 | static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) { | |
992 | int r, c = 0; | |
993 | unsigned i; | |
994 | ||
995 | assert(s); | |
996 | assert(addresses || n == 0); | |
997 | ||
998 | for (i = 0; i < n; i++) { | |
999 | r = ordered_set_put_in_addr_data(s, addresses+i); | |
1000 | if (r < 0) | |
1001 | return r; | |
1002 | ||
1003 | c += r; | |
1004 | } | |
1005 | ||
1006 | return c; | |
1007 | } | |
1008 | ||
1009 | static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) { | |
1010 | char *p; | |
1011 | int r; | |
1012 | ||
1013 | assert(s); | |
1014 | assert(address); | |
84de38c5 TG |
1015 | |
1016 | r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p); | |
1017 | if (r < 0) | |
1018 | return r; | |
1019 | ||
00616955 | 1020 | r = ordered_set_consume(s, p); |
84de38c5 TG |
1021 | if (r == -EEXIST) |
1022 | return 0; | |
1023 | ||
1024 | return r; | |
1025 | } | |
1026 | ||
5512a963 LP |
1027 | static int ordered_set_put_in4_addrv(OrderedSet *s, const struct in_addr *addresses, unsigned n) { |
1028 | int r, c = 0; | |
1029 | unsigned i; | |
84de38c5 TG |
1030 | |
1031 | assert(s); | |
5512a963 | 1032 | assert(n == 0 || addresses); |
84de38c5 TG |
1033 | |
1034 | for (i = 0; i < n; i++) { | |
5512a963 | 1035 | r = ordered_set_put_in4_addr(s, addresses+i); |
84de38c5 TG |
1036 | if (r < 0) |
1037 | return r; | |
1038 | ||
1039 | c += r; | |
1040 | } | |
1041 | ||
1042 | return c; | |
1043 | } | |
1044 | ||
00616955 | 1045 | static void print_string_set(FILE *f, const char *field, OrderedSet *s) { |
84de38c5 TG |
1046 | bool space = false; |
1047 | Iterator i; | |
1048 | char *p; | |
1049 | ||
00616955 | 1050 | if (ordered_set_isempty(s)) |
84de38c5 TG |
1051 | return; |
1052 | ||
0d536673 | 1053 | fputs(field, f); |
84de38c5 | 1054 | |
d390f8ef LP |
1055 | ORDERED_SET_FOREACH(p, s, i) |
1056 | fputs_with_space(f, p, NULL, &space); | |
1057 | ||
0d536673 | 1058 | fputc('\n', f); |
84de38c5 TG |
1059 | } |
1060 | ||
1061 | static int manager_save(Manager *m) { | |
00616955 | 1062 | _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL; |
84de38c5 TG |
1063 | Link *link; |
1064 | Iterator i; | |
1065 | _cleanup_free_ char *temp_path = NULL; | |
1066 | _cleanup_fclose_ FILE *f = NULL; | |
1067 | LinkOperationalState operstate = LINK_OPERSTATE_OFF; | |
1068 | const char *operstate_str; | |
1069 | int r; | |
1070 | ||
1071 | assert(m); | |
1072 | assert(m->state_file); | |
1073 | ||
1074 | /* We add all NTP and DNS server to a set, to filter out duplicates */ | |
00616955 | 1075 | dns = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
1076 | if (!dns) |
1077 | return -ENOMEM; | |
1078 | ||
00616955 | 1079 | ntp = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
1080 | if (!ntp) |
1081 | return -ENOMEM; | |
1082 | ||
482d1aeb | 1083 | search_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 LP |
1084 | if (!search_domains) |
1085 | return -ENOMEM; | |
1086 | ||
482d1aeb | 1087 | route_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 | 1088 | if (!route_domains) |
84de38c5 TG |
1089 | return -ENOMEM; |
1090 | ||
1091 | HASHMAP_FOREACH(link, m->links, i) { | |
1092 | if (link->flags & IFF_LOOPBACK) | |
1093 | continue; | |
1094 | ||
1095 | if (link->operstate > operstate) | |
1096 | operstate = link->operstate; | |
1097 | ||
1098 | if (!link->network) | |
1099 | continue; | |
1100 | ||
1101 | /* First add the static configured entries */ | |
5512a963 | 1102 | r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns); |
84de38c5 TG |
1103 | if (r < 0) |
1104 | return r; | |
1105 | ||
00616955 | 1106 | r = ordered_set_put_strdupv(ntp, link->network->ntp); |
84de38c5 TG |
1107 | if (r < 0) |
1108 | return r; | |
1109 | ||
00616955 | 1110 | r = ordered_set_put_strdupv(search_domains, link->network->search_domains); |
3df9bec5 LP |
1111 | if (r < 0) |
1112 | return r; | |
1113 | ||
00616955 | 1114 | r = ordered_set_put_strdupv(route_domains, link->network->route_domains); |
84de38c5 TG |
1115 | if (r < 0) |
1116 | return r; | |
1117 | ||
1118 | if (!link->dhcp_lease) | |
1119 | continue; | |
1120 | ||
1121 | /* Secondly, add the entries acquired via DHCP */ | |
27cb34f5 | 1122 | if (link->network->dhcp_use_dns) { |
84de38c5 TG |
1123 | const struct in_addr *addresses; |
1124 | ||
1125 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
1126 | if (r > 0) { | |
5512a963 | 1127 | r = ordered_set_put_in4_addrv(dns, addresses, r); |
84de38c5 TG |
1128 | if (r < 0) |
1129 | return r; | |
1130 | } else if (r < 0 && r != -ENODATA) | |
1131 | return r; | |
1132 | } | |
1133 | ||
27cb34f5 | 1134 | if (link->network->dhcp_use_ntp) { |
84de38c5 TG |
1135 | const struct in_addr *addresses; |
1136 | ||
1137 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
1138 | if (r > 0) { | |
5512a963 | 1139 | r = ordered_set_put_in4_addrv(ntp, addresses, r); |
84de38c5 TG |
1140 | if (r < 0) |
1141 | return r; | |
1142 | } else if (r < 0 && r != -ENODATA) | |
1143 | return r; | |
1144 | } | |
1145 | ||
b2a81c0b | 1146 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
84de38c5 | 1147 | const char *domainname; |
b85bc551 | 1148 | char **domains = NULL; |
84de38c5 | 1149 | |
b85bc551 | 1150 | OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains; |
84de38c5 TG |
1151 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); |
1152 | if (r >= 0) { | |
b85bc551 DW |
1153 | r = ordered_set_put_strdup(target_domains, domainname); |
1154 | if (r < 0) | |
1155 | return r; | |
1156 | } else if (r != -ENODATA) | |
1157 | return r; | |
b2a81c0b | 1158 | |
b85bc551 DW |
1159 | r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains); |
1160 | if (r >= 0) { | |
1161 | r = ordered_set_put_strdupv(target_domains, domains); | |
84de38c5 TG |
1162 | if (r < 0) |
1163 | return r; | |
1164 | } else if (r != -ENODATA) | |
1165 | return r; | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | operstate_str = link_operstate_to_string(operstate); | |
1170 | assert(operstate_str); | |
1171 | ||
1172 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
1173 | if (r < 0) | |
1174 | return r; | |
1175 | ||
0d536673 | 1176 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
5512a963 | 1177 | (void) fchmod(fileno(f), 0644); |
84de38c5 TG |
1178 | |
1179 | fprintf(f, | |
1180 | "# This is private data. Do not parse.\n" | |
1181 | "OPER_STATE=%s\n", operstate_str); | |
1182 | ||
1183 | print_string_set(f, "DNS=", dns); | |
1184 | print_string_set(f, "NTP=", ntp); | |
3df9bec5 LP |
1185 | print_string_set(f, "DOMAINS=", search_domains); |
1186 | print_string_set(f, "ROUTE_DOMAINS=", route_domains); | |
84de38c5 | 1187 | |
458d8ae3 ZJS |
1188 | r = routing_policy_serialize_rules(m->rules, f); |
1189 | if (r < 0) | |
1190 | goto fail; | |
bce67bbe | 1191 | |
84de38c5 TG |
1192 | r = fflush_and_check(f); |
1193 | if (r < 0) | |
1194 | goto fail; | |
1195 | ||
1196 | if (rename(temp_path, m->state_file) < 0) { | |
1197 | r = -errno; | |
1198 | goto fail; | |
1199 | } | |
1200 | ||
1201 | if (m->operational_state != operstate) { | |
1202 | m->operational_state = operstate; | |
1203 | r = manager_send_changed(m, "OperationalState", NULL); | |
1204 | if (r < 0) | |
1205 | log_error_errno(r, "Could not emit changed OperationalState: %m"); | |
1206 | } | |
1207 | ||
1208 | m->dirty = false; | |
1209 | ||
1210 | return 0; | |
1211 | ||
1212 | fail: | |
1213 | (void) unlink(m->state_file); | |
1214 | (void) unlink(temp_path); | |
1215 | ||
1216 | return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file); | |
1217 | } | |
1218 | ||
1219 | static int manager_dirty_handler(sd_event_source *s, void *userdata) { | |
1220 | Manager *m = userdata; | |
1221 | Link *link; | |
1222 | Iterator i; | |
1223 | int r; | |
1224 | ||
1225 | assert(m); | |
1226 | ||
1227 | if (m->dirty) | |
1228 | manager_save(m); | |
1229 | ||
1230 | SET_FOREACH(link, m->dirty_links, i) { | |
1231 | r = link_save(link); | |
1232 | if (r >= 0) | |
1233 | link_clean(link); | |
1234 | } | |
1235 | ||
1236 | return 1; | |
1237 | } | |
1238 | ||
e133b289 PF |
1239 | Link *manager_dhcp6_prefix_get(Manager *m, struct in6_addr *addr) { |
1240 | assert_return(m, NULL); | |
1241 | assert_return(m->dhcp6_prefixes, NULL); | |
1242 | assert_return(addr, NULL); | |
1243 | ||
1244 | return hashmap_get(m->dhcp6_prefixes, addr); | |
1245 | } | |
1246 | ||
9f386c6d PF |
1247 | static int dhcp6_route_add_callback(sd_netlink *nl, sd_netlink_message *m, |
1248 | void *userdata) { | |
1249 | Link *l = userdata; | |
1250 | int r; | |
1251 | union in_addr_union prefix; | |
1252 | _cleanup_free_ char *buf = NULL; | |
1253 | ||
1254 | r = sd_netlink_message_get_errno(m); | |
1255 | if (r != 0) { | |
1256 | log_link_debug_errno(l, r, "Received error adding DHCPv6 Prefix Delegation route: %m"); | |
1257 | return 0; | |
1258 | } | |
1259 | ||
1260 | r = sd_netlink_message_read_in6_addr(m, RTA_DST, &prefix.in6); | |
1261 | if (r < 0) { | |
1262 | log_link_debug_errno(l, r, "Could not read IPv6 address from DHCPv6 Prefix Delegation while adding route: %m"); | |
1263 | return 0; | |
1264 | } | |
1265 | ||
1266 | (void) in_addr_to_string(AF_INET6, &prefix, &buf); | |
1267 | log_link_debug(l, "Added DHCPv6 Prefix Deleagtion route %s/64", | |
1268 | strnull(buf)); | |
1269 | ||
1270 | return 0; | |
1271 | } | |
1272 | ||
e133b289 | 1273 | int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) { |
9f386c6d PF |
1274 | int r; |
1275 | Route *route; | |
1276 | ||
e133b289 PF |
1277 | assert_return(m, -EINVAL); |
1278 | assert_return(m->dhcp6_prefixes, -ENODATA); | |
1279 | assert_return(addr, -EINVAL); | |
1280 | ||
9f386c6d PF |
1281 | r = route_add(link, AF_INET6, (union in_addr_union *) addr, 64, |
1282 | 0, 0, 0, &route); | |
1283 | if (r < 0) | |
1284 | return r; | |
1285 | ||
1286 | r = route_configure(route, link, dhcp6_route_add_callback); | |
1287 | if (r < 0) | |
1288 | return r; | |
1289 | ||
e133b289 PF |
1290 | return hashmap_put(m->dhcp6_prefixes, addr, link); |
1291 | } | |
1292 | ||
9f386c6d PF |
1293 | static int dhcp6_route_remove_callback(sd_netlink *nl, sd_netlink_message *m, |
1294 | void *userdata) { | |
1295 | Link *l = userdata; | |
1296 | int r; | |
1297 | union in_addr_union prefix; | |
1298 | _cleanup_free_ char *buf = NULL; | |
1299 | ||
1300 | r = sd_netlink_message_get_errno(m); | |
1301 | if (r != 0) { | |
1302 | log_link_debug_errno(l, r, "Received error on DHCPv6 Prefix Delegation route removal: %m"); | |
1303 | return 0; | |
1304 | } | |
1305 | ||
1306 | r = sd_netlink_message_read_in6_addr(m, RTA_DST, &prefix.in6); | |
1307 | if (r < 0) { | |
1308 | log_link_debug_errno(l, r, "Could not read IPv6 address from DHCPv6 Prefix Delegation while removing route: %m"); | |
1309 | return 0; | |
1310 | } | |
1311 | ||
1312 | (void) in_addr_to_string(AF_INET6, &prefix, &buf); | |
1313 | log_link_debug(l, "Removed DHCPv6 Prefix Delegation route %s/64", | |
1314 | strnull(buf)); | |
1315 | ||
1316 | return 0; | |
1317 | } | |
1318 | ||
e133b289 PF |
1319 | int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) { |
1320 | Link *l; | |
9f386c6d PF |
1321 | int r; |
1322 | Route *route; | |
e133b289 PF |
1323 | |
1324 | assert_return(m, -EINVAL); | |
1325 | assert_return(m->dhcp6_prefixes, -ENODATA); | |
1326 | assert_return(addr, -EINVAL); | |
1327 | ||
1328 | l = hashmap_remove(m->dhcp6_prefixes, addr); | |
1329 | if (!l) | |
1330 | return -EINVAL; | |
1331 | ||
1332 | (void) sd_radv_remove_prefix(l->radv, addr, 64); | |
9f386c6d PF |
1333 | r = route_get(l, AF_INET6, (union in_addr_union *) addr, 64, |
1334 | 0, 0, 0, &route); | |
1335 | if (r >= 0) | |
1336 | (void) route_remove(route, l, dhcp6_route_remove_callback); | |
e133b289 PF |
1337 | |
1338 | return 0; | |
1339 | } | |
1340 | ||
1341 | int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) { | |
1342 | Iterator i; | |
1343 | Link *l; | |
1344 | struct in6_addr *addr; | |
1345 | ||
1346 | assert_return(m, -EINVAL); | |
b7d16a91 | 1347 | assert_return(link, -EINVAL); |
e133b289 PF |
1348 | |
1349 | HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i) { | |
1350 | if (l != link) | |
1351 | continue; | |
1352 | ||
9f386c6d | 1353 | (void) manager_dhcp6_prefix_remove(m, addr); |
e133b289 PF |
1354 | } |
1355 | ||
1356 | return 0; | |
1357 | } | |
1358 | ||
1359 | static void dhcp6_prefixes_hash_func(const void *p, struct siphash *state) { | |
1360 | const struct in6_addr *addr = p; | |
1361 | ||
1362 | assert(p); | |
1363 | ||
1364 | siphash24_compress(addr, sizeof(*addr), state); | |
1365 | } | |
1366 | ||
1367 | static int dhcp6_prefixes_compare_func(const void *_a, const void *_b) { | |
1368 | const struct in6_addr *a = _a, *b = _b; | |
1369 | ||
aa18944d | 1370 | return memcmp(a, b, sizeof(*a)); |
e133b289 PF |
1371 | } |
1372 | ||
1373 | static const struct hash_ops dhcp6_prefixes_hash_ops = { | |
1374 | .hash = dhcp6_prefixes_hash_func, | |
1375 | .compare = dhcp6_prefixes_compare_func, | |
1376 | }; | |
1377 | ||
b76d99d9 | 1378 | int manager_new(Manager **ret, sd_event *event) { |
8e766630 | 1379 | _cleanup_(manager_freep) Manager *m = NULL; |
45af44d4 | 1380 | int r; |
f579559b | 1381 | |
5fae368b TG |
1382 | m = new0(Manager, 1); |
1383 | if (!m) | |
1384 | return -ENOMEM; | |
45af44d4 | 1385 | |
5fae368b TG |
1386 | m->state_file = strdup("/run/systemd/netif/state"); |
1387 | if (!m->state_file) | |
1388 | return -ENOMEM; | |
1389 | ||
b76d99d9 | 1390 | m->event = sd_event_ref(event); |
5fae368b | 1391 | |
84de38c5 TG |
1392 | r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m); |
1393 | if (r < 0) | |
1394 | return r; | |
1395 | ||
5fae368b | 1396 | r = manager_connect_rtnl(m); |
45af44d4 TG |
1397 | if (r < 0) |
1398 | return r; | |
1399 | ||
05d0c2e3 JT |
1400 | r = manager_connect_genl(m); |
1401 | if (r < 0) | |
1402 | return r; | |
1403 | ||
5fae368b TG |
1404 | r = manager_connect_udev(m); |
1405 | if (r < 0) | |
1406 | return r; | |
45af44d4 | 1407 | |
5fae368b TG |
1408 | m->netdevs = hashmap_new(&string_hash_ops); |
1409 | if (!m->netdevs) | |
1410 | return -ENOMEM; | |
f579559b | 1411 | |
5fae368b | 1412 | LIST_HEAD_INIT(m->networks); |
f579559b | 1413 | |
05d0c2e3 JT |
1414 | r = sd_resolve_default(&m->resolve); |
1415 | if (r < 0) | |
1416 | return r; | |
1417 | ||
1418 | r = sd_resolve_attach_event(m->resolve, m->event, 0); | |
1419 | if (r < 0) | |
1420 | return r; | |
1421 | ||
5fae368b TG |
1422 | r = setup_default_address_pool(m); |
1423 | if (r < 0) | |
1424 | return r; | |
f579559b | 1425 | |
e133b289 PF |
1426 | m->dhcp6_prefixes = hashmap_new(&dhcp6_prefixes_hash_ops); |
1427 | if (!m->dhcp6_prefixes) | |
1428 | return -ENOMEM; | |
1429 | ||
8341a5c3 | 1430 | m->duid.type = DUID_TYPE_EN; |
413708d1 | 1431 | |
458d8ae3 | 1432 | (void) routing_policy_load_rules(m->state_file, &m->rules_saved); |
bce67bbe | 1433 | |
1cc6c93a | 1434 | *ret = TAKE_PTR(m); |
f579559b | 1435 | |
f579559b TG |
1436 | return 0; |
1437 | } | |
1438 | ||
5fae368b TG |
1439 | void manager_free(Manager *m) { |
1440 | Network *network; | |
1441 | NetDev *netdev; | |
1442 | Link *link; | |
1443 | AddressPool *pool; | |
f579559b | 1444 | |
5fae368b TG |
1445 | if (!m) |
1446 | return; | |
505f8da7 | 1447 | |
5fae368b | 1448 | free(m->state_file); |
505f8da7 | 1449 | |
bce67bbe SS |
1450 | while ((network = m->networks)) |
1451 | network_free(network); | |
1452 | ||
e133b289 PF |
1453 | while ((link = hashmap_first(m->dhcp6_prefixes))) |
1454 | link_unref(link); | |
1455 | hashmap_free(m->dhcp6_prefixes); | |
1456 | ||
5fae368b TG |
1457 | while ((link = hashmap_first(m->links))) |
1458 | link_unref(link); | |
1459 | hashmap_free(m->links); | |
f579559b | 1460 | |
dbffab87 TG |
1461 | hashmap_free(m->networks_by_name); |
1462 | ||
5fae368b TG |
1463 | while ((netdev = hashmap_first(m->netdevs))) |
1464 | netdev_unref(netdev); | |
1465 | hashmap_free(m->netdevs); | |
1466 | ||
1467 | while ((pool = m->address_pools)) | |
1468 | address_pool_free(pool); | |
1469 | ||
bce67bbe SS |
1470 | set_free(m->rules); |
1471 | set_free(m->rules_foreign); | |
1472 | ||
b921fcb2 | 1473 | set_free_with_destructor(m->rules_saved, routing_policy_rule_free); |
bce67bbe | 1474 | |
1c4baffc | 1475 | sd_netlink_unref(m->rtnl); |
2f5b4a77 | 1476 | sd_event_unref(m->event); |
5fae368b | 1477 | |
05d0c2e3 JT |
1478 | sd_resolve_unref(m->resolve); |
1479 | ||
7d20d375 TG |
1480 | sd_event_source_unref(m->udev_event_source); |
1481 | udev_monitor_unref(m->udev_monitor); | |
1482 | udev_unref(m->udev); | |
1483 | ||
1484 | sd_bus_unref(m->bus); | |
1485 | sd_bus_slot_unref(m->prepare_for_sleep_slot); | |
d7afd945 | 1486 | sd_bus_slot_unref(m->connected_slot); |
7d20d375 | 1487 | |
7901cea1 MP |
1488 | free(m->dynamic_timezone); |
1489 | free(m->dynamic_hostname); | |
1490 | ||
5fae368b TG |
1491 | free(m); |
1492 | } | |
1493 | ||
b76d99d9 | 1494 | int manager_start(Manager *m) { |
84de38c5 TG |
1495 | Link *link; |
1496 | Iterator i; | |
1497 | ||
a97dcc12 TG |
1498 | assert(m); |
1499 | ||
84de38c5 TG |
1500 | /* The dirty handler will deal with future serialization, but the first one |
1501 | must be done explicitly. */ | |
1502 | ||
1503 | manager_save(m); | |
1504 | ||
1505 | HASHMAP_FOREACH(link, m->links, i) | |
1506 | link_save(link); | |
1507 | ||
b76d99d9 | 1508 | return 0; |
a97dcc12 TG |
1509 | } |
1510 | ||
5fae368b TG |
1511 | int manager_load_config(Manager *m) { |
1512 | int r; | |
1513 | ||
1514 | /* update timestamp */ | |
1515 | paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true); | |
1516 | ||
1517 | r = netdev_load(m); | |
f579559b TG |
1518 | if (r < 0) |
1519 | return r; | |
1520 | ||
5fae368b | 1521 | r = network_load(m); |
9021bb9f TG |
1522 | if (r < 0) |
1523 | return r; | |
1524 | ||
f579559b TG |
1525 | return 0; |
1526 | } | |
f882c247 | 1527 | |
5fae368b TG |
1528 | bool manager_should_reload(Manager *m) { |
1529 | return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false); | |
1530 | } | |
1531 | ||
1532 | int manager_rtnl_enumerate_links(Manager *m) { | |
4afd3348 | 1533 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1534 | sd_netlink_message *link; |
f882c247 TG |
1535 | int r; |
1536 | ||
5da8149f | 1537 | assert(m); |
5fae368b | 1538 | assert(m->rtnl); |
5da8149f | 1539 | |
5fae368b | 1540 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
f882c247 TG |
1541 | if (r < 0) |
1542 | return r; | |
1543 | ||
1c4baffc | 1544 | r = sd_netlink_message_request_dump(req, true); |
dd3efc09 TG |
1545 | if (r < 0) |
1546 | return r; | |
1547 | ||
1c4baffc | 1548 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
f2236469 TG |
1549 | if (r < 0) |
1550 | return r; | |
1551 | ||
1c4baffc | 1552 | for (link = reply; link; link = sd_netlink_message_next(link)) { |
5fae368b | 1553 | int k; |
2e9f08ea | 1554 | |
6a24f148 TG |
1555 | m->enumerating = true; |
1556 | ||
5fae368b TG |
1557 | k = manager_rtnl_process_link(m->rtnl, link, m); |
1558 | if (k < 0) | |
1559 | r = k; | |
6a24f148 TG |
1560 | |
1561 | m->enumerating = false; | |
5fae368b | 1562 | } |
2e9f08ea | 1563 | |
5fae368b | 1564 | return r; |
f882c247 | 1565 | } |
3bef724f | 1566 | |
5fae368b | 1567 | int manager_rtnl_enumerate_addresses(Manager *m) { |
4afd3348 | 1568 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1569 | sd_netlink_message *addr; |
1346b1f0 TG |
1570 | int r; |
1571 | ||
5fae368b TG |
1572 | assert(m); |
1573 | assert(m->rtnl); | |
bcbca829 | 1574 | |
5fae368b TG |
1575 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); |
1576 | if (r < 0) | |
1577 | return r; | |
bcbca829 | 1578 | |
1c4baffc | 1579 | r = sd_netlink_message_request_dump(req, true); |
1346b1f0 TG |
1580 | if (r < 0) |
1581 | return r; | |
1582 | ||
1c4baffc | 1583 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
5fae368b TG |
1584 | if (r < 0) |
1585 | return r; | |
1586 | ||
1c4baffc | 1587 | for (addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
5fae368b TG |
1588 | int k; |
1589 | ||
6a24f148 TG |
1590 | m->enumerating = true; |
1591 | ||
200a0868 | 1592 | k = manager_rtnl_process_address(m->rtnl, addr, m); |
5fae368b TG |
1593 | if (k < 0) |
1594 | r = k; | |
6a24f148 TG |
1595 | |
1596 | m->enumerating = false; | |
5fae368b TG |
1597 | } |
1598 | ||
1599 | return r; | |
1346b1f0 TG |
1600 | } |
1601 | ||
1c8e710c | 1602 | int manager_rtnl_enumerate_routes(Manager *m) { |
4afd3348 | 1603 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c8e710c TG |
1604 | sd_netlink_message *route; |
1605 | int r; | |
1606 | ||
1607 | assert(m); | |
1608 | assert(m->rtnl); | |
1609 | ||
1610 | r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0); | |
1611 | if (r < 0) | |
1612 | return r; | |
1613 | ||
1614 | r = sd_netlink_message_request_dump(req, true); | |
1615 | if (r < 0) | |
1616 | return r; | |
1617 | ||
1618 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
1619 | if (r < 0) | |
1620 | return r; | |
1621 | ||
1622 | for (route = reply; route; route = sd_netlink_message_next(route)) { | |
1623 | int k; | |
1624 | ||
1625 | m->enumerating = true; | |
1626 | ||
1627 | k = manager_rtnl_process_route(m->rtnl, route, m); | |
1628 | if (k < 0) | |
1629 | r = k; | |
1630 | ||
1631 | m->enumerating = false; | |
1632 | } | |
1633 | ||
1634 | return r; | |
1635 | } | |
1636 | ||
bce67bbe SS |
1637 | int manager_rtnl_enumerate_rules(Manager *m) { |
1638 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
1639 | sd_netlink_message *rule; | |
1640 | int r; | |
1641 | ||
1642 | assert(m); | |
1643 | assert(m->rtnl); | |
1644 | ||
1645 | r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0); | |
1646 | if (r < 0) | |
1647 | return r; | |
1648 | ||
1649 | r = sd_netlink_message_request_dump(req, true); | |
1650 | if (r < 0) | |
1651 | return r; | |
1652 | ||
1653 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
6acbbdd4 SS |
1654 | if (r < 0) { |
1655 | if (r == -EOPNOTSUPP) { | |
1656 | log_debug("FIB Rules are not supported by the kernel. Ignoring."); | |
1657 | return 0; | |
1658 | } | |
1659 | ||
bce67bbe | 1660 | return r; |
6acbbdd4 | 1661 | } |
bce67bbe SS |
1662 | |
1663 | for (rule = reply; rule; rule = sd_netlink_message_next(rule)) { | |
1664 | int k; | |
1665 | ||
1666 | m->enumerating = true; | |
1667 | ||
1668 | k = manager_rtnl_process_rule(m->rtnl, rule, m); | |
1669 | if (k < 0) | |
1670 | r = k; | |
1671 | ||
1672 | m->enumerating = false; | |
1673 | } | |
1674 | ||
1675 | return r; | |
1676 | } | |
1677 | ||
0dd25fb9 | 1678 | int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) { |
11bf3cce LP |
1679 | AddressPool *p; |
1680 | int r; | |
1681 | ||
1682 | assert(m); | |
1683 | assert(prefixlen > 0); | |
1684 | assert(found); | |
1685 | ||
1686 | LIST_FOREACH(address_pools, p, m->address_pools) { | |
1687 | if (p->family != family) | |
1688 | continue; | |
1689 | ||
1690 | r = address_pool_acquire(p, prefixlen, found); | |
1691 | if (r != 0) | |
1692 | return r; | |
1693 | } | |
1694 | ||
1695 | return 0; | |
1696 | } | |
4f5f911e LP |
1697 | |
1698 | Link* manager_find_uplink(Manager *m, Link *exclude) { | |
1699 | _cleanup_free_ struct local_address *gateways = NULL; | |
1700 | int n, i; | |
1701 | ||
1702 | assert(m); | |
1703 | ||
1704 | /* Looks for a suitable "uplink", via black magic: an | |
1705 | * interface that is up and where the default route with the | |
1706 | * highest priority points to. */ | |
1707 | ||
1708 | n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways); | |
1709 | if (n < 0) { | |
1710 | log_warning_errno(n, "Failed to determine list of default gateways: %m"); | |
1711 | return NULL; | |
1712 | } | |
1713 | ||
1714 | for (i = 0; i < n; i++) { | |
1715 | Link *link; | |
1716 | ||
1717 | link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex)); | |
1718 | if (!link) { | |
c2c940bd | 1719 | log_debug("Weird, found a gateway for a link we don't know. Ignoring."); |
4f5f911e LP |
1720 | continue; |
1721 | } | |
1722 | ||
1723 | if (link == exclude) | |
1724 | continue; | |
1725 | ||
1726 | if (link->operstate < LINK_OPERSTATE_ROUTABLE) | |
1727 | continue; | |
1728 | ||
1729 | return link; | |
1730 | } | |
1731 | ||
1732 | return NULL; | |
1733 | } | |
84de38c5 TG |
1734 | |
1735 | void manager_dirty(Manager *manager) { | |
1736 | assert(manager); | |
1737 | ||
1738 | /* the serialized state in /run is no longer up-to-date */ | |
1739 | manager->dirty = true; | |
1740 | } | |
59eb33e0 MP |
1741 | |
1742 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
1743 | Manager *manager = userdata; | |
1744 | const sd_bus_error *e; | |
1745 | ||
1746 | assert(m); | |
1747 | assert(manager); | |
1748 | ||
1749 | e = sd_bus_message_get_error(m); | |
1750 | if (e) | |
1751 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message); | |
1752 | ||
1753 | return 1; | |
1754 | } | |
1755 | ||
1756 | int manager_set_hostname(Manager *m, const char *hostname) { | |
1757 | int r; | |
1758 | ||
1759 | log_debug("Setting transient hostname: '%s'", strna(hostname)); | |
d7afd945 | 1760 | |
7901cea1 MP |
1761 | if (free_and_strdup(&m->dynamic_hostname, hostname) < 0) |
1762 | return log_oom(); | |
59eb33e0 | 1763 | |
d7afd945 LP |
1764 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { |
1765 | log_info("Not connected to system bus, not setting hostname."); | |
59eb33e0 MP |
1766 | return 0; |
1767 | } | |
1768 | ||
1769 | r = sd_bus_call_method_async( | |
1770 | m->bus, | |
1771 | NULL, | |
1772 | "org.freedesktop.hostname1", | |
1773 | "/org/freedesktop/hostname1", | |
1774 | "org.freedesktop.hostname1", | |
1775 | "SetHostname", | |
1776 | set_hostname_handler, | |
1777 | m, | |
1778 | "sb", | |
1779 | hostname, | |
1780 | false); | |
1781 | ||
1782 | if (r < 0) | |
1783 | return log_error_errno(r, "Could not set transient hostname: %m"); | |
1784 | ||
1785 | return 0; | |
1786 | } | |
1787 | ||
1788 | static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
1789 | Manager *manager = userdata; | |
1790 | const sd_bus_error *e; | |
1791 | ||
1792 | assert(m); | |
1793 | assert(manager); | |
1794 | ||
1795 | e = sd_bus_message_get_error(m); | |
1796 | if (e) | |
1797 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message); | |
1798 | ||
1799 | return 1; | |
1800 | } | |
1801 | ||
1802 | int manager_set_timezone(Manager *m, const char *tz) { | |
1803 | int r; | |
1804 | ||
1805 | assert(m); | |
1806 | assert(tz); | |
1807 | ||
1808 | log_debug("Setting system timezone: '%s'", tz); | |
7901cea1 MP |
1809 | if (free_and_strdup(&m->dynamic_timezone, tz) < 0) |
1810 | return log_oom(); | |
59eb33e0 | 1811 | |
d7afd945 | 1812 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { |
3d282fff | 1813 | log_info("Not connected to system bus, not setting timezone."); |
59eb33e0 MP |
1814 | return 0; |
1815 | } | |
1816 | ||
1817 | r = sd_bus_call_method_async( | |
1818 | m->bus, | |
1819 | NULL, | |
1820 | "org.freedesktop.timedate1", | |
1821 | "/org/freedesktop/timedate1", | |
1822 | "org.freedesktop.timedate1", | |
1823 | "SetTimezone", | |
1824 | set_timezone_handler, | |
1825 | m, | |
1826 | "sb", | |
1827 | tz, | |
1828 | false); | |
1829 | if (r < 0) | |
1830 | return log_error_errno(r, "Could not set timezone: %m"); | |
1831 | ||
1832 | return 0; | |
1833 | } |