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