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