btproxy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2012 Intel Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <ctype.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <alloca.h>
  23. #include <getopt.h>
  24. #include <stdbool.h>
  25. #include <termios.h>
  26. #include <sys/stat.h>
  27. #include <sys/socket.h>
  28. #include <sys/un.h>
  29. #include <netdb.h>
  30. #include <arpa/inet.h>
  31. #include "src/shared/util.h"
  32. #include "src/shared/mainloop.h"
  33. #include "src/shared/ecc.h"
  34. #include "monitor/bt.h"
  35. #define HCI_PRIMARY 0x00
  36. #define HCI_AMP 0x01
  37. #define BTPROTO_HCI 1
  38. struct sockaddr_hci {
  39. sa_family_t hci_family;
  40. unsigned short hci_dev;
  41. unsigned short hci_channel;
  42. };
  43. #define HCI_CHANNEL_USER 1
  44. static uint16_t hci_index = 0;
  45. static bool client_active = false;
  46. static bool debug_enabled = false;
  47. static bool emulate_ecc = false;
  48. static bool skip_first_zero = false;
  49. static void hexdump_print(const char *str, void *user_data)
  50. {
  51. printf("%s%s\n", (char *) user_data, str);
  52. }
  53. struct proxy {
  54. /* Receive commands, ACL, SCO and ISO data */
  55. int host_fd;
  56. uint8_t host_buf[4096];
  57. uint16_t host_len;
  58. bool host_shutdown;
  59. bool host_skip_first_zero;
  60. /* Receive events, ACL, SCO and ISO data */
  61. int dev_fd;
  62. uint8_t dev_buf[4096];
  63. uint16_t dev_len;
  64. bool dev_shutdown;
  65. /* ECC emulation */
  66. uint8_t event_mask[8];
  67. uint8_t local_sk256[32];
  68. };
  69. static bool write_packet(int fd, const void *data, size_t size,
  70. void *user_data)
  71. {
  72. while (size > 0) {
  73. ssize_t written;
  74. written = write(fd, data, size);
  75. if (written < 0) {
  76. if (errno == EAGAIN || errno == EINTR)
  77. continue;
  78. return false;
  79. }
  80. if (debug_enabled)
  81. util_hexdump('<', data, written, hexdump_print,
  82. user_data);
  83. data += written;
  84. size -= written;
  85. }
  86. return true;
  87. }
  88. static void host_write_packet(struct proxy *proxy, void *buf, uint16_t len)
  89. {
  90. if (!write_packet(proxy->dev_fd, buf, len, "D: ")) {
  91. fprintf(stderr, "Write to device descriptor failed\n");
  92. mainloop_remove_fd(proxy->dev_fd);
  93. }
  94. }
  95. static void dev_write_packet(struct proxy *proxy, void *buf, uint16_t len)
  96. {
  97. if (!write_packet(proxy->host_fd, buf, len, "H: ")) {
  98. fprintf(stderr, "Write to host descriptor failed\n");
  99. mainloop_remove_fd(proxy->host_fd);
  100. }
  101. }
  102. static void cmd_status(struct proxy *proxy, uint8_t status, uint16_t opcode)
  103. {
  104. size_t buf_size = 1 + sizeof(struct bt_hci_evt_hdr) +
  105. sizeof(struct bt_hci_evt_cmd_status);
  106. void *buf = alloca(buf_size);
  107. struct bt_hci_evt_hdr *hdr = buf + 1;
  108. struct bt_hci_evt_cmd_status *cs = buf + 1 + sizeof(*hdr);
  109. *((uint8_t *) buf) = BT_H4_EVT_PKT;
  110. hdr->evt = BT_HCI_EVT_CMD_STATUS;
  111. hdr->plen = sizeof(*cs);
  112. cs->status = status;
  113. cs->ncmd = 0x01;
  114. cs->opcode = cpu_to_le16(opcode);
  115. dev_write_packet(proxy, buf, buf_size);
  116. }
  117. static void le_meta_event(struct proxy *proxy, uint8_t event,
  118. void *data, uint8_t len)
  119. {
  120. size_t buf_size = 1 + sizeof(struct bt_hci_evt_hdr) + 1 + len;
  121. void *buf = alloca(buf_size);
  122. struct bt_hci_evt_hdr *hdr = buf + 1;
  123. *((uint8_t *) buf) = BT_H4_EVT_PKT;
  124. hdr->evt = BT_HCI_EVT_LE_META_EVENT;
  125. hdr->plen = 1 + len;
  126. *((uint8_t *) (buf + 1 + sizeof(*hdr))) = event;
  127. if (len > 0)
  128. memcpy(buf + 1 + sizeof(*hdr) + 1, data, len);
  129. dev_write_packet(proxy, buf, buf_size);
  130. }
  131. static void host_emulate_ecc(struct proxy *proxy, void *buf, uint16_t len)
  132. {
  133. uint8_t pkt_type = *((uint8_t *) buf);
  134. struct bt_hci_cmd_hdr *hdr = buf + 1;
  135. struct bt_hci_cmd_le_set_event_mask *lsem;
  136. struct bt_hci_cmd_le_generate_dhkey *lgd;
  137. struct bt_hci_evt_le_read_local_pk256_complete lrlpkc;
  138. struct bt_hci_evt_le_generate_dhkey_complete lgdc;
  139. if (pkt_type != BT_H4_CMD_PKT) {
  140. host_write_packet(proxy, buf, len);
  141. return;
  142. }
  143. switch (le16_to_cpu(hdr->opcode)) {
  144. case BT_HCI_CMD_LE_SET_EVENT_MASK:
  145. lsem = buf + 1 + sizeof(*hdr);
  146. memcpy(proxy->event_mask, lsem->mask, 8);
  147. lsem->mask[0] &= ~0x80; /* P-256 Public Key Complete */
  148. lsem->mask[1] &= ~0x01; /* Generate DHKey Complete */
  149. host_write_packet(proxy, buf, len);
  150. break;
  151. case BT_HCI_CMD_LE_READ_LOCAL_PK256:
  152. if (!ecc_make_key(lrlpkc.local_pk256, proxy->local_sk256)) {
  153. cmd_status(proxy, BT_HCI_ERR_COMMAND_DISALLOWED,
  154. BT_HCI_CMD_LE_READ_LOCAL_PK256);
  155. break;
  156. }
  157. cmd_status(proxy, BT_HCI_ERR_SUCCESS,
  158. BT_HCI_CMD_LE_READ_LOCAL_PK256);
  159. if (!(proxy->event_mask[0] & 0x80))
  160. break;
  161. lrlpkc.status = BT_HCI_ERR_SUCCESS;
  162. le_meta_event(proxy, BT_HCI_EVT_LE_READ_LOCAL_PK256_COMPLETE,
  163. &lrlpkc, sizeof(lrlpkc));
  164. break;
  165. case BT_HCI_CMD_LE_GENERATE_DHKEY:
  166. lgd = buf + 1 + sizeof(*hdr);
  167. if (!ecdh_shared_secret(lgd->remote_pk256, proxy->local_sk256,
  168. lgdc.dhkey)) {
  169. cmd_status(proxy, BT_HCI_ERR_COMMAND_DISALLOWED,
  170. BT_HCI_CMD_LE_GENERATE_DHKEY);
  171. break;
  172. }
  173. cmd_status(proxy, BT_HCI_ERR_SUCCESS,
  174. BT_HCI_CMD_LE_GENERATE_DHKEY);
  175. if (!(proxy->event_mask[1] & 0x01))
  176. break;
  177. lgdc.status = BT_HCI_ERR_SUCCESS;
  178. le_meta_event(proxy, BT_HCI_EVT_LE_GENERATE_DHKEY_COMPLETE,
  179. &lgdc, sizeof(lgdc));
  180. break;
  181. default:
  182. host_write_packet(proxy, buf, len);
  183. break;
  184. }
  185. }
  186. static void dev_emulate_ecc(struct proxy *proxy, void *buf, uint16_t len)
  187. {
  188. uint8_t pkt_type = *((uint8_t *) buf);
  189. struct bt_hci_evt_hdr *hdr = buf + 1;
  190. struct bt_hci_evt_cmd_complete *cc;
  191. struct bt_hci_rsp_read_local_commands *rlc;
  192. if (pkt_type != BT_H4_EVT_PKT) {
  193. dev_write_packet(proxy, buf, len);
  194. return;
  195. }
  196. switch (hdr->evt) {
  197. case BT_HCI_EVT_CMD_COMPLETE:
  198. cc = buf + 1 + sizeof(*hdr);
  199. switch (le16_to_cpu(cc->opcode)) {
  200. case BT_HCI_CMD_READ_LOCAL_COMMANDS:
  201. rlc = buf + 1 + sizeof(*hdr) + sizeof(*cc);
  202. rlc->commands[34] |= 0x02; /* P-256 Public Key */
  203. rlc->commands[34] |= 0x04; /* Generate DHKey */
  204. break;
  205. }
  206. dev_write_packet(proxy, buf, len);
  207. break;
  208. default:
  209. dev_write_packet(proxy, buf, len);
  210. break;
  211. }
  212. }
  213. static void host_read_destroy(void *user_data)
  214. {
  215. struct proxy *proxy = user_data;
  216. printf("Closing host descriptor\n");
  217. if (proxy->host_shutdown)
  218. shutdown(proxy->host_fd, SHUT_RDWR);
  219. close(proxy->host_fd);
  220. proxy->host_fd = -1;
  221. if (proxy->dev_fd < 0) {
  222. client_active = false;
  223. free(proxy);
  224. } else
  225. mainloop_remove_fd(proxy->dev_fd);
  226. }
  227. static void host_read_callback(int fd, uint32_t events, void *user_data)
  228. {
  229. struct proxy *proxy = user_data;
  230. struct bt_hci_cmd_hdr *cmd_hdr;
  231. struct bt_hci_acl_hdr *acl_hdr;
  232. struct bt_hci_sco_hdr *sco_hdr;
  233. struct bt_hci_iso_hdr *iso_hdr;
  234. ssize_t len;
  235. uint16_t pktlen;
  236. if (events & (EPOLLERR | EPOLLHUP)) {
  237. fprintf(stderr, "Error from host descriptor\n");
  238. mainloop_remove_fd(proxy->host_fd);
  239. return;
  240. }
  241. if (events & EPOLLRDHUP) {
  242. fprintf(stderr, "Remote hangup of host descriptor\n");
  243. mainloop_remove_fd(proxy->host_fd);
  244. return;
  245. }
  246. len = read(proxy->host_fd, proxy->host_buf + proxy->host_len,
  247. sizeof(proxy->host_buf) - proxy->host_len);
  248. if (len < 0) {
  249. if (errno == EAGAIN || errno == EINTR)
  250. return;
  251. fprintf(stderr, "Read from host descriptor failed\n");
  252. mainloop_remove_fd(proxy->host_fd);
  253. return;
  254. }
  255. if (debug_enabled)
  256. util_hexdump('>', proxy->host_buf + proxy->host_len, len,
  257. hexdump_print, "H: ");
  258. if (proxy->host_skip_first_zero && len > 0) {
  259. proxy->host_skip_first_zero = false;
  260. if (proxy->host_buf[proxy->host_len] == '\0') {
  261. printf("Skipping initial zero byte\n");
  262. len--;
  263. memmove(proxy->host_buf + proxy->host_len,
  264. proxy->host_buf + proxy->host_len + 1, len);
  265. }
  266. }
  267. proxy->host_len += len;
  268. process_packet:
  269. if (proxy->host_len < 1)
  270. return;
  271. switch (proxy->host_buf[0]) {
  272. case BT_H4_CMD_PKT:
  273. if (proxy->host_len < 1 + sizeof(*cmd_hdr))
  274. return;
  275. cmd_hdr = (void *) (proxy->host_buf + 1);
  276. pktlen = 1 + sizeof(*cmd_hdr) + cmd_hdr->plen;
  277. break;
  278. case BT_H4_ACL_PKT:
  279. if (proxy->host_len < 1 + sizeof(*acl_hdr))
  280. return;
  281. acl_hdr = (void *) (proxy->host_buf + 1);
  282. pktlen = 1 + sizeof(*acl_hdr) + cpu_to_le16(acl_hdr->dlen);
  283. break;
  284. case BT_H4_SCO_PKT:
  285. if (proxy->host_len < 1 + sizeof(*sco_hdr))
  286. return;
  287. sco_hdr = (void *) (proxy->host_buf + 1);
  288. pktlen = 1 + sizeof(*sco_hdr) + sco_hdr->dlen;
  289. break;
  290. case BT_H4_ISO_PKT:
  291. if (proxy->host_len < 1 + sizeof(*iso_hdr))
  292. return;
  293. iso_hdr = (void *) (proxy->host_buf + 1);
  294. pktlen = 1 + sizeof(*iso_hdr) + cpu_to_le16(iso_hdr->dlen);
  295. break;
  296. case 0xff:
  297. /* Notification packet from /dev/vhci - ignore */
  298. proxy->host_len = 0;
  299. return;
  300. default:
  301. fprintf(stderr, "Received unknown host packet type 0x%02x\n",
  302. proxy->host_buf[0]);
  303. mainloop_remove_fd(proxy->host_fd);
  304. return;
  305. }
  306. if (proxy->host_len < pktlen)
  307. return;
  308. if (emulate_ecc)
  309. host_emulate_ecc(proxy, proxy->host_buf, pktlen);
  310. else
  311. host_write_packet(proxy, proxy->host_buf, pktlen);
  312. if (proxy->host_len > pktlen) {
  313. memmove(proxy->host_buf, proxy->host_buf + pktlen,
  314. proxy->host_len - pktlen);
  315. proxy->host_len -= pktlen;
  316. goto process_packet;
  317. }
  318. proxy->host_len = 0;
  319. }
  320. static void dev_read_destroy(void *user_data)
  321. {
  322. struct proxy *proxy = user_data;
  323. printf("Closing device descriptor\n");
  324. if (proxy->dev_shutdown)
  325. shutdown(proxy->dev_fd, SHUT_RDWR);
  326. close(proxy->dev_fd);
  327. proxy->dev_fd = -1;
  328. if (proxy->host_fd < 0) {
  329. client_active = false;
  330. free(proxy);
  331. } else
  332. mainloop_remove_fd(proxy->host_fd);
  333. }
  334. static void dev_read_callback(int fd, uint32_t events, void *user_data)
  335. {
  336. struct proxy *proxy = user_data;
  337. struct bt_hci_evt_hdr *evt_hdr;
  338. struct bt_hci_acl_hdr *acl_hdr;
  339. struct bt_hci_sco_hdr *sco_hdr;
  340. struct bt_hci_iso_hdr *iso_hdr;
  341. ssize_t len;
  342. uint16_t pktlen;
  343. if (events & (EPOLLERR | EPOLLHUP)) {
  344. fprintf(stderr, "Error from device descriptor\n");
  345. mainloop_remove_fd(proxy->dev_fd);
  346. return;
  347. }
  348. if (events & EPOLLRDHUP) {
  349. fprintf(stderr, "Remote hangup of device descriptor\n");
  350. mainloop_remove_fd(proxy->host_fd);
  351. return;
  352. }
  353. len = read(proxy->dev_fd, proxy->dev_buf + proxy->dev_len,
  354. sizeof(proxy->dev_buf) - proxy->dev_len);
  355. if (len < 0) {
  356. if (errno == EAGAIN || errno == EINTR)
  357. return;
  358. fprintf(stderr, "Read from device descriptor failed\n");
  359. mainloop_remove_fd(proxy->dev_fd);
  360. return;
  361. }
  362. if (debug_enabled)
  363. util_hexdump('>', proxy->dev_buf + proxy->dev_len, len,
  364. hexdump_print, "D: ");
  365. proxy->dev_len += len;
  366. process_packet:
  367. if (proxy->dev_len < 1)
  368. return;
  369. switch (proxy->dev_buf[0]) {
  370. case BT_H4_EVT_PKT:
  371. if (proxy->dev_len < 1 + sizeof(*evt_hdr))
  372. return;
  373. evt_hdr = (void *) (proxy->dev_buf + 1);
  374. pktlen = 1 + sizeof(*evt_hdr) + evt_hdr->plen;
  375. break;
  376. case BT_H4_ACL_PKT:
  377. if (proxy->dev_len < 1 + sizeof(*acl_hdr))
  378. return;
  379. acl_hdr = (void *) (proxy->dev_buf + 1);
  380. pktlen = 1 + sizeof(*acl_hdr) + cpu_to_le16(acl_hdr->dlen);
  381. break;
  382. case BT_H4_SCO_PKT:
  383. if (proxy->dev_len < 1 + sizeof(*sco_hdr))
  384. return;
  385. sco_hdr = (void *) (proxy->dev_buf + 1);
  386. pktlen = 1 + sizeof(*sco_hdr) + sco_hdr->dlen;
  387. break;
  388. case BT_H4_ISO_PKT:
  389. if (proxy->dev_len < 1 + sizeof(*iso_hdr))
  390. return;
  391. iso_hdr = (void *) (proxy->dev_buf + 1);
  392. pktlen = 1 + sizeof(*iso_hdr) + cpu_to_le16(iso_hdr->dlen);
  393. break;
  394. default:
  395. fprintf(stderr, "Received unknown device packet type 0x%02x\n",
  396. proxy->dev_buf[0]);
  397. mainloop_remove_fd(proxy->dev_fd);
  398. return;
  399. }
  400. if (proxy->dev_len < pktlen)
  401. return;
  402. if (emulate_ecc)
  403. dev_emulate_ecc(proxy, proxy->dev_buf, pktlen);
  404. else
  405. dev_write_packet(proxy, proxy->dev_buf, pktlen);
  406. if (proxy->dev_len > pktlen) {
  407. memmove(proxy->dev_buf, proxy->dev_buf + pktlen,
  408. proxy->dev_len - pktlen);
  409. proxy->dev_len -= pktlen;
  410. goto process_packet;
  411. }
  412. proxy->dev_len = 0;
  413. }
  414. static bool setup_proxy(int host_fd, bool host_shutdown,
  415. int dev_fd, bool dev_shutdown)
  416. {
  417. struct proxy *proxy;
  418. proxy = new0(struct proxy, 1);
  419. if (!proxy)
  420. return false;
  421. if (emulate_ecc)
  422. printf("Enabling ECC emulation\n");
  423. proxy->host_fd = host_fd;
  424. proxy->host_shutdown = host_shutdown;
  425. proxy->host_skip_first_zero = skip_first_zero;
  426. proxy->dev_fd = dev_fd;
  427. proxy->dev_shutdown = dev_shutdown;
  428. mainloop_add_fd(proxy->host_fd, EPOLLIN | EPOLLRDHUP,
  429. host_read_callback, proxy, host_read_destroy);
  430. mainloop_add_fd(proxy->dev_fd, EPOLLIN | EPOLLRDHUP,
  431. dev_read_callback, proxy, dev_read_destroy);
  432. return true;
  433. }
  434. static int open_channel(uint16_t index)
  435. {
  436. struct sockaddr_hci addr;
  437. int fd;
  438. printf("Opening user channel for hci%u\n", hci_index);
  439. fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
  440. if (fd < 0) {
  441. perror("Failed to open Bluetooth socket");
  442. return -1;
  443. }
  444. memset(&addr, 0, sizeof(addr));
  445. addr.hci_family = AF_BLUETOOTH;
  446. addr.hci_dev = index;
  447. addr.hci_channel = HCI_CHANNEL_USER;
  448. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  449. close(fd);
  450. perror("Failed to bind Bluetooth socket");
  451. return -1;
  452. }
  453. return fd;
  454. }
  455. static void server_callback(int fd, uint32_t events, void *user_data)
  456. {
  457. union {
  458. struct sockaddr common;
  459. struct sockaddr_un sun;
  460. struct sockaddr_in sin;
  461. } addr;
  462. socklen_t len;
  463. int host_fd, dev_fd;
  464. if (events & (EPOLLERR | EPOLLHUP)) {
  465. mainloop_quit();
  466. return;
  467. }
  468. memset(&addr, 0, sizeof(addr));
  469. len = sizeof(addr);
  470. if (getsockname(fd, &addr.common, &len) < 0) {
  471. perror("Failed to get socket name");
  472. return;
  473. }
  474. host_fd = accept(fd, &addr.common, &len);
  475. if (host_fd < 0) {
  476. perror("Failed to accept client socket");
  477. return;
  478. }
  479. if (client_active) {
  480. fprintf(stderr, "Active client already present\n");
  481. close(host_fd);
  482. return;
  483. }
  484. dev_fd = open_channel(hci_index);
  485. if (dev_fd < 0) {
  486. close(host_fd);
  487. return;
  488. }
  489. printf("New client connected\n");
  490. if (!setup_proxy(host_fd, true, dev_fd, false)) {
  491. close(dev_fd);
  492. close(host_fd);
  493. return;
  494. }
  495. client_active = true;
  496. }
  497. static int open_unix(const char *path)
  498. {
  499. struct sockaddr_un addr;
  500. size_t len;
  501. int fd;
  502. len = strlen(path);
  503. if (len > sizeof(addr.sun_path) - 1) {
  504. fprintf(stderr, "Path too long\n");
  505. return -1;
  506. }
  507. unlink(path);
  508. fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
  509. if (fd < 0) {
  510. perror("Failed to open Unix server socket");
  511. return -1;
  512. }
  513. memset(&addr, 0, sizeof(addr));
  514. addr.sun_family = AF_UNIX;
  515. strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
  516. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  517. perror("Failed to bind Unix server socket");
  518. close(fd);
  519. return -1;
  520. }
  521. if (listen(fd, 1) < 0) {
  522. perror("Failed to listen Unix server socket");
  523. close(fd);
  524. return -1;
  525. }
  526. if (chmod(path, 0666) < 0)
  527. perror("Failed to change mode");
  528. return fd;
  529. }
  530. static int open_tcp(const char *address, unsigned int port)
  531. {
  532. struct sockaddr_in addr;
  533. int fd, opt = 1;
  534. fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  535. if (fd < 0) {
  536. perror("Failed to open TCP server socket");
  537. return -1;
  538. }
  539. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  540. memset(&addr, 0, sizeof(addr));
  541. addr.sin_family = AF_INET;
  542. addr.sin_addr.s_addr = inet_addr(address);
  543. addr.sin_port = htons(port);
  544. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  545. perror("Failed to bind TCP server socket");
  546. close(fd);
  547. return -1;
  548. }
  549. if (listen(fd, 1) < 0) {
  550. perror("Failed to listen TCP server socket");
  551. close(fd);
  552. return -1;
  553. }
  554. return fd;
  555. }
  556. static int connect_tcp(const char *address, unsigned int port)
  557. {
  558. struct sockaddr_in addr;
  559. int fd;
  560. fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  561. if (fd < 0) {
  562. perror("Failed to open TCP client socket");
  563. return -1;
  564. }
  565. memset(&addr, 0, sizeof(addr));
  566. addr.sin_family = AF_INET;
  567. addr.sin_addr.s_addr = inet_addr(address);
  568. addr.sin_port = htons(port);
  569. if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  570. perror("Failed to connect TCP client socket");
  571. close(fd);
  572. return -1;
  573. }
  574. return fd;
  575. }
  576. static int open_vhci(uint8_t type)
  577. {
  578. uint8_t create_req[2] = { 0xff, type };
  579. ssize_t written;
  580. int fd;
  581. fd = open("/dev/vhci", O_RDWR | O_CLOEXEC);
  582. if (fd < 0) {
  583. perror("Failed to open /dev/vhci device");
  584. return -1;
  585. }
  586. written = write(fd, create_req, sizeof(create_req));
  587. if (written < 0) {
  588. perror("Failed to set device type");
  589. close(fd);
  590. return -1;
  591. }
  592. return fd;
  593. }
  594. static void signal_callback(int signum, void *user_data)
  595. {
  596. switch (signum) {
  597. case SIGINT:
  598. case SIGTERM:
  599. mainloop_quit();
  600. break;
  601. }
  602. }
  603. static void usage(void)
  604. {
  605. printf("btproxy - Bluetooth controller proxy\n"
  606. "Usage:\n");
  607. printf("\tbtproxy [options]\n");
  608. printf("Options:\n"
  609. "\t-c, --connect <address> Connect to server\n"
  610. "\t-l, --listen [address] Use TCP server\n"
  611. "\t-u, --unix [path] Use Unix server\n"
  612. "\t-p, --port <port> Use specified TCP port\n"
  613. "\t-i, --index <num> Use specified controller\n"
  614. "\t-a, --amp Create AMP controller\n"
  615. "\t-e, --ecc Emulate ECC support\n"
  616. "\t-d, --debug Enable debugging output\n"
  617. "\t-h, --help Show help options\n");
  618. }
  619. static const struct option main_options[] = {
  620. { "redirect", no_argument, NULL, 'r' },
  621. { "connect", required_argument, NULL, 'c' },
  622. { "listen", optional_argument, NULL, 'l' },
  623. { "unix", optional_argument, NULL, 'u' },
  624. { "port", required_argument, NULL, 'p' },
  625. { "index", required_argument, NULL, 'i' },
  626. { "amp", no_argument, NULL, 'a' },
  627. { "ecc", no_argument, NULL, 'e' },
  628. { "debug", no_argument, NULL, 'd' },
  629. { "version", no_argument, NULL, 'v' },
  630. { "help", no_argument, NULL, 'h' },
  631. { }
  632. };
  633. int main(int argc, char *argv[])
  634. {
  635. const char *connect_address = NULL;
  636. const char *server_address = NULL;
  637. const char *unix_path = NULL;
  638. unsigned short tcp_port = 0xb1ee; /* 45550 */
  639. bool use_redirect = false;
  640. uint8_t type = HCI_PRIMARY;
  641. const char *str;
  642. for (;;) {
  643. int opt;
  644. opt = getopt_long(argc, argv, "rc:l::u::p:i:aezdvh",
  645. main_options, NULL);
  646. if (opt < 0)
  647. break;
  648. switch (opt) {
  649. case 'r':
  650. use_redirect = true;
  651. break;
  652. case 'c':
  653. connect_address = optarg;
  654. break;
  655. case 'l':
  656. if (optarg)
  657. server_address = optarg;
  658. else
  659. server_address = "0.0.0.0";
  660. break;
  661. case 'u':
  662. if (optarg) {
  663. struct sockaddr_un addr;
  664. unix_path = optarg;
  665. if (strlen(unix_path) >
  666. sizeof(addr.sun_path) - 1) {
  667. fprintf(stderr, "Path too long\n");
  668. return EXIT_FAILURE;
  669. }
  670. } else
  671. unix_path = "/tmp/bt-server-bredr";
  672. break;
  673. case 'p':
  674. tcp_port = atoi(optarg);
  675. break;
  676. case 'i':
  677. if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
  678. str = optarg + 3;
  679. else
  680. str = optarg;
  681. if (!isdigit(*str)) {
  682. usage();
  683. return EXIT_FAILURE;
  684. }
  685. hci_index = atoi(str);
  686. break;
  687. case 'a':
  688. type = HCI_AMP;
  689. break;
  690. case 'e':
  691. emulate_ecc = true;
  692. break;
  693. case 'z':
  694. skip_first_zero = true;
  695. break;
  696. case 'd':
  697. debug_enabled = true;
  698. break;
  699. case 'v':
  700. printf("%s\n", VERSION);
  701. return EXIT_SUCCESS;
  702. case 'h':
  703. usage();
  704. return EXIT_SUCCESS;
  705. default:
  706. return EXIT_FAILURE;
  707. }
  708. }
  709. if (argc - optind > 0) {
  710. fprintf(stderr, "Invalid command line parameters\n");
  711. return EXIT_FAILURE;
  712. }
  713. if (unix_path && (server_address || use_redirect)) {
  714. fprintf(stderr, "Invalid to specify TCP and Unix servers\n");
  715. return EXIT_FAILURE;
  716. }
  717. if (connect_address && (unix_path || server_address || use_redirect)) {
  718. fprintf(stderr, "Invalid to specify client and server mode\n");
  719. return EXIT_FAILURE;
  720. }
  721. mainloop_init();
  722. if (connect_address || use_redirect) {
  723. int host_fd, dev_fd;
  724. if (use_redirect) {
  725. printf("Creating local redirect\n");
  726. dev_fd = open_channel(hci_index);
  727. } else {
  728. printf("Connecting to %s:%u\n", connect_address,
  729. tcp_port);
  730. dev_fd = connect_tcp(connect_address, tcp_port);
  731. }
  732. if (dev_fd < 0)
  733. return EXIT_FAILURE;
  734. printf("Opening virtual device\n");
  735. host_fd = open_vhci(type);
  736. if (host_fd < 0) {
  737. close(dev_fd);
  738. return EXIT_FAILURE;
  739. }
  740. if (!setup_proxy(host_fd, false, dev_fd, true)) {
  741. close(dev_fd);
  742. close(host_fd);
  743. return EXIT_FAILURE;
  744. }
  745. } else {
  746. int server_fd;
  747. if (unix_path) {
  748. printf("Listening on %s\n", unix_path);
  749. server_fd = open_unix(unix_path);
  750. } else if (server_address) {
  751. printf("Listening on %s:%u\n", server_address,
  752. tcp_port);
  753. server_fd = open_tcp(server_address, tcp_port);
  754. } else {
  755. fprintf(stderr, "Missing emulator device\n");
  756. return EXIT_FAILURE;
  757. }
  758. if (server_fd < 0)
  759. return EXIT_FAILURE;
  760. mainloop_add_fd(server_fd, EPOLLIN, server_callback,
  761. NULL, NULL);
  762. }
  763. return mainloop_run_with_signal(signal_callback, NULL);
  764. }