1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <glib.h>
#include "lib/bluetooth.h"
#include "lib/bnep.h"
#include "lib/sdp.h"
#include "gdbus/gdbus.h"
#include "btio/btio.h"
#include "src/log.h"
#include "src/dbus-common.h"
#include "src/adapter.h"
#include "src/device.h"
#include "src/profile.h"
#include "src/service.h"
#include "src/error.h"
#include "lib/uuid.h"
#include "bnep.h"
#include "connection.h"
#define NETWORK_PEER_INTERFACE "org.bluez.Network1"
#define BNEP_INTERFACE "bnep%d"
typedef enum {
CONNECTED,
CONNECTING,
DISCONNECTED
} conn_state;
struct network_peer {
struct btd_device *device;
GSList *connections;
};
struct network_conn {
struct btd_service *service;
char dev[16]; /* Interface name */
uint16_t id; /* Role: Service Class Identifier */
conn_state state;
GIOChannel *io;
guint dc_id;
struct network_peer *peer;
DBusMessage *connect;
struct bnep *session;
};
static GSList *peers = NULL;
static uint16_t get_pan_srv_id(const char *svc)
{
if (!strcasecmp(svc, "panu") || !strcasecmp(svc, PANU_UUID))
return BNEP_SVC_PANU;
if (!strcasecmp(svc, "nap") || !strcasecmp(svc, NAP_UUID))
return BNEP_SVC_NAP;
if (!strcasecmp(svc, "gn") || !strcasecmp(svc, GN_UUID))
return BNEP_SVC_GN;
return 0;
}
static struct network_peer *find_peer(GSList *list, struct btd_device *device)
{
for (; list; list = list->next) {
struct network_peer *peer = list->data;
if (peer->device == device)
return peer;
}
return NULL;
}
static struct network_conn *find_connection_by_state(GSList *list,
conn_state state)
{
for (; list; list = list->next) {
struct network_conn *nc = list->data;
if (nc->state == state)
return nc;
}
return NULL;
}
static void bnep_disconn_cb(gpointer data)
{
struct network_conn *nc = data;
DBusConnection *conn = btd_get_dbus_connection();
const char *path = device_get_path(nc->peer->device);
g_dbus_emit_property_changed(conn, path,
NETWORK_PEER_INTERFACE, "Connected");
g_dbus_emit_property_changed(conn, path,
NETWORK_PEER_INTERFACE, "Interface");
g_dbus_emit_property_changed(conn, path,
NETWORK_PEER_INTERFACE, "UUID");
device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
nc->dc_id = 0;
btd_service_disconnecting_complete(nc->service, 0);
info("%s disconnected", nc->dev);
nc->state = DISCONNECTED;
memset(nc->dev, 0, sizeof(nc->dev));
strncpy(nc->dev, BNEP_INTERFACE, 16);
nc->dev[15] = '\0';
bnep_free(nc->session);
nc->session = NULL;
}
static void local_connect_cb(struct network_conn *nc, int err)
{
DBusConnection *conn = btd_get_dbus_connection();
const char *pdev = nc->dev;
if (err < 0) {
DBusMessage *reply = btd_error_failed(nc->connect,
strerror(-err));
g_dbus_send_message(conn, reply);
} else {
g_dbus_send_reply(conn, nc->connect, DBUS_TYPE_STRING, &pdev,
DBUS_TYPE_INVALID);
}
dbus_message_unref(nc->connect);
nc->connect = NULL;
}
static void cancel_connection(struct network_conn *nc, int err)
{
btd_service_connecting_complete(nc->service, err);
if (nc->connect)
local_connect_cb(nc, err);
if (nc->io) {
g_io_channel_shutdown(nc->io, FALSE, NULL);
g_io_channel_unref(nc->io);
nc->io = NULL;
}
if (nc->state == CONNECTED)
bnep_disconnect(nc->session);
bnep_free(nc->session);
nc->session = NULL;
nc->state = DISCONNECTED;
}
static void connection_destroy(DBusConnection *conn, void *user_data)
{
struct network_conn *nc = user_data;
cancel_connection(nc, -EIO);
}
static void disconnect_cb(struct btd_device *device, gboolean removal,
void *user_data)
{
struct network_conn *nc = user_data;
info("Network: disconnect %s", device_get_path(nc->peer->device));
connection_destroy(NULL, user_data);
}
static void bnep_conn_cb(char *iface, int err, void *data)
{
struct network_conn *nc = data;
const char *path;
DBusConnection *conn;
DBG("");
if (err < 0) {
error("connect failed %s", strerror(-err));
goto failed;
}
memcpy(nc->dev, iface, sizeof(nc->dev));
info("%s connected", nc->dev);
btd_service_connecting_complete(nc->service, 0);
if (nc->connect)
local_connect_cb(nc, 0);
conn = btd_get_dbus_connection();
path = device_get_path(nc->peer->device);
g_dbus_emit_property_changed(conn, path,
NETWORK_PEER_INTERFACE, "Connected");
g_dbus_emit_property_changed(conn, path,
NETWORK_PEER_INTERFACE, "Interface");
g_dbus_emit_property_changed(conn, path,
NETWORK_PEER_INTERFACE, "UUID");
nc->state = CONNECTED;
nc->dc_id = device_add_disconnect_watch(nc->peer->device, disconnect_cb,
nc, NULL);
return;
failed:
cancel_connection(nc, -EIO);
}
static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
{
struct network_conn *nc = data;
int sk, perr;
if (err) {
error("%s", err->message);
goto failed;
}
sk = g_io_channel_unix_get_fd(nc->io);
nc->session = bnep_new(sk, BNEP_SVC_PANU, nc->id, BNEP_INTERFACE);
if (!nc->session)
goto failed;
perr = bnep_connect(nc->session, bnep_conn_cb, bnep_disconn_cb, nc, nc);
if (perr < 0) {
error("bnep connect(): %s (%d)", strerror(-perr), -perr);
goto failed;
}
if (nc->io) {
g_io_channel_unref(nc->io);
nc->io = NULL;
}
return;
failed:
cancel_connection(nc, -EIO);
}
static DBusMessage *local_connect(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct network_peer *peer = data;
struct btd_service *service;
struct network_conn *nc;
const char *svc;
uint16_t id;
int err;
char uuid_str[MAX_LEN_UUID_STR];
bt_uuid_t uuid16, uuid128;
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &svc,
DBUS_TYPE_INVALID) == FALSE)
return btd_error_invalid_args(msg);
id = get_pan_srv_id(svc);
bt_uuid16_create(&uuid16, id);
bt_uuid_to_uuid128(&uuid16, &uuid128);
if (bt_uuid_to_string(&uuid128, uuid_str, MAX_LEN_UUID_STR) < 0)
return btd_error_invalid_args(msg);
service = btd_device_get_service(peer->device, uuid_str);
if (service == NULL)
return btd_error_not_supported(msg);
nc = btd_service_get_user_data(service);
if (nc->connect != NULL)
return btd_error_busy(msg);
err = connection_connect(nc->service);
if (err < 0)
return btd_error_failed(msg, strerror(-err));
nc->connect = dbus_message_ref(msg);
return NULL;
}
/* Connect and initiate BNEP session */
int connection_connect(struct btd_service *svc)
{
struct network_conn *nc = btd_service_get_user_data(svc);
struct network_peer *peer = nc->peer;
uint16_t id = get_pan_srv_id(btd_service_get_profile(svc)->remote_uuid);
GError *err = NULL;
const bdaddr_t *src;
const bdaddr_t *dst;
DBG("id %u", id);
if (nc->state != DISCONNECTED)
return -EALREADY;
src = btd_adapter_get_address(device_get_adapter(peer->device));
dst = device_get_address(peer->device);
nc->io = bt_io_connect(connect_cb, nc,
NULL, &err,
BT_IO_OPT_SOURCE_BDADDR, src,
BT_IO_OPT_DEST_BDADDR, dst,
BT_IO_OPT_PSM, BNEP_PSM,
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
BT_IO_OPT_OMTU, BNEP_MTU,
BT_IO_OPT_IMTU, BNEP_MTU,
BT_IO_OPT_INVALID);
if (!nc->io)
return -EIO;
nc->state = CONNECTING;
return 0;
}
int connection_disconnect(struct btd_service *svc)
{
struct network_conn *nc = btd_service_get_user_data(svc);
if (nc->state == DISCONNECTED)
return 0;
connection_destroy(NULL, nc);
return 0;
}
static DBusMessage *local_disconnect(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct network_peer *peer = data;
GSList *l;
for (l = peer->connections; l; l = l->next) {
struct network_conn *nc = l->data;
int err;
if (nc->state == DISCONNECTED)
continue;
err = connection_disconnect(nc->service);
if (err < 0)
return btd_error_failed(msg, strerror(-err));
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}
return btd_error_not_connected(msg);
}
static gboolean
network_property_get_connected(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct network_peer *peer = data;
struct network_conn *nc;
dbus_bool_t connected;
nc = find_connection_by_state(peer->connections, CONNECTED);
connected = nc != NULL ? TRUE : FALSE;
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &connected);
return TRUE;
}
static gboolean network_property_exists(const GDBusPropertyTable *property,
void *data)
{
struct network_peer *peer = data;
struct network_conn *nc;
nc = find_connection_by_state(peer->connections, CONNECTED);
if (nc == NULL)
return FALSE;
return TRUE;
}
static gboolean
network_property_get_interface(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct network_peer *peer = data;
struct network_conn *nc;
const char *iface;
nc = find_connection_by_state(peer->connections, CONNECTED);
if (nc == NULL)
return FALSE;
iface = nc->dev;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &iface);
return TRUE;
}
static gboolean network_property_get_uuid(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *data)
{
struct network_peer *peer = data;
struct network_conn *nc;
char uuid_str[MAX_LEN_UUID_STR];
const char *uuid = uuid_str;
bt_uuid_t uuid16, uuid128;
nc = find_connection_by_state(peer->connections, CONNECTED);
if (nc == NULL)
return FALSE;
bt_uuid16_create(&uuid16, nc->id);
bt_uuid_to_uuid128(&uuid16, &uuid128);
bt_uuid_to_string(&uuid128, uuid_str, MAX_LEN_UUID_STR);
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
return TRUE;
}
static void connection_free(void *data)
{
struct network_conn *nc = data;
if (nc->dc_id)
device_remove_disconnect_watch(nc->peer->device, nc->dc_id);
connection_destroy(NULL, nc);
if (nc->connect)
dbus_message_unref(nc->connect);
btd_service_unref(nc->service);
g_free(nc);
}
static void peer_free(struct network_peer *peer)
{
g_slist_free_full(peer->connections, connection_free);
btd_device_unref(peer->device);
g_free(peer);
}
static void path_unregister(void *data)
{
struct network_peer *peer = data;
DBG("Unregistered interface %s on path %s",
NETWORK_PEER_INTERFACE, device_get_path(peer->device));
peers = g_slist_remove(peers, peer);
peer_free(peer);
}
static const GDBusMethodTable connection_methods[] = {
{ GDBUS_ASYNC_METHOD("Connect",
GDBUS_ARGS({"uuid", "s"}),
GDBUS_ARGS({"interface", "s"}),
local_connect) },
{ GDBUS_METHOD("Disconnect",
NULL, NULL, local_disconnect) },
{ }
};
static const GDBusPropertyTable connection_properties[] = {
{ "Connected", "b", network_property_get_connected },
{ "Interface", "s", network_property_get_interface, NULL,
network_property_exists },
{ "UUID", "s", network_property_get_uuid, NULL,
network_property_exists },
{ }
};
void connection_unregister(struct btd_service *svc)
{
struct btd_device *device = btd_service_get_device(svc);
struct network_conn *conn = btd_service_get_user_data(svc);
struct network_peer *peer = conn->peer;
uint16_t id = get_pan_srv_id(btd_service_get_profile(svc)->remote_uuid);
DBG("%s id %u", device_get_path(device), id);
peer->connections = g_slist_remove(peer->connections, conn);
connection_free(conn);
if (peer->connections != NULL)
return;
g_dbus_unregister_interface(btd_get_dbus_connection(),
device_get_path(device),
NETWORK_PEER_INTERFACE);
}
static struct network_peer *create_peer(struct btd_device *device)
{
struct network_peer *peer;
const char *path;
peer = g_new0(struct network_peer, 1);
peer->device = btd_device_ref(device);
path = device_get_path(device);
if (g_dbus_register_interface(btd_get_dbus_connection(), path,
NETWORK_PEER_INTERFACE,
connection_methods,
NULL, connection_properties,
peer, path_unregister) == FALSE) {
error("D-Bus failed to register %s interface",
NETWORK_PEER_INTERFACE);
peer_free(peer);
return NULL;
}
DBG("Registered interface %s on path %s",
NETWORK_PEER_INTERFACE, path);
return peer;
}
int connection_register(struct btd_service *svc)
{
struct btd_device *device = btd_service_get_device(svc);
struct network_peer *peer;
struct network_conn *nc;
uint16_t id = get_pan_srv_id(btd_service_get_profile(svc)->remote_uuid);
DBG("%s id %u", device_get_path(device), id);
peer = find_peer(peers, device);
if (!peer) {
peer = create_peer(device);
if (!peer)
return -1;
peers = g_slist_append(peers, peer);
}
nc = g_new0(struct network_conn, 1);
nc->id = id;
nc->service = btd_service_ref(svc);
nc->state = DISCONNECTED;
nc->peer = peer;
btd_service_set_user_data(svc, nc);
DBG("id %u registered", id);
peer->connections = g_slist_append(peer->connections, nc);
return 0;
}
|