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