connection.c 13 KB

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