Debian Codegraph @

src/libmbim-glib/mbim-net-port-manager.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 /*
4 * libmbim-glib -- GLib/GIO based library to control MBIM devices
5 *
6 * Copyright (C) 2021 Aleksander Morgado <[email protected]>
7 *
8 * Based on the QmiNetPortManagerRmnet from libqmi:
9 * Copyright (C) 2020-2021 Eric Caruso <[email protected]>
10 * Copyright (C) 2020-2021 Andrew Lassalle <[email protected]>
11 */
12
13 #include <linux/if_link.h>
14 #include <linux/netlink.h>
15 #include <linux/rtnetlink.h>
16 #include <net/if.h>
17
18 #include "mbim-device.h"
19 #include "mbim-helpers.h"
20 #include "mbim-error-types.h"
21 #include "mbim-net-port-manager.h"
22 #include "mbim-helpers-netlink.h"
23
24 G_DEFINE_ABSTRACT_TYPE (MbimNetPortManager, mbim_net_port_manager, G_TYPE_OBJECT)
25
26 struct _MbimNetPortManagerPrivate {
27 gchar *iface;
28
29 /* Netlink socket */
30 GSocket *socket;
31 GSource *source;
32
33 /* Netlink state */
34 guint current_sequence_id;
35 GHashTable *transactions;
36 };
37
38 /*****************************************************************************/
39
40 void
41 mbim_net_port_manager_add_link (MbimNetPortManager *self,
42 guint session_id,
43 const gchar *base_ifname,
44 const gchar *ifname_prefix,
45 guint timeout,
46 GCancellable *cancellable,
47 GAsyncReadyCallback callback,
48 gpointer user_data)
49 {
50 MBIM_NET_PORT_MANAGER_GET_CLASS (self)->add_link (self,
51 session_id,
52 base_ifname,
53 ifname_prefix,
54 timeout,
55 cancellable,
56 callback,
57 user_data);
58 }
59
60 gchar *
61 mbim_net_port_manager_add_link_finish (MbimNetPortManager *self,
62 guint *session_id,
63 GAsyncResult *res,
64 GError **error)
65 {
66 return MBIM_NET_PORT_MANAGER_GET_CLASS (self)->add_link_finish (self, session_id, res, error);
67 }
68
69 void
70 mbim_net_port_manager_del_link (MbimNetPortManager *self,
71 const gchar *ifname,
72 guint timeout,
73 GCancellable *cancellable,
74 GAsyncReadyCallback callback,
75 gpointer user_data)
76 {
77 MBIM_NET_PORT_MANAGER_GET_CLASS (self)->del_link (self,
78 ifname,
79 timeout,
80 cancellable,
81 callback,
82 user_data);
83 }
84
85 gboolean
86 mbim_net_port_manager_del_link_finish (MbimNetPortManager *self,
87 GAsyncResult *res,
88 GError **error)
89 {
90 return MBIM_NET_PORT_MANAGER_GET_CLASS (self)->del_link_finish (self, res, error);
91 }
92
93 void
94 mbim_net_port_manager_del_all_links (MbimNetPortManager *self,
95 const gchar *base_ifname,
96 GCancellable *cancellable,
97 GAsyncReadyCallback callback,
98 gpointer user_data)
99 {
100 MBIM_NET_PORT_MANAGER_GET_CLASS (self)->del_all_links (self,
101 base_ifname,
102 cancellable,
103 callback,
104 user_data);
105 }
106
107 gboolean
108 mbim_net_port_manager_del_all_links_finish (MbimNetPortManager *self,
109 GAsyncResult *res,
110 GError **error)
111 {
112 return MBIM_NET_PORT_MANAGER_GET_CLASS (self)->del_all_links_finish (self, res, error);
113 }
114
115 gboolean
116 mbim_net_port_manager_list_links (MbimNetPortManager *self,
117 const gchar *base_ifname,
118 GPtrArray **out_links,
119 GError **error)
120 {
121 return MBIM_NET_PORT_MANAGER_GET_CLASS (self)->list_links (self, base_ifname, out_links, error);
122 }
123
124 /*****************************************************************************/
125 /* Default implementations */
126
127 static gboolean
128 net_port_manager_del_link_finish (MbimNetPortManager *self,
129 GAsyncResult *res,
130 GError **error)
131 {
132 return g_task_propagate_boolean (G_TASK (res), error);
133 }
134
135 static NetlinkMessage *
136 netlink_message_del_link (guint ifindex)
137 {
138 NetlinkMessage *msg;
139
140 g_assert (ifindex != 0);
141
142 msg = mbim_helpers_netlink_message_new (RTM_DELLINK, 0);
143 mbim_helpers_netlink_get_message_header (msg)->ifreq.ifi_index = ifindex;
144
145 return msg;
146 }
147
148 static void
149 net_port_manager_del_link (MbimNetPortManager *self,
150 const gchar *ifname,
151 guint timeout,
152 GCancellable *cancellable,
153 GAsyncReadyCallback callback,
154 gpointer user_data)
155 {
156 guint ifindex;
157 NetlinkMessage *msg;
158 NetlinkTransaction *tr;
159 GTask *task;
160 GError *error = NULL;
161 gssize bytes_sent;
162
163 task = g_task_new (self, cancellable, callback, user_data);
164
165 ifindex = if_nametoindex (ifname);
166 if (ifindex == 0) {
167 g_task_return_new_error (task, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
168 "Failed to retrieve interface index for interface %s",
169 ifname);
170 g_object_unref (task);
171 return;
172 }
173
174 msg = netlink_message_del_link (ifindex);
175 /* The task ownership is transferred to the transaction. */
176 tr = mbim_helpers_netlink_transaction_new (&self->priv->current_sequence_id,
177 self->priv->transactions,
178 msg,
179 timeout,
180 task);
181
182 bytes_sent = g_socket_send (self->priv->socket,
183 (const gchar *) msg->data,
184 msg->len,
185 cancellable,
186 &error);
187 mbim_helpers_netlink_message_free (msg);
188
189 if (bytes_sent < 0)
190 mbim_helpers_netlink_transaction_complete_with_error (tr, self->priv->transactions, error);
191
192 g_object_unref (task);
193 }
194
195 typedef struct {
196 GPtrArray *links;
197 guint link_i;
198 } DelAllLinksContext;
199
200 static void
201 del_all_links_context_free (DelAllLinksContext *ctx)
202 {
203 g_clear_pointer (&ctx->links, g_ptr_array_unref);
204 g_slice_free (DelAllLinksContext, ctx);
205 }
206
207 static gboolean
208 net_port_manager_del_all_links_finish (MbimNetPortManager *self,
209 GAsyncResult *res,
210 GError **error)
211 {
212 return g_task_propagate_boolean (G_TASK (res), error);
213 }
214
215 static void delete_next_link (GTask *task);
216
217 static void
218 port_manager_del_link_ready (MbimNetPortManager *self,
219 GAsyncResult *res,
220 GTask *task)
221 {
222 DelAllLinksContext *ctx;
223 GError *error = NULL;
224
225 ctx = g_task_get_task_data (task);
226
227 if (!mbim_net_port_manager_del_link_finish (self, res, &error)) {
228 g_task_return_error (task, error);
229 g_object_unref (task);
230 return;
231 }
232
233 g_ptr_array_remove_index_fast (ctx->links, 0);
234 delete_next_link (task);
235 }
236
237 static void
238 delete_next_link (GTask *task)
239 {
240 MbimNetPortManager *self;
241 DelAllLinksContext *ctx;
242
243 self = g_task_get_source_object (task);
244 ctx = g_task_get_task_data (task);
245
246 if (!ctx->links || ctx->links->len == 0) {
247 g_task_return_boolean (task, TRUE);
248 g_object_unref (task);
249 return;
250 }
251
252 mbim_net_port_manager_del_link (self,
253 g_ptr_array_index (ctx->links, 0),
254 5,
255 g_task_get_cancellable (task),
256 (GAsyncReadyCallback) port_manager_del_link_ready,
257 task);
258 }
259
260 static void
261 net_port_manager_del_all_links (MbimNetPortManager *self,
262 const gchar *base_ifname,
263 GCancellable *cancellable,
264 GAsyncReadyCallback callback,
265 gpointer user_data)
266 {
267 GTask *task;
268 DelAllLinksContext *ctx;
269 GError *error = NULL;
270
271 task = g_task_new (self, cancellable, callback, user_data);
272 ctx = g_slice_new0 (DelAllLinksContext);
273 g_task_set_task_data (task, ctx, (GDestroyNotify)del_all_links_context_free);
274
275 if (!mbim_net_port_manager_list_links (self, base_ifname, &ctx->links, &error)) {
276 g_task_return_error (task, error);
277 g_object_unref (task);
278 return;
279 }
280
281 delete_next_link (task);
282 }
283
284 /*****************************************************************************/
285
286 GHashTable *
287 mbim_net_port_manager_peek_transactions (MbimNetPortManager *self)
288 {
289 return self->priv->transactions;
290 }
291
292 gchar *
293 mbim_net_port_manager_peek_iface (MbimNetPortManager *self)
294 {
295 return self->priv->iface;
296 }
297
298 guint *
299 mbim_net_port_manager_peek_current_sequence_id (MbimNetPortManager *self)
300 {
301 return &self->priv->current_sequence_id;
302 }
303
304 GSocket *
305 mbim_net_port_manager_peek_socket (MbimNetPortManager *self)
306 {
307 return self->priv->socket;
308 }
309
310 void
311 mbim_net_port_manager_common_setup (MbimNetPortManager *self,
312 const gchar *iface,
313 GSocket *gsocket)
314 {
315 self->priv->iface = g_strdup (iface);
316 self->priv->socket = gsocket;
317 self->priv->current_sequence_id = 0;
318 self->priv->transactions =
319 g_hash_table_new_full (g_direct_hash,
320 g_direct_equal,
321 NULL,
322 (GDestroyNotify) mbim_helpers_netlink_transaction_free);
323 mbim_helpers_netlink_set_callback (&self->priv->source,
324 self->priv->socket,
325 self->priv->transactions);
326 }
327
328 gchar *
329 mbim_net_port_manager_util_session_id_to_ifname (const gchar *ifname_prefix,
330 guint session_id)
331 {
332 /* Link names are in the form <PREFIX>.<SESSION ID> */
333 return g_strdup_printf ("%s%u", ifname_prefix, session_id);
334 }
335
336 gboolean
337 mbim_net_port_manager_util_get_first_free_session_id (const gchar *ifname_prefix,
338 guint *session_id)
339 {
340 guint i;
341
342 /* The minimum session id is really 0 (MBIM_DEVICE_SESSION_ID_MIN), but
343 * when we have to automatically allocate a new session id we'll start at
344 * 1, because 0 is also used by the non-muxed setup. */
345 for (i = 1; i <= MBIM_DEVICE_SESSION_ID_MAX; i++) {
346 g_autofree gchar *ifname = NULL;
347
348 ifname = mbim_net_port_manager_util_session_id_to_ifname (ifname_prefix, i);
349 if (!if_nametoindex (ifname)) {
350 *session_id = i;
351 return TRUE;
352 }
353 }
354
355 return FALSE;
356 }
357
358 /*****************************************************************************/
359
360 static void
361 mbim_net_port_manager_init (MbimNetPortManager *self)
362 {
363 /* Initialize private data */
364 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, MBIM_TYPE_NET_PORT_MANAGER, MbimNetPortManagerPrivate);
365 }
366
367 static void
368 finalize (GObject *object)
369 {
370 MbimNetPortManager *self = MBIM_NET_PORT_MANAGER (object);
371
372 g_assert (g_hash_table_size (self->priv->transactions) == 0);
373 g_hash_table_unref (self->priv->transactions);
374 g_free (self->priv->iface);
375
376 G_OBJECT_CLASS (mbim_net_port_manager_parent_class)->finalize (object);
377 }
378
379 static void
380 mbim_net_port_manager_class_init (MbimNetPortManagerClass *klass)
381 {
382 GObjectClass *object_class = G_OBJECT_CLASS (klass);
383
384 g_type_class_add_private (object_class, sizeof (MbimNetPortManagerPrivate));
385
386 object_class->finalize = finalize;
387
388 klass->del_link = net_port_manager_del_link;
389 klass->del_link_finish = net_port_manager_del_link_finish;
390 klass->del_all_links = net_port_manager_del_all_links;
391 klass->del_all_links_finish = net_port_manager_del_all_links_finish;
392 }