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