Debian Codegraph @

src/libmbim-glib/mbim-net-port-manager-wwan.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 /* These are built-in files, not provided by the kernel headers,
19 * used to add wwan/link symbols if not available */
20 #include <kernel/wwan.h>
21 #include <kernel/if_link.h>
22
23 #include <net/if.h>
24 #include <net/if_arp.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <string.h>
28
29 #include "mbim-device.h"
30 #include "mbim-helpers.h"
31 #include "mbim-error-types.h"
32 #include "mbim-net-port-manager.h"
33 #include "mbim-net-port-manager-wwan.h"
34 #include "mbim-helpers-netlink.h"
35
36 G_DEFINE_TYPE (MbimNetPortManagerWwan, mbim_net_port_manager_wwan, MBIM_TYPE_NET_PORT_MANAGER)
37
38 #define WWAN_DATA_TYPE "wwan"
39
40 /*****************************************************************************/
41
42 static NetlinkMessage *
43 netlink_message_new_link (guint link_id,
44 gchar *ifname,
45 const gchar *base_if_name)
46 {
47 NetlinkMessage *msg;
48 guint linkinfo_pos, datainfo_pos;
49 struct rtattr info;
50
51 msg = mbim_helpers_netlink_message_new (RTM_NEWLINK, NLM_F_CREATE | NLM_F_EXCL);
52 /* IFLA_PARENT_DEV_NAME has type NLA_NUL_STRING */
53 mbim_helpers_netlink_append_attribute_string_null (msg, MBIM_IFLA_PARENT_DEV_NAME, base_if_name);
54 mbim_helpers_netlink_append_attribute_string (msg, IFLA_IFNAME, ifname);
55
56 /* Store the position of the next attribute to adjust its length later. */
57 linkinfo_pos = mbim_helpers_netlink_get_pos_of_next_attr (msg);
58 mbim_helpers_netlink_append_attribute_nested (msg, IFLA_LINKINFO);
59 mbim_helpers_netlink_append_attribute_string (msg, IFLA_INFO_KIND, WWAN_DATA_TYPE);
60
61 /* Store the position of the next attribute to adjust its length later. */
62 datainfo_pos = mbim_helpers_netlink_get_pos_of_next_attr (msg);
63 mbim_helpers_netlink_append_attribute_nested (msg, IFLA_INFO_DATA);
64 mbim_helpers_netlink_append_attribute_uint32 (msg, MBIM_IFLA_WWAN_LINK_ID, link_id);
65
66 /* Use memcpy to preserve byte alignment */
67 memcpy (&info, (char *) msg->data + datainfo_pos, sizeof (struct rtattr));
68 info.rta_len = msg->len - datainfo_pos;
69 memcpy ((char *) msg->data + datainfo_pos, &info, sizeof (struct rtattr));
70
71 memcpy (&info, (char *) msg->data + linkinfo_pos, sizeof (struct rtattr));
72 info.rta_len = msg->len - linkinfo_pos;
73 memcpy ((char *) msg->data + linkinfo_pos, &info, sizeof (struct rtattr));
74
75 return msg;
76 }
77
78 /*****************************************************************************/
79
80 static gboolean
81 mbim_net_port_manager_wwan_list_links (MbimNetPortManager *self,
82 const gchar *base_ifname,
83 GPtrArray **out_links,
84 GError **error)
85 {
86 g_autoptr(GFile) sysfs_file = NULL;
87 g_autofree gchar *sysfs_path = NULL;
88
89 sysfs_path = g_strdup_printf ("/sys/class/net/%s/device/net", base_ifname);
90 sysfs_file = g_file_new_for_path (sysfs_path);
91
92 return mbim_helpers_list_links_wwan (base_ifname, sysfs_file, NULL, NULL, out_links, error);
93 }
94
95 /*****************************************************************************/
96
97 typedef struct {
98 guint session_id;
99 guint link_id;
100 gchar *ifname;
101 } AddLinkContext;
102
103 static void
104 add_link_context_free (AddLinkContext *ctx)
105 {
106 g_free (ctx->ifname);
107 g_free (ctx);
108 }
109
110 static gchar *
111 mbim_net_port_manager_wwan_add_link_finish (MbimNetPortManager *self,
112 guint *session_id,
113 GAsyncResult *res,
114 GError **error)
115 {
116 AddLinkContext *ctx;
117
118 ctx = g_task_get_task_data (G_TASK (res));
119
120 if (!g_task_propagate_boolean (G_TASK (res), error)) {
121 g_prefix_error (error, "Failed to add link with session id %d: ",
122 ctx->session_id);
123 return NULL;
124 }
125
126 *session_id = ctx->session_id;
127 return g_steal_pointer (&ctx->ifname);
128 }
129
130 static void
131 mbim_net_port_manager_wwan_add_link (MbimNetPortManager *self,
132 guint session_id,
133 const gchar *base_ifname,
134 const gchar *ifname_prefix,
135 guint timeout,
136 GCancellable *cancellable,
137 GAsyncReadyCallback callback,
138 gpointer user_data)
139 {
140 NetlinkMessage *msg;
141 NetlinkTransaction *tr;
142 GTask *task;
143 GError *error = NULL;
144 gssize bytes_sent;
145 guint base_if_index;
146 AddLinkContext *ctx;
147
148 task = g_task_new (self, cancellable, callback, user_data);
149
150 ctx = g_new0 (AddLinkContext, 1);
151 ctx->session_id = session_id;
152 g_task_set_task_data (task, ctx, (GDestroyNotify) add_link_context_free);
153
154 if (ctx->session_id == MBIM_DEVICE_SESSION_ID_AUTOMATIC) {
155 if (!mbim_net_port_manager_util_get_first_free_session_id (ifname_prefix, &ctx->session_id)) {
156 g_task_return_new_error (task, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
157 "Failed to find an available session ID");
158 g_object_unref (task);
159 return;
160 }
161 g_debug ("Using dynamic session ID %u", ctx->session_id);
162 } else
163 g_debug ("Using static session ID %u", ctx->session_id);
164
165 base_if_index = if_nametoindex (base_ifname);
166 if (!base_if_index) {
167 g_task_return_new_error (task, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
168 "%s interface is not available",
169 base_ifname);
170 g_object_unref (task);
171 return;
172 }
173
174 ctx->ifname = mbim_net_port_manager_util_session_id_to_ifname (ifname_prefix, ctx->session_id);
175 ctx->link_id = ctx->session_id;
176 g_debug ("Using ifname '%s' and link id %u", ctx->ifname, ctx->link_id);
177
178 msg = netlink_message_new_link (ctx->link_id, ctx->ifname, base_ifname);
179
180 /* The task ownership is transferred to the transaction. */
181 tr = mbim_helpers_netlink_transaction_new (mbim_net_port_manager_peek_current_sequence_id (MBIM_NET_PORT_MANAGER (self)),
182 mbim_net_port_manager_peek_transactions (MBIM_NET_PORT_MANAGER (self)),
183 msg, timeout, task);
184
185 bytes_sent = g_socket_send (mbim_net_port_manager_peek_socket (MBIM_NET_PORT_MANAGER (self)),
186 (const gchar *) msg->data,
187 msg->len,
188 cancellable,
189 &error);
190 mbim_helpers_netlink_message_free (msg);
191
192 if (bytes_sent < 0)
193 mbim_helpers_netlink_transaction_complete_with_error (tr,
194 mbim_net_port_manager_peek_transactions (MBIM_NET_PORT_MANAGER (self)),
195 error);
196
197 g_object_unref (task);
198 }
199
200 /*****************************************************************************/
201
202 MbimNetPortManagerWwan *
203 mbim_net_port_manager_wwan_new (GError **error)
204 {
205 MbimNetPortManagerWwan *self;
206 gint socket_fd;
207 GSocket *gsocket;
208 GError *inner_error = NULL;
209
210 socket_fd = socket (AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
211 if (socket_fd < 0) {
212 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
213 "Failed to create netlink socket");
214 return NULL;
215 }
216
217 gsocket = g_socket_new_from_fd (socket_fd, &inner_error);
218 if (inner_error) {
219 g_debug ("Could not create socket: %s", inner_error->message);
220 close (socket_fd);
221 g_propagate_error (error, inner_error);
222 return NULL;
223 }
224
225 self = g_object_new (MBIM_TYPE_NET_PORT_MANAGER_WWAN, NULL);
226
227 mbim_net_port_manager_common_setup (MBIM_NET_PORT_MANAGER (self), NULL, gsocket);
228
229 return self;
230 }
231
232 static void
233 mbim_net_port_manager_wwan_init (MbimNetPortManagerWwan *self)
234 {
235 }
236
237 static void
238 mbim_net_port_manager_wwan_class_init (MbimNetPortManagerWwanClass *klass)
239 {
240 MbimNetPortManagerClass *net_port_manager_class = MBIM_NET_PORT_MANAGER_CLASS (klass);
241
242 net_port_manager_class->list_links = mbim_net_port_manager_wwan_list_links;
243 net_port_manager_class->add_link = mbim_net_port_manager_wwan_add_link;
244 net_port_manager_class->add_link_finish = mbim_net_port_manager_wwan_add_link_finish;
245 }