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