Debian Codegraph @

src/libmbim-glib/mbim-helpers-netlink.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) 2022 Daniele Palmas <[email protected]>
7 *
8 * Based on previous work:
9 * Copyright (C) 2020-2021 Eric Caruso <[email protected]>
10 * Copyright (C) 2020-2021 Andrew Lassalle <[email protected]>
11 * Copyright (C) 2021 Aleksander Morgado <[email protected]>
12 */
13
14 #include <linux/if_link.h>
15 #include <linux/netlink.h>
16 #include <linux/rtnetlink.h>
17
18 /* This is a built-in file, not provided by the kernel headers,
19 * used to add wwan symbols if not available */
20 #include <kernel/wwan.h>
21
22 #include <sys/socket.h>
23
24 #include "mbim-helpers-netlink.h"
25
26 /*****************************************************************************/
27 /*
28 * Netlink message construction functions
29 */
30
31 NetlinkHeader *
32 mbim_helpers_netlink_get_message_header (NetlinkMessage *msg)
33 {
34 #if defined(__clang__) || defined(__GNUC__)
35 # pragma GCC diagnostic push
36 # pragma GCC diagnostic ignored "-Wcast-align"
37 #endif
38 return (NetlinkHeader *) (msg->data);
39 #if defined(__clang__) || defined(__GNUC__)
40 # pragma GCC diagnostic pop
41 #endif
42 }
43
44 guint
45 mbim_helpers_netlink_get_pos_of_next_attr (NetlinkMessage *msg)
46 {
47 return NLMSG_ALIGN (msg->len);
48 }
49
50 static void
51 append_netlink_attribute (NetlinkMessage *msg,
52 gushort type,
53 gconstpointer value,
54 gushort len)
55 {
56 guint attr_len;
57 guint old_len;
58 guint next_attr_rel_pos;
59 char *next_attr_abs_pos;
60 struct rtattr new_attr;
61
62 /* Expand the buffer to hold the new attribute */
63 attr_len = RTA_ALIGN (RTA_LENGTH (len));
64 old_len = msg->len;
65 next_attr_rel_pos = mbim_helpers_netlink_get_pos_of_next_attr (msg);
66
67 g_byte_array_set_size (msg, next_attr_rel_pos + attr_len);
68 /* fill new bytes with zero, since some padding is added between attributes. */
69 memset ((char *) msg->data + old_len, 0, msg->len - old_len);
70
71 new_attr.rta_type = type;
72 new_attr.rta_len = RTA_LENGTH (len);
73 next_attr_abs_pos = (char *) msg->data + next_attr_rel_pos;
74 memcpy (next_attr_abs_pos, &new_attr, sizeof (struct rtattr));
75
76 if (value)
77 memcpy (RTA_DATA (next_attr_abs_pos), value, len);
78
79 /* Update the total netlink message length */
80 mbim_helpers_netlink_get_message_header (msg)->msghdr.nlmsg_len = msg->len;
81 }
82
83 void
84 mbim_helpers_netlink_append_attribute_nested (NetlinkMessage *msg,
85 gushort type)
86 {
87 append_netlink_attribute (msg, type, NULL, 0);
88 }
89
90 void
91 mbim_helpers_netlink_append_attribute_string (NetlinkMessage *msg,
92 gushort type,
93 const gchar *value)
94 {
95 append_netlink_attribute (msg, type, value, strlen (value));
96 }
97
98 void
99 mbim_helpers_netlink_append_attribute_string_null (NetlinkMessage *msg,
100 gushort type,
101 const gchar *value)
102 {
103 append_netlink_attribute (msg, type, value, strlen (value) + 1);
104 }
105
106 void
107 mbim_helpers_netlink_append_attribute_uint16 (NetlinkMessage *msg,
108 gushort type,
109 guint16 value)
110 {
111 append_netlink_attribute (msg, type, &value, sizeof (value));
112 }
113
114 void
115 mbim_helpers_netlink_append_attribute_uint32 (NetlinkMessage *msg,
116 gushort type,
117 guint32 value)
118 {
119 append_netlink_attribute (msg, type, &value, sizeof (value));
120 }
121
122 NetlinkMessage *
123 mbim_helpers_netlink_message_new (guint16 type,
124 guint16 extra_flags)
125 {
126 NetlinkMessage *msg;
127 NetlinkHeader *hdr;
128
129 int size = sizeof (NetlinkHeader);
130
131 msg = g_byte_array_new ();
132 g_byte_array_set_size (msg, size);
133 memset ((char *) msg->data, 0, size);
134
135 hdr = mbim_helpers_netlink_get_message_header (msg);
136 hdr->msghdr.nlmsg_len = msg->len;
137 hdr->msghdr.nlmsg_type = type;
138 hdr->msghdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | extra_flags;
139 hdr->ifreq.ifi_family = AF_UNSPEC;
140 return msg;
141 }
142
143 void
144 mbim_helpers_netlink_message_free (NetlinkMessage *msg)
145 {
146 g_byte_array_unref (msg);
147 }
148
149 /*****************************************************************************/
150 /*
151 * Transaction management functions
152 */
153
154 static gboolean
155 transaction_timed_out (NetlinkTransaction *tr,
156 GHashTable *transactions)
157 {
158 GTask *task;
159 guint32 sequence_id;
160
161 task = g_steal_pointer (&tr->completion_task);
162 sequence_id = tr->sequence_id;
163
164 g_hash_table_remove (transactions,
165 GUINT_TO_POINTER (tr->sequence_id));
166
167 g_task_return_new_error (task,
168 G_IO_ERROR,
169 G_IO_ERROR_TIMED_OUT,
170 "Netlink message with sequence ID %u timed out",
171 sequence_id);
172
173 g_object_unref (task);
174 return G_SOURCE_REMOVE;
175 }
176
177 void
178 mbim_helpers_netlink_transaction_complete_with_error (NetlinkTransaction *tr,
179 GHashTable *transactions,
180 GError *error)
181 {
182 GTask *task;
183
184 task = g_steal_pointer (&tr->completion_task);
185
186 g_hash_table_remove (transactions,
187 GUINT_TO_POINTER (tr->sequence_id));
188
189 g_task_return_error (task, error);
190 g_object_unref (task);
191 }
192
193 void
194 mbim_helpers_netlink_transaction_complete (NetlinkTransaction *tr,
195 GHashTable *transactions,
196 gint saved_errno)
197 {
198 GTask *task;
199 guint32 sequence_id;
200
201 task = g_steal_pointer (&tr->completion_task);
202 sequence_id = tr->sequence_id;
203
204 g_hash_table_remove (transactions,
205 GUINT_TO_POINTER (tr->sequence_id));
206
207 if (!saved_errno) {
208 g_task_return_boolean (task, TRUE);
209 } else {
210 g_task_return_new_error (task,
211 G_IO_ERROR,
212 g_io_error_from_errno (saved_errno),
213 "Netlink message with transaction %u failed: %s",
214 sequence_id,
215 g_strerror (saved_errno));
216 }
217
218 g_object_unref (task);
219 }
220
221 void
222 mbim_helpers_netlink_transaction_free (NetlinkTransaction *tr)
223 {
224 g_assert (tr->completion_task == NULL);
225 g_source_destroy (tr->timeout_source);
226 g_source_unref (tr->timeout_source);
227 g_slice_free (NetlinkTransaction, tr);
228 }
229
230 NetlinkTransaction *
231 mbim_helpers_netlink_transaction_new (guint *sequence_id,
232 GHashTable *transactions,
233 NetlinkMessage *msg,
234 guint timeout,
235 GTask *task)
236 {
237 NetlinkTransaction *tr;
238
239 tr = g_slice_new0 (NetlinkTransaction);
240 tr->sequence_id = ++(*sequence_id);
241 mbim_helpers_netlink_get_message_header (msg)->msghdr.nlmsg_seq = tr->sequence_id;
242 if (timeout) {
243 tr->timeout_source = g_timeout_source_new_seconds (timeout);
244 g_source_set_callback (tr->timeout_source,
245 (GSourceFunc) transaction_timed_out,
246 tr,
247 NULL);
248 g_source_attach (tr->timeout_source,
249 g_main_context_get_thread_default ());
250 }
251 tr->completion_task = g_object_ref (task);
252
253 g_hash_table_insert (transactions,
254 GUINT_TO_POINTER (tr->sequence_id),
255 tr);
256 return tr;
257 }
258
259 /*****************************************************************************/
260
261 static gboolean
262 netlink_message_cb (GSocket *socket,
263 GIOCondition condition,
264 GHashTable *transactions)
265 {
266 GError *error = NULL;
267 gchar buf[512];
268 int bytes_received;
269 unsigned int buffer_len;
270 struct nlmsghdr *hdr;
271
272 if (condition & G_IO_HUP || condition & G_IO_ERR) {
273 g_warning ("[netlink] socket connection closed.");
274 return G_SOURCE_REMOVE;
275 }
276
277 bytes_received = g_socket_receive (socket, buf, sizeof (buf), NULL, &error);
278
279 if (bytes_received < 0) {
280 g_warning ("[netlink] socket i/o failure: %s", error->message);
281 g_error_free (error);
282 return G_SOURCE_REMOVE;
283 }
284
285 buffer_len = (unsigned int ) bytes_received;
286
287 #if defined(__clang__) || defined(__GNUC__)
288 # pragma GCC diagnostic push
289 # pragma GCC diagnostic ignored "-Wcast-align"
290 #endif
291 for (hdr = (struct nlmsghdr *) buf;
292 NLMSG_OK (hdr, buffer_len);
293 NLMSG_NEXT (hdr, buffer_len)) {
294 #if defined(__clang__) || defined(__GNUC__)
295 # pragma GCC diagnostic pop
296 #endif
297
298 NetlinkTransaction *tr;
299 struct nlmsgerr *err;
300
301 if (hdr->nlmsg_type != NLMSG_ERROR)
302 continue;
303
304 tr = g_hash_table_lookup (transactions,
305 GUINT_TO_POINTER (hdr->nlmsg_seq));
306 if (!tr)
307 continue;
308
309 err = NLMSG_DATA (buf);
310 mbim_helpers_netlink_transaction_complete (tr, transactions, err->error);
311 }
312 return G_SOURCE_CONTINUE;
313 }
314
315 void
316 mbim_helpers_netlink_set_callback (GSource **source,
317 GSocket *socket,
318 GHashTable *transactions)
319 {
320 *source = g_socket_create_source (socket,
321 G_IO_IN | G_IO_ERR | G_IO_HUP,
322 NULL);
323 g_source_set_callback (*source,
324 (GSourceFunc) netlink_message_cb,
325 transactions,
326 NULL);
327 g_source_attach (*source, g_main_context_get_thread_default ());
328 }