/src/glib/gio/gnetworkmonitorsystemd.c
Line | Count | Source |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright 2026 Sorah Fukumori |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | |
23 | | #include <string.h> |
24 | | |
25 | | #include "gnetworkmonitorsystemd.h" |
26 | | #include "gcancellable.h" |
27 | | #include "gioerror.h" |
28 | | #include "ginitable.h" |
29 | | #include "giomodule-priv.h" |
30 | | #include "gnetworkmonitor.h" |
31 | | #include "gdbusproxy.h" |
32 | | |
33 | 0 | #define G_NETWORK_MONITOR_SYSTEMD_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable)) |
34 | | |
35 | | static void g_network_monitor_systemd_iface_init (GNetworkMonitorInterface *iface); |
36 | | static void g_network_monitor_systemd_initable_iface_init (GInitableIface *iface); |
37 | | |
38 | | typedef enum |
39 | | { |
40 | | PROP_NETWORK_AVAILABLE = 1, |
41 | | PROP_NETWORK_METERED, |
42 | | PROP_CONNECTIVITY |
43 | | } GNetworkMonitorSystemdProperty; |
44 | | |
45 | | /* Network monitor backend for systemd-networkd, via its |
46 | | * org.freedesktop.network1.Manager D-Bus interface. Ranks below the |
47 | | * NetworkManager backend, which reports richer system-wide state. |
48 | | * |
49 | | * This backend initializes itself asynchronously, to avoid blocking D-Bus |
50 | | * round trips during initable_init(). Until the connection to |
51 | | * systemd-networkd is established, network-available is FALSE. If networkd |
52 | | * is unavailable or not authoritative, it falls back to the netlink backend. |
53 | | * can_reach() is inherited from the netlink parent and works in every |
54 | | * state. */ |
55 | | struct _GNetworkMonitorSystemd |
56 | | { |
57 | | GNetworkMonitorNetlink parent_instance; |
58 | | |
59 | | GCancellable *cancellable; /* (owned) */ |
60 | | |
61 | | /* At most one of proxy and fallback is set; neither while the async proxy |
62 | | * setup is still pending. */ |
63 | | GDBusProxy *proxy; /* (owned) (nullable) */ |
64 | | unsigned long signal_id; |
65 | | |
66 | | /* Non-NULL only in fallback mode. The netlink backend disables its |
67 | | * availability tracking when subclassed, so a standalone instance is needed |
68 | | * to activate its full functionality. */ |
69 | | GNetworkMonitorNetlink *fallback; /* (owned) (nullable) */ |
70 | | unsigned long fallback_network_changed_id; |
71 | | |
72 | | GNetworkConnectivity connectivity; |
73 | | gboolean network_available; |
74 | | }; |
75 | | |
76 | 0 | G_DEFINE_TYPE_WITH_CODE (GNetworkMonitorSystemd, g_network_monitor_systemd, G_TYPE_NETWORK_MONITOR_NETLINK, |
77 | 0 | G_IMPLEMENT_INTERFACE (G_TYPE_NETWORK_MONITOR, |
78 | 0 | g_network_monitor_systemd_iface_init) |
79 | 0 | G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, |
80 | 0 | g_network_monitor_systemd_initable_iface_init) |
81 | 0 | _g_io_modules_ensure_extension_points_registered (); |
82 | 0 | g_io_extension_point_implement (G_NETWORK_MONITOR_EXTENSION_POINT_NAME, |
83 | 0 | g_define_type_id, |
84 | 0 | "systemd", |
85 | 0 | 25)) |
86 | 0 |
|
87 | 0 | static void |
88 | 0 | g_network_monitor_systemd_init (GNetworkMonitorSystemd *self) |
89 | 0 | { |
90 | 0 | self->connectivity = G_NETWORK_CONNECTIVITY_LOCAL; |
91 | 0 | self->network_available = FALSE; |
92 | 0 | } |
93 | | |
94 | | static void |
95 | | g_network_monitor_systemd_get_property (GObject *object, |
96 | | guint prop_id, |
97 | | GValue *value, |
98 | | GParamSpec *pspec) |
99 | 0 | { |
100 | 0 | GNetworkMonitorSystemd *self = G_NETWORK_MONITOR_SYSTEMD (object); |
101 | |
|
102 | 0 | switch ((GNetworkMonitorSystemdProperty) prop_id) |
103 | 0 | { |
104 | 0 | case PROP_NETWORK_AVAILABLE: |
105 | 0 | g_value_set_boolean (value, self->network_available); |
106 | 0 | break; |
107 | | |
108 | 0 | case PROP_NETWORK_METERED: |
109 | | /* systemd-networkd does not expose metered state. */ |
110 | 0 | g_value_set_boolean (value, FALSE); |
111 | 0 | break; |
112 | | |
113 | 0 | case PROP_CONNECTIVITY: |
114 | 0 | g_value_set_enum (value, self->connectivity); |
115 | 0 | break; |
116 | | |
117 | 0 | default: |
118 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
119 | 0 | break; |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | /* "routable" means at least one managed link has a global address, i.e. the host |
124 | | * can reach off-link destinations. OperationalState is used rather than |
125 | | * OnlineState because OnlineState is shaped by RequiredForOnline= policy and |
126 | | * treats link-local-only links as online. */ |
127 | | static gboolean |
128 | | operational_state_is_available (GDBusProxy *proxy) |
129 | 0 | { |
130 | 0 | GVariant *v; |
131 | 0 | const char *state; |
132 | 0 | gboolean available; |
133 | |
|
134 | 0 | v = g_dbus_proxy_get_cached_property (proxy, "OperationalState"); |
135 | 0 | if (v == NULL) |
136 | 0 | return FALSE; |
137 | | |
138 | 0 | state = g_variant_get_string (v, NULL); |
139 | 0 | available = (g_strcmp0 (state, "routable") == 0); |
140 | 0 | g_variant_unref (v); |
141 | |
|
142 | 0 | return available; |
143 | 0 | } |
144 | | |
145 | | static gboolean |
146 | | set_availability (GNetworkMonitorSystemd *self, |
147 | | gboolean new_available, |
148 | | GNetworkConnectivity new_connectivity) |
149 | 0 | { |
150 | 0 | gboolean changed = FALSE; |
151 | |
|
152 | 0 | g_object_freeze_notify (G_OBJECT (self)); |
153 | 0 | if (new_available != self->network_available) |
154 | 0 | { |
155 | 0 | self->network_available = new_available; |
156 | 0 | g_object_notify (G_OBJECT (self), "network-available"); |
157 | 0 | changed = TRUE; |
158 | 0 | } |
159 | 0 | if (new_connectivity != self->connectivity) |
160 | 0 | { |
161 | 0 | self->connectivity = new_connectivity; |
162 | 0 | g_object_notify (G_OBJECT (self), "connectivity"); |
163 | 0 | changed = TRUE; |
164 | 0 | } |
165 | 0 | g_object_thaw_notify (G_OBJECT (self)); |
166 | |
|
167 | 0 | return changed; |
168 | 0 | } |
169 | | |
170 | | static void |
171 | | sync_properties (GNetworkMonitorSystemd *self) |
172 | 0 | { |
173 | 0 | gboolean new_available; |
174 | 0 | GNetworkConnectivity new_connectivity; |
175 | |
|
176 | 0 | new_available = operational_state_is_available (self->proxy); |
177 | | /* networkd performs no internet-reachability or captive-portal check, so we |
178 | | * can only distinguish "routable" (FULL) from "not routable" (LOCAL). */ |
179 | 0 | new_connectivity = new_available ? G_NETWORK_CONNECTIVITY_FULL |
180 | 0 | : G_NETWORK_CONNECTIVITY_LOCAL; |
181 | |
|
182 | 0 | if (set_availability (self, new_available, new_connectivity)) |
183 | 0 | g_signal_emit_by_name (self, "network-changed", new_available); |
184 | 0 | } |
185 | | |
186 | | static void |
187 | | proxy_properties_changed_cb (GDBusProxy *proxy, |
188 | | GVariant *changed_properties, |
189 | | GStrv invalidated_properties, |
190 | | GNetworkMonitorSystemd *self) |
191 | 0 | { |
192 | 0 | sync_properties (self); |
193 | 0 | } |
194 | | |
195 | | /* networkd may be running without being the connectivity authority (e.g. it |
196 | | * manages no links). It then reports OnlineState "unknown". Only act as the |
197 | | * backend when we can confirm it is managing the network; otherwise decline so |
198 | | * a lower-priority backend (plain netlink) is used instead. */ |
199 | | static gboolean |
200 | | networkd_is_authoritative (GDBusProxy *proxy) |
201 | 0 | { |
202 | 0 | GVariant *v; |
203 | 0 | const char *state; |
204 | 0 | gboolean authoritative; |
205 | |
|
206 | 0 | v = g_dbus_proxy_get_cached_property (proxy, "OnlineState"); |
207 | 0 | if (v == NULL) |
208 | 0 | return FALSE; /* networkd too old (pre-v249) to tell us; let netlink handle it. */ |
209 | | |
210 | 0 | state = g_variant_get_string (v, NULL); |
211 | 0 | authoritative = g_strcmp0 (state, "unknown") != 0 && state[0] != '\0'; |
212 | 0 | g_variant_unref (v); |
213 | |
|
214 | 0 | return authoritative; |
215 | 0 | } |
216 | | |
217 | | /* The netlink monitor emits network-changed on any default route change, not |
218 | | * only when availability flips; force_network_changed preserves that. */ |
219 | | static void |
220 | | sync_properties_from_fallback (GNetworkMonitorSystemd *self, |
221 | | gboolean force_network_changed) |
222 | 0 | { |
223 | 0 | gboolean available; |
224 | 0 | GNetworkConnectivity connectivity; |
225 | 0 | gboolean changed; |
226 | |
|
227 | 0 | g_object_get (self->fallback, |
228 | 0 | "network-available", &available, |
229 | 0 | "connectivity", &connectivity, |
230 | 0 | NULL); |
231 | |
|
232 | 0 | changed = set_availability (self, available, connectivity); |
233 | 0 | if (changed || force_network_changed) |
234 | 0 | g_signal_emit_by_name (self, "network-changed", available); |
235 | 0 | } |
236 | | |
237 | | static void |
238 | | fallback_network_changed_cb (GNetworkMonitor *fallback, |
239 | | gboolean network_available, |
240 | | GNetworkMonitorSystemd *self) |
241 | 0 | { |
242 | 0 | sync_properties_from_fallback (self, TRUE); |
243 | 0 | } |
244 | | |
245 | | static void |
246 | | start_fallback (GNetworkMonitorSystemd *self) |
247 | 0 | { |
248 | 0 | GError *error = NULL; |
249 | |
|
250 | 0 | self->fallback = g_initable_new (G_TYPE_NETWORK_MONITOR_NETLINK, |
251 | 0 | self->cancellable, &error, NULL); |
252 | 0 | if (self->fallback == NULL) |
253 | 0 | { |
254 | 0 | g_debug ("GNetworkMonitorSystemd: could not create the netlink fallback monitor: %s", |
255 | 0 | error->message); |
256 | 0 | g_error_free (error); |
257 | 0 | return; |
258 | 0 | } |
259 | | |
260 | 0 | self->fallback_network_changed_id = |
261 | 0 | g_signal_connect (self->fallback, "network-changed", |
262 | 0 | G_CALLBACK (fallback_network_changed_cb), self); |
263 | 0 | sync_properties_from_fallback (self, FALSE); |
264 | 0 | } |
265 | | |
266 | | static void |
267 | | proxy_ready_cb (GObject *source_object, |
268 | | GAsyncResult *result, |
269 | | gpointer user_data) |
270 | 0 | { |
271 | 0 | GNetworkMonitorSystemd *self = user_data; |
272 | 0 | GDBusProxy *proxy; |
273 | 0 | GError *error = NULL; |
274 | 0 | char *name_owner = NULL; |
275 | |
|
276 | 0 | proxy = g_dbus_proxy_new_for_bus_finish (result, &error); |
277 | |
|
278 | 0 | if (proxy == NULL) |
279 | 0 | { |
280 | | /* Cancelled means the monitor is being disposed; leave it alone. */ |
281 | 0 | if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
282 | 0 | { |
283 | 0 | g_debug ("GNetworkMonitorSystemd: could not create the D-Bus proxy: %s", |
284 | 0 | error->message); |
285 | 0 | start_fallback (self); |
286 | 0 | } |
287 | 0 | g_error_free (error); |
288 | 0 | g_object_unref (self); |
289 | 0 | return; |
290 | 0 | } |
291 | | |
292 | 0 | name_owner = g_dbus_proxy_get_name_owner (proxy); |
293 | |
|
294 | 0 | if (name_owner == NULL) |
295 | 0 | { |
296 | 0 | g_debug ("GNetworkMonitorSystemd: systemd-networkd is not running"); |
297 | 0 | g_object_unref (proxy); |
298 | 0 | start_fallback (self); |
299 | 0 | } |
300 | 0 | else if (!networkd_is_authoritative (proxy)) |
301 | 0 | { |
302 | 0 | g_debug ("GNetworkMonitorSystemd: systemd-networkd is not managing the network"); |
303 | 0 | g_object_unref (proxy); |
304 | 0 | start_fallback (self); |
305 | 0 | } |
306 | 0 | else |
307 | 0 | { |
308 | 0 | self->signal_id = g_signal_connect (G_OBJECT (proxy), "g-properties-changed", |
309 | 0 | G_CALLBACK (proxy_properties_changed_cb), self); |
310 | 0 | self->proxy = g_steal_pointer (&proxy); |
311 | 0 | sync_properties (self); |
312 | 0 | } |
313 | |
|
314 | 0 | g_free (name_owner); |
315 | 0 | g_object_unref (self); |
316 | 0 | } |
317 | | |
318 | | static gboolean |
319 | | g_network_monitor_systemd_initable_init (GInitable *initable, |
320 | | GCancellable *cancellable, |
321 | | GError **error) |
322 | 0 | { |
323 | 0 | GNetworkMonitorSystemd *self = G_NETWORK_MONITOR_SYSTEMD (initable); |
324 | 0 | GInitableIface *parent_iface; |
325 | | |
326 | | /* Set up the netlink parent, which provides the route lookups used by |
327 | | * can_reach(). */ |
328 | 0 | parent_iface = g_type_interface_peek_parent (G_NETWORK_MONITOR_SYSTEMD_GET_INITABLE_IFACE (initable)); |
329 | 0 | if (!parent_iface->init (initable, cancellable, error)) |
330 | 0 | return FALSE; |
331 | | |
332 | | /* Whether networkd is usable is determined asynchronously in |
333 | | * proxy_ready_cb(), which holds a reference on the monitor. */ |
334 | 0 | self->cancellable = g_cancellable_new (); |
335 | 0 | g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, |
336 | 0 | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, |
337 | 0 | NULL, |
338 | 0 | "org.freedesktop.network1", |
339 | 0 | "/org/freedesktop/network1", |
340 | 0 | "org.freedesktop.network1.Manager", |
341 | 0 | self->cancellable, |
342 | 0 | proxy_ready_cb, |
343 | 0 | g_object_ref (self)); |
344 | |
|
345 | 0 | return TRUE; |
346 | 0 | } |
347 | | |
348 | | static void |
349 | | g_network_monitor_systemd_dispose (GObject *object) |
350 | 0 | { |
351 | 0 | GNetworkMonitorSystemd *self = G_NETWORK_MONITOR_SYSTEMD (object); |
352 | |
|
353 | 0 | g_cancellable_cancel (self->cancellable); |
354 | 0 | g_clear_object (&self->cancellable); |
355 | |
|
356 | 0 | g_clear_signal_handler (&self->signal_id, self->proxy); |
357 | 0 | g_clear_object (&self->proxy); |
358 | |
|
359 | 0 | g_clear_signal_handler (&self->fallback_network_changed_id, self->fallback); |
360 | 0 | g_clear_object (&self->fallback); |
361 | |
|
362 | 0 | G_OBJECT_CLASS (g_network_monitor_systemd_parent_class)->dispose (object); |
363 | 0 | } |
364 | | |
365 | | static void |
366 | | g_network_monitor_systemd_class_init (GNetworkMonitorSystemdClass *class) |
367 | 0 | { |
368 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (class); |
369 | |
|
370 | 0 | gobject_class->dispose = g_network_monitor_systemd_dispose; |
371 | 0 | gobject_class->get_property = g_network_monitor_systemd_get_property; |
372 | |
|
373 | 0 | g_object_class_override_property (gobject_class, PROP_NETWORK_AVAILABLE, "network-available"); |
374 | 0 | g_object_class_override_property (gobject_class, PROP_NETWORK_METERED, "network-metered"); |
375 | 0 | g_object_class_override_property (gobject_class, PROP_CONNECTIVITY, "connectivity"); |
376 | 0 | } |
377 | | |
378 | | static void |
379 | | g_network_monitor_systemd_iface_init (GNetworkMonitorInterface *monitor_iface) |
380 | 0 | { |
381 | | /* can_reach() is inherited from the netlink backend. */ |
382 | 0 | } |
383 | | |
384 | | static void |
385 | | g_network_monitor_systemd_initable_iface_init (GInitableIface *iface) |
386 | 0 | { |
387 | 0 | iface->init = g_network_monitor_systemd_initable_init; |
388 | 0 | } |