| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* libqmi-glib -- GLib/GIO based library to control QMI devices |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 |
* Boston, MA 02110-1301 USA. |
| 19 |
* |
| 20 |
* Copyright (C) 2020 Eric Caruso <[email protected]> |
| 21 |
* Copyright (C) 2020 Andrew Lassalle <[email protected]> |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <linux/if_link.h> |
| 25 |
#include <linux/netlink.h> |
| 26 |
#include <linux/rtnetlink.h> |
| 27 |
|
| 28 |
/* This is a built-in file, not provided by the kernel headers, |
| 29 |
* used to add unconditional rmnet support if requested in the build */ |
| 30 |
#include <kernel/if_link_rmnet.h> |
| 31 |
|
| 32 |
#include <net/if.h> |
| 33 |
#include <net/if_arp.h> |
| 34 |
|
| 35 |
/* The if_arp.h from libc may not have this symbol yet */ |
| 36 |
#if !defined ARPHRD_RAWIP |
| 37 |
#define ARPHRD_RAWIP 519 |
| 38 |
#endif |
| 39 |
|
| 40 |
#include <sys/socket.h> |
| 41 |
#include <sys/types.h> |
| 42 |
#include <string.h> |
| 43 |
|
| 44 |
#include "qmi-device.h" |
| 45 |
#include "qmi-error-types.h" |
| 46 |
#include "qmi-errors.h" |
| 47 |
#include "qmi-net-port-manager-rmnet.h" |
| 48 |
|
| 49 |
G_DEFINE_TYPE (QmiNetPortManagerRmnet, qmi_net_port_manager_rmnet, QMI_TYPE_NET_PORT_MANAGER) |
| 50 |
|
| 51 |
struct _QmiNetPortManagerRmnetPrivate { |
| 52 |
/* Netlink socket */ |
| 53 |
GSocket *socket; |
| 54 |
GSource *source; |
| 55 |
|
| 56 |
/* Netlink state */ |
| 57 |
guint current_sequence_id; |
| 58 |
GHashTable *transactions; |
| 59 |
}; |
| 60 |
|
| 61 |
#define RMNET_DATA_TYPE "rmnet" |
| 62 |
#define RMNET_MAX_MUX_ID 255 |
| 63 |
|
| 64 |
/*****************************************************************************/ |
| 65 |
|
| 66 |
static gchar * |
| 67 |
mux_id_to_ifname (const gchar *ifname_prefix, |
| 68 |
guint mux_id) |
| 69 |
{ |
| 70 |
/* |
| 71 |
* By convention, ifname_prefix0 corresponds to mux ID 1, and so on. |
| 72 |
* A more defensive implementation of this class could always fetch |
| 73 |
* mux ID via netlink for each existing rmnet interface instead of |
| 74 |
* encoding it in the interface name in this manner, and then the |
| 75 |
* interface name could just be assigned incrementally regardless |
| 76 |
* of the mux ID. */ |
| 77 |
return g_strdup_printf ("%s%u", ifname_prefix, mux_id - 1); |
| 78 |
} |
| 79 |
|
| 80 |
/*****************************************************************************/ |
| 81 |
/* |
| 82 |
* Netlink message construction functions |
| 83 |
*/ |
| 84 |
|
| 85 |
typedef GByteArray NetlinkMessage; |
| 86 |
|
| 87 |
typedef struct { |
| 88 |
struct nlmsghdr msghdr; |
| 89 |
struct ifinfomsg ifreq; |
| 90 |
} NetlinkHeader; |
| 91 |
|
| 92 |
/* Work around 'cast-align' warnings on some architectures */ |
| 93 |
#define CAST_ALIGN(Type, ptr) \ |
| 94 |
({ \ |
| 95 |
gconstpointer const _ptr = (ptr); \ |
| 96 |
\ |
| 97 |
g_assert (((gsize) (gpointer) _ptr % __alignof (Type)) == 0); \ |
| 98 |
\ |
| 99 |
((Type *) _ptr); \ |
| 100 |
}) |
| 101 |
|
| 102 |
static NetlinkHeader * |
| 103 |
netlink_message_header (NetlinkMessage *msg) |
| 104 |
{ |
| 105 |
return CAST_ALIGN (NetlinkHeader, msg->data); |
| 106 |
} |
| 107 |
|
| 108 |
static guint |
| 109 |
get_pos_of_next_attr (NetlinkMessage *msg) |
| 110 |
{ |
| 111 |
return NLMSG_ALIGN (msg->len); |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
append_netlink_attribute (NetlinkMessage *msg, |
| 116 |
gushort type, |
| 117 |
gconstpointer value, |
| 118 |
gushort len) |
| 119 |
{ |
| 120 |
guint attr_len; |
| 121 |
guint old_len; |
| 122 |
guint next_attr_rel_pos; |
| 123 |
char *next_attr_abs_pos; |
| 124 |
struct rtattr new_attr; |
| 125 |
|
| 126 |
/* Expand the buffer to hold the new attribute */ |
| 127 |
attr_len = RTA_ALIGN (RTA_LENGTH (len)); |
| 128 |
old_len = msg->len; |
| 129 |
next_attr_rel_pos = get_pos_of_next_attr (msg); |
| 130 |
|
| 131 |
g_byte_array_set_size (msg, next_attr_rel_pos + attr_len); |
| 132 |
/* fill new bytes with zero, since some padding is added between attributes. */ |
| 133 |
memset ((char *) msg->data + old_len, 0, msg->len - old_len); |
| 134 |
|
| 135 |
new_attr.rta_type = type; |
| 136 |
new_attr.rta_len = RTA_LENGTH (len); |
| 137 |
next_attr_abs_pos = (char *) msg->data + next_attr_rel_pos; |
| 138 |
memcpy (next_attr_abs_pos, &new_attr, sizeof (struct rtattr)); |
| 139 |
|
| 140 |
if (value) |
| 141 |
memcpy (RTA_DATA (next_attr_abs_pos), value, len); |
| 142 |
|
| 143 |
/* Update the total netlink message length */ |
| 144 |
netlink_message_header (msg)->msghdr.nlmsg_len = msg->len; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
append_netlink_attribute_nested (NetlinkMessage *msg, |
| 149 |
gushort type) |
| 150 |
{ |
| 151 |
append_netlink_attribute (msg, type, NULL, 0); |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
append_netlink_attribute_string (NetlinkMessage *msg, |
| 156 |
gushort type, |
| 157 |
const gchar *value) |
| 158 |
{ |
| 159 |
append_netlink_attribute (msg, type, value, strlen (value)); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
append_netlink_attribute_uint16 (NetlinkMessage *msg, |
| 164 |
gushort type, |
| 165 |
guint16 value) |
| 166 |
{ |
| 167 |
append_netlink_attribute (msg, type, &value, sizeof (value)); |
| 168 |
} |
| 169 |
|
| 170 |
static void |
| 171 |
append_netlink_attribute_uint32 (NetlinkMessage *msg, |
| 172 |
gushort type, |
| 173 |
guint32 value) |
| 174 |
{ |
| 175 |
append_netlink_attribute (msg, type, &value, sizeof (value)); |
| 176 |
} |
| 177 |
|
| 178 |
static NetlinkMessage * |
| 179 |
netlink_message_new (guint16 type, |
| 180 |
guint16 extra_flags) |
| 181 |
{ |
| 182 |
NetlinkMessage *msg; |
| 183 |
NetlinkHeader *hdr; |
| 184 |
|
| 185 |
int size = sizeof (NetlinkHeader); |
| 186 |
|
| 187 |
msg = g_byte_array_new (); |
| 188 |
g_byte_array_set_size (msg, size); |
| 189 |
memset ((char *) msg->data, 0, size); |
| 190 |
|
| 191 |
hdr = netlink_message_header (msg); |
| 192 |
hdr->msghdr.nlmsg_len = msg->len; |
| 193 |
hdr->msghdr.nlmsg_type = type; |
| 194 |
hdr->msghdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | extra_flags; |
| 195 |
hdr->ifreq.ifi_family = AF_UNSPEC; |
| 196 |
if (type != RTM_DELLINK) { |
| 197 |
hdr->ifreq.ifi_type = ARPHRD_RAWIP; |
| 198 |
hdr->ifreq.ifi_flags = 0; |
| 199 |
hdr->ifreq.ifi_change = 0xFFFFFFFF; |
| 200 |
} |
| 201 |
return msg; |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
netlink_message_free (NetlinkMessage *msg) |
| 206 |
{ |
| 207 |
g_byte_array_unref (msg); |
| 208 |
} |
| 209 |
|
| 210 |
/*****************************************************************************/ |
| 211 |
|
| 212 |
typedef struct { |
| 213 |
QmiNetPortManagerRmnet *manager; |
| 214 |
guint32 sequence_id; |
| 215 |
GSource *timeout_source; |
| 216 |
GTask *completion_task; |
| 217 |
} Transaction; |
| 218 |
|
| 219 |
static gboolean |
| 220 |
transaction_timed_out (Transaction *tr) |
| 221 |
{ |
| 222 |
GTask *task; |
| 223 |
guint32 sequence_id; |
| 224 |
|
| 225 |
task = g_steal_pointer (&tr->completion_task); |
| 226 |
sequence_id = tr->sequence_id; |
| 227 |
|
| 228 |
g_hash_table_remove (tr->manager->priv->transactions, |
| 229 |
GUINT_TO_POINTER (tr->sequence_id)); |
| 230 |
|
| 231 |
g_task_return_new_error (task, |
| 232 |
G_IO_ERROR, |
| 233 |
G_IO_ERROR_TIMED_OUT, |
| 234 |
"Netlink message with sequence ID %u timed out", |
| 235 |
sequence_id); |
| 236 |
|
| 237 |
g_object_unref (task); |
| 238 |
return G_SOURCE_REMOVE; |
| 239 |
} |
| 240 |
|
| 241 |
static void |
| 242 |
transaction_complete_with_error (Transaction *tr, |
| 243 |
GError *error) |
| 244 |
{ |
| 245 |
GTask *task; |
| 246 |
|
| 247 |
task = g_steal_pointer (&tr->completion_task); |
| 248 |
|
| 249 |
g_hash_table_remove (tr->manager->priv->transactions, |
| 250 |
GUINT_TO_POINTER (tr->sequence_id)); |
| 251 |
|
| 252 |
g_task_return_error (task, error); |
| 253 |
g_object_unref (task); |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
transaction_complete (Transaction *tr, |
| 258 |
gint saved_errno) |
| 259 |
{ |
| 260 |
GTask *task; |
| 261 |
guint32 sequence_id; |
| 262 |
|
| 263 |
task = g_steal_pointer (&tr->completion_task); |
| 264 |
sequence_id = tr->sequence_id; |
| 265 |
|
| 266 |
g_hash_table_remove (tr->manager->priv->transactions, |
| 267 |
GUINT_TO_POINTER (tr->sequence_id)); |
| 268 |
|
| 269 |
if (!saved_errno) { |
| 270 |
g_task_return_boolean (task, TRUE); |
| 271 |
} else { |
| 272 |
g_task_return_new_error (task, |
| 273 |
G_IO_ERROR, |
| 274 |
g_io_error_from_errno (saved_errno), |
| 275 |
"Netlink message with transaction %u failed: %s", |
| 276 |
sequence_id, |
| 277 |
g_strerror (saved_errno)); |
| 278 |
} |
| 279 |
|
| 280 |
g_object_unref (task); |
| 281 |
} |
| 282 |
|
| 283 |
static void |
| 284 |
transaction_free (Transaction *tr) |
| 285 |
{ |
| 286 |
g_assert (tr->completion_task == NULL); |
| 287 |
g_source_destroy (tr->timeout_source); |
| 288 |
g_source_unref (tr->timeout_source); |
| 289 |
g_slice_free (Transaction, tr); |
| 290 |
} |
| 291 |
|
| 292 |
static Transaction * |
| 293 |
transaction_new (QmiNetPortManagerRmnet *manager, |
| 294 |
NetlinkMessage *msg, |
| 295 |
guint timeout, |
| 296 |
GTask *task) |
| 297 |
{ |
| 298 |
Transaction *tr; |
| 299 |
|
| 300 |
tr = g_slice_new0 (Transaction); |
| 301 |
tr->manager = manager; |
| 302 |
tr->sequence_id = ++manager->priv->current_sequence_id; |
| 303 |
netlink_message_header (msg)->msghdr.nlmsg_seq = tr->sequence_id; |
| 304 |
if (timeout) { |
| 305 |
tr->timeout_source = g_timeout_source_new_seconds (timeout); |
| 306 |
g_source_set_callback (tr->timeout_source, |
| 307 |
(GSourceFunc) transaction_timed_out, |
| 308 |
tr, |
| 309 |
NULL); |
| 310 |
g_source_attach (tr->timeout_source, |
| 311 |
g_main_context_get_thread_default ()); |
| 312 |
} |
| 313 |
tr->completion_task = g_object_ref (task); |
| 314 |
|
| 315 |
g_hash_table_insert (manager->priv->transactions, |
| 316 |
GUINT_TO_POINTER (tr->sequence_id), |
| 317 |
tr); |
| 318 |
return tr; |
| 319 |
} |
| 320 |
|
| 321 |
/*****************************************************************************/ |
| 322 |
|
| 323 |
static NetlinkMessage * |
| 324 |
netlink_message_new_link (guint mux_id, |
| 325 |
gchar *ifname, |
| 326 |
guint base_if_index, |
| 327 |
guint rmnet_flags, |
| 328 |
guint rmnet_mask) |
| 329 |
{ |
| 330 |
NetlinkMessage *msg; |
| 331 |
guint linkinfo_pos, datainfo_pos; |
| 332 |
struct rtattr info; |
| 333 |
struct ifla_rmnet_flags flags; |
| 334 |
|
| 335 |
g_assert (mux_id != QMI_DEVICE_MUX_ID_UNBOUND); |
| 336 |
|
| 337 |
msg = netlink_message_new (RTM_NEWLINK, NLM_F_CREATE | NLM_F_EXCL); |
| 338 |
append_netlink_attribute_uint32 (msg, IFLA_LINK, base_if_index); |
| 339 |
append_netlink_attribute_string (msg, IFLA_IFNAME, ifname); |
| 340 |
|
| 341 |
/* Store the position of the next attribute to adjust its length later. */ |
| 342 |
linkinfo_pos = get_pos_of_next_attr (msg); |
| 343 |
append_netlink_attribute_nested (msg, IFLA_LINKINFO); |
| 344 |
append_netlink_attribute_string (msg, IFLA_INFO_KIND, RMNET_DATA_TYPE); |
| 345 |
|
| 346 |
/* Store the position of the next attribute to adjust its length later. */ |
| 347 |
datainfo_pos = get_pos_of_next_attr (msg); |
| 348 |
append_netlink_attribute_nested (msg, IFLA_INFO_DATA); |
| 349 |
append_netlink_attribute_uint16 (msg, IFLA_RMNET_MUX_ID, mux_id); |
| 350 |
|
| 351 |
flags.flags = rmnet_flags; |
| 352 |
flags.mask = rmnet_mask; |
| 353 |
append_netlink_attribute (msg, IFLA_RMNET_FLAGS, &flags, sizeof (struct ifla_rmnet_flags)); |
| 354 |
|
| 355 |
/* Use memcpy to preserve byte alignment */ |
| 356 |
memcpy (&info, (char *) msg->data + datainfo_pos, sizeof (struct rtattr)); |
| 357 |
info.rta_len = msg->len - datainfo_pos; |
| 358 |
memcpy ((char *) msg->data + datainfo_pos, &info, sizeof (struct rtattr)); |
| 359 |
|
| 360 |
memcpy (&info, (char *) msg->data + linkinfo_pos, sizeof (struct rtattr)); |
| 361 |
info.rta_len = msg->len - linkinfo_pos; |
| 362 |
memcpy ((char *) msg->data + linkinfo_pos, &info, sizeof (struct rtattr)); |
| 363 |
|
| 364 |
return msg; |
| 365 |
} |
| 366 |
|
| 367 |
static NetlinkMessage * |
| 368 |
netlink_message_del_link (guint ifindex) |
| 369 |
{ |
| 370 |
NetlinkMessage *msg; |
| 371 |
|
| 372 |
g_assert (ifindex != 0); |
| 373 |
|
| 374 |
msg = netlink_message_new (RTM_DELLINK, 0); |
| 375 |
netlink_message_header (msg)->ifreq.ifi_index = ifindex; |
| 376 |
|
| 377 |
return msg; |
| 378 |
} |
| 379 |
|
| 380 |
/*****************************************************************************/ |
| 381 |
|
| 382 |
static gboolean |
| 383 |
netlink_message_cb (GSocket *socket, |
| 384 |
GIOCondition condition, |
| 385 |
QmiNetPortManagerRmnet *self) |
| 386 |
{ |
| 387 |
GError *error = NULL; |
| 388 |
gchar buf[512]; |
| 389 |
int bytes_received; |
| 390 |
unsigned int buffer_len; |
| 391 |
struct nlmsghdr *hdr; |
| 392 |
|
| 393 |
if (condition & G_IO_HUP || condition & G_IO_ERR) { |
| 394 |
g_warning ("[netlink] socket connection closed."); |
| 395 |
return G_SOURCE_REMOVE; |
| 396 |
} |
| 397 |
|
| 398 |
bytes_received = g_socket_receive (socket, buf, sizeof (buf), NULL, &error); |
| 399 |
|
| 400 |
if (bytes_received < 0) { |
| 401 |
g_warning ("[netlink] socket i/o failure: %s", error->message); |
| 402 |
g_error_free (error); |
| 403 |
return G_SOURCE_REMOVE; |
| 404 |
} |
| 405 |
|
| 406 |
buffer_len = (unsigned int ) bytes_received; |
| 407 |
for (hdr = CAST_ALIGN (struct nlmsghdr, buf); NLMSG_OK (hdr, buffer_len); |
| 408 |
NLMSG_NEXT (hdr, buffer_len)) { |
| 409 |
Transaction *tr; |
| 410 |
struct nlmsgerr *err; |
| 411 |
|
| 412 |
if (hdr->nlmsg_type != NLMSG_ERROR) |
| 413 |
continue; |
| 414 |
|
| 415 |
tr = g_hash_table_lookup (self->priv->transactions, |
| 416 |
GUINT_TO_POINTER (hdr->nlmsg_seq)); |
| 417 |
if (!tr) |
| 418 |
continue; |
| 419 |
|
| 420 |
err = NLMSG_DATA (buf); |
| 421 |
transaction_complete (tr, err->error); |
| 422 |
} |
| 423 |
return G_SOURCE_CONTINUE; |
| 424 |
} |
| 425 |
|
| 426 |
/*****************************************************************************/ |
| 427 |
|
| 428 |
static guint |
| 429 |
get_first_free_mux_id (QmiNetPortManagerRmnet *self, |
| 430 |
const gchar *ifname_prefix, |
| 431 |
guint initial_value) |
| 432 |
{ |
| 433 |
guint i; |
| 434 |
|
| 435 |
/* Note that this function does not actually need to use the |
| 436 |
* QmiNetPortManagerRmnet at the moment. But it will if we ever do rewrite it |
| 437 |
* to inspect mux IDs of existing rmnet interfaces, so we will pass it |
| 438 |
* for now. */ |
| 439 |
|
| 440 |
for (i = initial_value; i <= QMI_DEVICE_MUX_ID_MAX; i++) { |
| 441 |
gchar *ifname; |
| 442 |
gboolean mux_id_is_free; |
| 443 |
|
| 444 |
ifname = mux_id_to_ifname (ifname_prefix, i); |
| 445 |
mux_id_is_free = !if_nametoindex (ifname); |
| 446 |
g_free (ifname); |
| 447 |
if (mux_id_is_free) |
| 448 |
return i; |
| 449 |
} |
| 450 |
|
| 451 |
return QMI_DEVICE_MUX_ID_UNBOUND; |
| 452 |
} |
| 453 |
|
| 454 |
/*****************************************************************************/ |
| 455 |
typedef struct { |
| 456 |
guint mux_id; |
| 457 |
gchar *ifname; |
| 458 |
} AddLinkContext; |
| 459 |
|
| 460 |
static void |
| 461 |
add_link_context_free (AddLinkContext *ctx) |
| 462 |
{ |
| 463 |
g_free (ctx->ifname); |
| 464 |
g_free (ctx); |
| 465 |
} |
| 466 |
|
| 467 |
static gchar * |
| 468 |
net_port_manager_add_link_finish (QmiNetPortManager *self, |
| 469 |
guint *mux_id, |
| 470 |
GAsyncResult *res, |
| 471 |
GError **error) |
| 472 |
{ |
| 473 |
AddLinkContext *ctx; |
| 474 |
|
| 475 |
ctx = g_task_get_task_data (G_TASK (res)); |
| 476 |
|
| 477 |
if (!g_task_propagate_boolean (G_TASK (res), error)) { |
| 478 |
g_prefix_error (error, "Failed to add link with mux id %d: ", |
| 479 |
ctx->mux_id); |
| 480 |
return NULL; |
| 481 |
} |
| 482 |
|
| 483 |
*mux_id = ctx->mux_id; |
| 484 |
return g_steal_pointer (&ctx->ifname); |
| 485 |
} |
| 486 |
|
| 487 |
static void |
| 488 |
net_port_manager_add_link (QmiNetPortManager *_self, |
| 489 |
guint mux_id, |
| 490 |
guint initial_mux_id, |
| 491 |
const gchar *base_ifname, |
| 492 |
const gchar *ifname_prefix, |
| 493 |
QmiDeviceAddLinkFlags flags, |
| 494 |
guint timeout, |
| 495 |
GCancellable *cancellable, |
| 496 |
GAsyncReadyCallback callback, |
| 497 |
gpointer user_data) |
| 498 |
{ |
| 499 |
QmiNetPortManagerRmnet *self = QMI_NET_PORT_MANAGER_RMNET (_self); |
| 500 |
NetlinkMessage *msg; |
| 501 |
Transaction *tr; |
| 502 |
GTask *task; |
| 503 |
GError *error = NULL; |
| 504 |
gssize bytes_sent; |
| 505 |
guint base_if_index; |
| 506 |
AddLinkContext *ctx; |
| 507 |
guint rmnet_flags; |
| 508 |
guint rmnet_mask; |
| 509 |
|
| 510 |
task = g_task_new (self, cancellable, callback, user_data); |
| 511 |
|
| 512 |
ctx = g_new0 (AddLinkContext, 1); |
| 513 |
ctx->mux_id = mux_id; |
| 514 |
g_task_set_task_data (task, ctx, (GDestroyNotify) add_link_context_free); |
| 515 |
|
| 516 |
if (ctx->mux_id == QMI_DEVICE_MUX_ID_UNBOUND) { |
| 517 |
g_task_return_new_error (task, |
| 518 |
QMI_CORE_ERROR, |
| 519 |
QMI_CORE_ERROR_FAILED, |
| 520 |
"Tried to create interface for unbound mux ID"); |
| 521 |
g_object_unref (task); |
| 522 |
return; |
| 523 |
} |
| 524 |
|
| 525 |
if (ctx->mux_id == QMI_DEVICE_MUX_ID_AUTOMATIC) { |
| 526 |
ctx->mux_id = get_first_free_mux_id (self, ifname_prefix, initial_mux_id); |
| 527 |
|
| 528 |
g_debug ("Using dynamic mux ID %u", ctx->mux_id); |
| 529 |
if (ctx->mux_id == QMI_DEVICE_MUX_ID_UNBOUND) { |
| 530 |
g_task_return_new_error (task, |
| 531 |
QMI_CORE_ERROR, |
| 532 |
QMI_CORE_ERROR_FAILED, |
| 533 |
"Failed to find an available mux ID"); |
| 534 |
g_object_unref (task); |
| 535 |
return; |
| 536 |
} |
| 537 |
} else |
| 538 |
g_debug ("Using static mux ID %u", ctx->mux_id); |
| 539 |
|
| 540 |
base_if_index = if_nametoindex (base_ifname); |
| 541 |
if (!base_if_index) { |
| 542 |
g_task_return_new_error (task, |
| 543 |
QMI_CORE_ERROR, |
| 544 |
QMI_CORE_ERROR_FAILED, |
| 545 |
"%s interface is not available", |
| 546 |
base_ifname); |
| 547 |
g_object_unref (task); |
| 548 |
return; |
| 549 |
} |
| 550 |
|
| 551 |
ctx->ifname = mux_id_to_ifname (ifname_prefix, ctx->mux_id); |
| 552 |
|
| 553 |
/* Convert flags from libqmi API to rmnet API */ |
| 554 |
rmnet_flags = RMNET_FLAGS_INGRESS_DEAGGREGATION; |
| 555 |
if (flags & QMI_DEVICE_ADD_LINK_FLAGS_INGRESS_MAP_CKSUMV4) |
| 556 |
rmnet_flags |= RMNET_FLAGS_INGRESS_MAP_CKSUMV4; |
| 557 |
if (flags & QMI_DEVICE_ADD_LINK_FLAGS_EGRESS_MAP_CKSUMV4) |
| 558 |
rmnet_flags |= RMNET_FLAGS_EGRESS_MAP_CKSUMV4; |
| 559 |
if (flags & QMI_DEVICE_ADD_LINK_FLAGS_INGRESS_MAP_CKSUMV5) |
| 560 |
rmnet_flags |= RMNET_FLAGS_INGRESS_MAP_CKSUMV5; |
| 561 |
if (flags & QMI_DEVICE_ADD_LINK_FLAGS_EGRESS_MAP_CKSUMV5) |
| 562 |
rmnet_flags |= RMNET_FLAGS_EGRESS_MAP_CKSUMV5; |
| 563 |
|
| 564 |
rmnet_mask = (RMNET_FLAGS_EGRESS_MAP_CKSUMV4 | |
| 565 |
RMNET_FLAGS_INGRESS_MAP_CKSUMV4 | |
| 566 |
RMNET_FLAGS_EGRESS_MAP_CKSUMV5 | |
| 567 |
RMNET_FLAGS_INGRESS_MAP_CKSUMV5 | |
| 568 |
RMNET_FLAGS_INGRESS_DEAGGREGATION); |
| 569 |
|
| 570 |
msg = netlink_message_new_link (ctx->mux_id, ctx->ifname, base_if_index, rmnet_flags, rmnet_mask); |
| 571 |
|
| 572 |
/* The task ownership is transferred to the transaction. */ |
| 573 |
tr = transaction_new (self, msg, timeout, task); |
| 574 |
|
| 575 |
bytes_sent = g_socket_send (self->priv->socket, |
| 576 |
(const gchar *) msg->data, |
| 577 |
msg->len, |
| 578 |
cancellable, |
| 579 |
&error); |
| 580 |
netlink_message_free (msg); |
| 581 |
|
| 582 |
if (bytes_sent < 0) |
| 583 |
transaction_complete_with_error (tr, error); |
| 584 |
|
| 585 |
g_object_unref (task); |
| 586 |
} |
| 587 |
|
| 588 |
static gboolean |
| 589 |
net_port_manager_del_link_finish (QmiNetPortManager *self, |
| 590 |
GAsyncResult *res, |
| 591 |
GError **error) |
| 592 |
{ |
| 593 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 594 |
} |
| 595 |
|
| 596 |
static void |
| 597 |
net_port_manager_del_link (QmiNetPortManager *_self, |
| 598 |
const gchar *ifname, |
| 599 |
guint mux_id_unused, |
| 600 |
guint timeout, |
| 601 |
GCancellable *cancellable, |
| 602 |
GAsyncReadyCallback callback, |
| 603 |
gpointer user_data) |
| 604 |
{ |
| 605 |
QmiNetPortManagerRmnet *self = QMI_NET_PORT_MANAGER_RMNET (_self); |
| 606 |
guint ifindex; |
| 607 |
NetlinkMessage *msg; |
| 608 |
Transaction *tr; |
| 609 |
GTask *task; |
| 610 |
GError *error = NULL; |
| 611 |
gssize bytes_sent; |
| 612 |
|
| 613 |
task = g_task_new (self, cancellable, callback, user_data); |
| 614 |
|
| 615 |
ifindex = if_nametoindex (ifname); |
| 616 |
if (ifindex == 0) { |
| 617 |
g_task_return_new_error (task, |
| 618 |
QMI_CORE_ERROR, |
| 619 |
QMI_CORE_ERROR_FAILED, |
| 620 |
"Failed to retrieve interface index for interface:%s", |
| 621 |
ifname); |
| 622 |
g_object_unref (task); |
| 623 |
return; |
| 624 |
} |
| 625 |
|
| 626 |
msg = netlink_message_del_link (ifindex); |
| 627 |
/* The task ownership is transferred to the transaction. */ |
| 628 |
tr = transaction_new (self, msg, timeout, task); |
| 629 |
|
| 630 |
bytes_sent = g_socket_send (self->priv->socket, |
| 631 |
(const gchar *) msg->data, |
| 632 |
msg->len, |
| 633 |
cancellable, |
| 634 |
&error); |
| 635 |
netlink_message_free (msg); |
| 636 |
|
| 637 |
if (bytes_sent < 0) |
| 638 |
transaction_complete_with_error (tr, error); |
| 639 |
|
| 640 |
g_object_unref (task); |
| 641 |
} |
| 642 |
|
| 643 |
/*****************************************************************************/ |
| 644 |
|
| 645 |
QmiNetPortManagerRmnet * |
| 646 |
qmi_net_port_manager_rmnet_new (GError **error) |
| 647 |
{ |
| 648 |
QmiNetPortManagerRmnet *self; |
| 649 |
gint socket_fd; |
| 650 |
GSocket *gsocket; |
| 651 |
GError *inner_error = NULL; |
| 652 |
|
| 653 |
socket_fd = socket (AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
| 654 |
if (socket_fd < 0) { |
| 655 |
g_set_error (error, |
| 656 |
QMI_CORE_ERROR, |
| 657 |
QMI_CORE_ERROR_FAILED, |
| 658 |
"Failed to create netlink socket"); |
| 659 |
return NULL; |
| 660 |
} |
| 661 |
|
| 662 |
gsocket = g_socket_new_from_fd (socket_fd, &inner_error); |
| 663 |
if (inner_error) { |
| 664 |
g_debug ("Could not create socket: %s", inner_error->message); |
| 665 |
close (socket_fd); |
| 666 |
g_propagate_error (error, inner_error); |
| 667 |
return NULL; |
| 668 |
} |
| 669 |
|
| 670 |
self = g_object_new (QMI_TYPE_NET_PORT_MANAGER_RMNET, NULL); |
| 671 |
self->priv->socket = gsocket; |
| 672 |
self->priv->source = g_socket_create_source (self->priv->socket, |
| 673 |
G_IO_IN | G_IO_ERR | G_IO_HUP, |
| 674 |
NULL); |
| 675 |
g_source_set_callback (self->priv->source, |
| 676 |
(GSourceFunc) netlink_message_cb, |
| 677 |
self, |
| 678 |
NULL); |
| 679 |
g_source_attach (self->priv->source, g_main_context_get_thread_default ()); |
| 680 |
|
| 681 |
self->priv->current_sequence_id = 0; |
| 682 |
self->priv->transactions = |
| 683 |
g_hash_table_new_full (g_direct_hash, |
| 684 |
g_direct_equal, |
| 685 |
NULL, |
| 686 |
(GDestroyNotify) transaction_free); |
| 687 |
return self; |
| 688 |
} |
| 689 |
|
| 690 |
/*****************************************************************************/ |
| 691 |
|
| 692 |
static void |
| 693 |
qmi_net_port_manager_rmnet_init (QmiNetPortManagerRmnet *self) |
| 694 |
{ |
| 695 |
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, |
| 696 |
QMI_TYPE_NET_PORT_MANAGER_RMNET, |
| 697 |
QmiNetPortManagerRmnetPrivate); |
| 698 |
} |
| 699 |
|
| 700 |
static void |
| 701 |
dispose (GObject *object) |
| 702 |
{ |
| 703 |
QmiNetPortManagerRmnet *self = QMI_NET_PORT_MANAGER_RMNET (object); |
| 704 |
|
| 705 |
g_assert (g_hash_table_size (self->priv->transactions) == 0); |
| 706 |
|
| 707 |
g_clear_pointer (&self->priv->transactions, g_hash_table_unref); |
| 708 |
if (self->priv->source) |
| 709 |
g_source_destroy (self->priv->source); |
| 710 |
g_clear_pointer (&self->priv->source, g_source_unref); |
| 711 |
g_clear_object (&self->priv->socket); |
| 712 |
|
| 713 |
G_OBJECT_CLASS (qmi_net_port_manager_rmnet_parent_class)->dispose (object); |
| 714 |
} |
| 715 |
|
| 716 |
static void |
| 717 |
qmi_net_port_manager_rmnet_class_init (QmiNetPortManagerRmnetClass *klass) |
| 718 |
{ |
| 719 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 720 |
QmiNetPortManagerClass *net_port_manager_class = QMI_NET_PORT_MANAGER_CLASS (klass); |
| 721 |
|
| 722 |
g_type_class_add_private (object_class, sizeof (QmiNetPortManagerRmnetPrivate)); |
| 723 |
|
| 724 |
object_class->dispose = dispose; |
| 725 |
|
| 726 |
net_port_manager_class->add_link = net_port_manager_add_link; |
| 727 |
net_port_manager_class->add_link_finish = net_port_manager_add_link_finish; |
| 728 |
net_port_manager_class->del_link = net_port_manager_del_link; |
| 729 |
net_port_manager_class->del_link_finish = net_port_manager_del_link_finish; |
| 730 |
} |