pcap-bt-linux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright (c) 2006 Paolo Abeni (Italy)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote
  15. * products derived from this software without specific prior written
  16. * permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * Bluetooth sniffing API implementation for Linux platform
  31. * By Paolo Abeni <paolo.abeni@email.it>
  32. *
  33. */
  34. #ifdef HAVE_CONFIG_H
  35. #include <config.h>
  36. #endif
  37. #include "pcap-int.h"
  38. #include "pcap-bt-linux.h"
  39. #include "pcap/bluetooth.h"
  40. #include <errno.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <string.h>
  45. #include <sys/ioctl.h>
  46. #include <sys/socket.h>
  47. #include <arpa/inet.h>
  48. #include <bluetooth/bluetooth.h>
  49. #include <bluetooth/hci.h>
  50. #define BT_IFACE "bluetooth"
  51. #define BT_CTRL_SIZE 128
  52. /* forward declaration */
  53. static int bt_activate(pcap_t *);
  54. static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *);
  55. static int bt_inject_linux(pcap_t *, const void *, size_t);
  56. static int bt_setdirection_linux(pcap_t *, pcap_direction_t);
  57. static int bt_stats_linux(pcap_t *, struct pcap_stat *);
  58. /*
  59. * Private data for capturing on Linux Bluetooth devices.
  60. */
  61. struct pcap_bt {
  62. int dev_id; /* device ID of device we're bound to */
  63. };
  64. int
  65. bt_findalldevs(pcap_if_list_t *devlistp, char *err_str)
  66. {
  67. struct hci_dev_list_req *dev_list;
  68. struct hci_dev_req *dev_req;
  69. int i, sock;
  70. int ret = 0;
  71. sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
  72. if (sock < 0)
  73. {
  74. /* if bluetooth is not supported this this is not fatal*/
  75. if (errno == EAFNOSUPPORT)
  76. return 0;
  77. pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
  78. errno, "Can't open raw Bluetooth socket");
  79. return -1;
  80. }
  81. dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
  82. if (!dev_list)
  83. {
  84. pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list",
  85. HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
  86. ret = -1;
  87. goto done;
  88. }
  89. dev_list->dev_num = HCI_MAX_DEV;
  90. if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
  91. {
  92. pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
  93. errno, "Can't get Bluetooth device list via ioctl");
  94. ret = -1;
  95. goto free;
  96. }
  97. dev_req = dev_list->dev_req;
  98. for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
  99. char dev_name[20], dev_descr[30];
  100. pcap_snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id);
  101. pcap_snprintf(dev_descr, 30, "Bluetooth adapter number %d", i);
  102. /*
  103. * Bluetooth is a wireless technology.
  104. * XXX - if there's the notion of associating with a
  105. * network, and we can determine whether the interface
  106. * is associated with a network, check that and set
  107. * the status to PCAP_IF_CONNECTION_STATUS_CONNECTED
  108. * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED.
  109. */
  110. if (add_dev(devlistp, dev_name, PCAP_IF_WIRELESS, dev_descr, err_str) == NULL)
  111. {
  112. ret = -1;
  113. break;
  114. }
  115. }
  116. free:
  117. free(dev_list);
  118. done:
  119. close(sock);
  120. return ret;
  121. }
  122. pcap_t *
  123. bt_create(const char *device, char *ebuf, int *is_ours)
  124. {
  125. const char *cp;
  126. char *cpend;
  127. long devnum;
  128. pcap_t *p;
  129. /* Does this look like a Bluetooth device? */
  130. cp = strrchr(device, '/');
  131. if (cp == NULL)
  132. cp = device;
  133. /* Does it begin with BT_IFACE? */
  134. if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) {
  135. /* Nope, doesn't begin with BT_IFACE */
  136. *is_ours = 0;
  137. return NULL;
  138. }
  139. /* Yes - is BT_IFACE followed by a number? */
  140. cp += sizeof BT_IFACE - 1;
  141. devnum = strtol(cp, &cpend, 10);
  142. if (cpend == cp || *cpend != '\0') {
  143. /* Not followed by a number. */
  144. *is_ours = 0;
  145. return NULL;
  146. }
  147. if (devnum < 0) {
  148. /* Followed by a non-valid number. */
  149. *is_ours = 0;
  150. return NULL;
  151. }
  152. /* OK, it's probably ours. */
  153. *is_ours = 1;
  154. p = pcap_create_common(ebuf, sizeof (struct pcap_bt));
  155. if (p == NULL)
  156. return (NULL);
  157. p->activate_op = bt_activate;
  158. return (p);
  159. }
  160. static int
  161. bt_activate(pcap_t* handle)
  162. {
  163. struct pcap_bt *handlep = handle->priv;
  164. struct sockaddr_hci addr;
  165. int opt;
  166. int dev_id;
  167. struct hci_filter flt;
  168. int err = PCAP_ERROR;
  169. /* get bt interface id */
  170. if (sscanf(handle->opt.device, BT_IFACE"%d", &dev_id) != 1)
  171. {
  172. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  173. "Can't get Bluetooth device index from %s",
  174. handle->opt.device);
  175. return PCAP_ERROR;
  176. }
  177. /*
  178. * Turn a negative snapshot value (invalid), a snapshot value of
  179. * 0 (unspecified), or a value bigger than the normal maximum
  180. * value, into the maximum allowed value.
  181. *
  182. * If some application really *needs* a bigger snapshot
  183. * length, we should just increase MAXIMUM_SNAPLEN.
  184. */
  185. if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
  186. handle->snapshot = MAXIMUM_SNAPLEN;
  187. /* Initialize some components of the pcap structure. */
  188. handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot;
  189. handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
  190. handle->read_op = bt_read_linux;
  191. handle->inject_op = bt_inject_linux;
  192. handle->setfilter_op = install_bpf_program; /* no kernel filtering */
  193. handle->setdirection_op = bt_setdirection_linux;
  194. handle->set_datalink_op = NULL; /* can't change data link type */
  195. handle->getnonblock_op = pcap_getnonblock_fd;
  196. handle->setnonblock_op = pcap_setnonblock_fd;
  197. handle->stats_op = bt_stats_linux;
  198. handlep->dev_id = dev_id;
  199. /* Create HCI socket */
  200. handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
  201. if (handle->fd < 0) {
  202. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  203. errno, "Can't create raw socket");
  204. return PCAP_ERROR;
  205. }
  206. handle->buffer = malloc(handle->bufsize);
  207. if (!handle->buffer) {
  208. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  209. errno, "Can't allocate dump buffer");
  210. goto close_fail;
  211. }
  212. opt = 1;
  213. if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
  214. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  215. errno, "Can't enable data direction info");
  216. goto close_fail;
  217. }
  218. opt = 1;
  219. if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
  220. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  221. errno, "Can't enable time stamp");
  222. goto close_fail;
  223. }
  224. /* Setup filter, do not call hci function to avoid dependence on
  225. * external libs */
  226. memset(&flt, 0, sizeof(flt));
  227. memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
  228. memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
  229. if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
  230. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  231. errno, "Can't set filter");
  232. goto close_fail;
  233. }
  234. /* Bind socket to the HCI device */
  235. addr.hci_family = AF_BLUETOOTH;
  236. addr.hci_dev = handlep->dev_id;
  237. #ifdef HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL
  238. addr.hci_channel = HCI_CHANNEL_RAW;
  239. #endif
  240. if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  241. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  242. errno, "Can't attach to device %d", handlep->dev_id);
  243. goto close_fail;
  244. }
  245. if (handle->opt.rfmon) {
  246. /*
  247. * Monitor mode doesn't apply to Bluetooth devices.
  248. */
  249. err = PCAP_ERROR_RFMON_NOTSUP;
  250. goto close_fail;
  251. }
  252. if (handle->opt.buffer_size != 0) {
  253. /*
  254. * Set the socket buffer size to the specified value.
  255. */
  256. if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
  257. &handle->opt.buffer_size,
  258. sizeof(handle->opt.buffer_size)) == -1) {
  259. pcap_fmt_errmsg_for_errno(handle->errbuf,
  260. errno, PCAP_ERRBUF_SIZE, "SO_RCVBUF");
  261. goto close_fail;
  262. }
  263. }
  264. handle->selectable_fd = handle->fd;
  265. return 0;
  266. close_fail:
  267. pcap_cleanup_live_common(handle);
  268. return err;
  269. }
  270. static int
  271. bt_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
  272. {
  273. struct cmsghdr *cmsg;
  274. struct msghdr msg;
  275. struct iovec iv;
  276. ssize_t ret;
  277. struct pcap_pkthdr pkth;
  278. pcap_bluetooth_h4_header* bthdr;
  279. u_char *pktd;
  280. int in = 0;
  281. pktd = (u_char *)handle->buffer + BT_CTRL_SIZE;
  282. bthdr = (pcap_bluetooth_h4_header*)(void *)pktd;
  283. iv.iov_base = pktd + sizeof(pcap_bluetooth_h4_header);
  284. iv.iov_len = handle->snapshot;
  285. memset(&msg, 0, sizeof(msg));
  286. msg.msg_iov = &iv;
  287. msg.msg_iovlen = 1;
  288. msg.msg_control = handle->buffer;
  289. msg.msg_controllen = BT_CTRL_SIZE;
  290. /* ignore interrupt system call error */
  291. do {
  292. ret = recvmsg(handle->fd, &msg, 0);
  293. if (handle->break_loop)
  294. {
  295. handle->break_loop = 0;
  296. return -2;
  297. }
  298. } while ((ret == -1) && (errno == EINTR));
  299. if (ret < 0) {
  300. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  301. errno, "Can't receive packet");
  302. return -1;
  303. }
  304. pkth.caplen = ret;
  305. /* get direction and timestamp*/
  306. cmsg = CMSG_FIRSTHDR(&msg);
  307. while (cmsg) {
  308. switch (cmsg->cmsg_type) {
  309. case HCI_CMSG_DIR:
  310. memcpy(&in, CMSG_DATA(cmsg), sizeof in);
  311. break;
  312. case HCI_CMSG_TSTAMP:
  313. memcpy(&pkth.ts, CMSG_DATA(cmsg),
  314. sizeof pkth.ts);
  315. break;
  316. }
  317. cmsg = CMSG_NXTHDR(&msg, cmsg);
  318. }
  319. if ((in && (handle->direction == PCAP_D_OUT)) ||
  320. ((!in) && (handle->direction == PCAP_D_IN)))
  321. return 0;
  322. bthdr->direction = htonl(in != 0);
  323. pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
  324. pkth.len = pkth.caplen;
  325. if (handle->fcode.bf_insns == NULL ||
  326. bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
  327. callback(user, &pkth, pktd);
  328. return 1;
  329. }
  330. return 0; /* didn't pass filter */
  331. }
  332. static int
  333. bt_inject_linux(pcap_t *handle, const void *buf _U_, size_t size _U_)
  334. {
  335. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
  336. "bluetooth devices");
  337. return (-1);
  338. }
  339. static int
  340. bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
  341. {
  342. struct pcap_bt *handlep = handle->priv;
  343. int ret;
  344. struct hci_dev_info dev_info;
  345. struct hci_dev_stats * s = &dev_info.stat;
  346. dev_info.dev_id = handlep->dev_id;
  347. /* ignore eintr */
  348. do {
  349. ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
  350. } while ((ret == -1) && (errno == EINTR));
  351. if (ret < 0) {
  352. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  353. errno, "Can't get stats via ioctl");
  354. return (-1);
  355. }
  356. /* we receive both rx and tx frames, so comulate all stats */
  357. stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
  358. s->acl_tx +s->sco_tx;
  359. stats->ps_drop = s->err_rx + s->err_tx;
  360. stats->ps_ifdrop = 0;
  361. return 0;
  362. }
  363. static int
  364. bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
  365. {
  366. p->direction = d;
  367. return 0;
  368. }