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