Debian Codegraph @

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