pcap-bt-monitor-linux.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2014 Michal Labedzki for Tieto Corporation
  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. */
  31. #ifdef HAVE_CONFIG_H
  32. #include <config.h>
  33. #endif
  34. #include <errno.h>
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <bluetooth/bluetooth.h>
  39. #include <bluetooth/hci.h>
  40. #include "pcap/bluetooth.h"
  41. #include "pcap-int.h"
  42. #include "pcap-bt-monitor-linux.h"
  43. #define BT_CONTROL_SIZE 32
  44. #define INTERFACE_NAME "bluetooth-monitor"
  45. /*
  46. * Fields and alignment must match the declaration in the Linux kernel 3.4+.
  47. * See struct hci_mon_hdr in include/net/bluetooth/hci_mon.h.
  48. */
  49. struct hci_mon_hdr {
  50. uint16_t opcode;
  51. uint16_t index;
  52. uint16_t len;
  53. } __attribute__((packed));
  54. int
  55. bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str)
  56. {
  57. int ret = 0;
  58. /*
  59. * Bluetooth is a wireless technology.
  60. *
  61. * This is a device to monitor all Bluetooth interfaces, so
  62. * there's no notion of "connected" or "disconnected", any
  63. * more than there's a notion of "connected" or "disconnected"
  64. * for the "any" device.
  65. */
  66. if (add_dev(devlistp, INTERFACE_NAME,
  67. PCAP_IF_WIRELESS|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
  68. "Bluetooth Linux Monitor", err_str) == NULL)
  69. {
  70. ret = -1;
  71. }
  72. return ret;
  73. }
  74. static int
  75. bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
  76. {
  77. struct cmsghdr *cmsg;
  78. struct msghdr msg;
  79. struct iovec iv[2];
  80. ssize_t ret;
  81. struct pcap_pkthdr pkth;
  82. pcap_bluetooth_linux_monitor_header *bthdr;
  83. u_char *pktd;
  84. struct hci_mon_hdr hdr;
  85. pktd = (u_char *)handle->buffer + BT_CONTROL_SIZE;
  86. bthdr = (pcap_bluetooth_linux_monitor_header*)(void *)pktd;
  87. iv[0].iov_base = &hdr;
  88. iv[0].iov_len = sizeof(hdr);
  89. iv[1].iov_base = pktd + sizeof(pcap_bluetooth_linux_monitor_header);
  90. iv[1].iov_len = handle->snapshot;
  91. memset(&pkth.ts, 0, sizeof(pkth.ts));
  92. memset(&msg, 0, sizeof(msg));
  93. msg.msg_iov = iv;
  94. msg.msg_iovlen = 2;
  95. msg.msg_control = handle->buffer;
  96. msg.msg_controllen = BT_CONTROL_SIZE;
  97. do {
  98. ret = recvmsg(handle->fd, &msg, 0);
  99. if (handle->break_loop)
  100. {
  101. handle->break_loop = 0;
  102. return -2;
  103. }
  104. } while ((ret == -1) && (errno == EINTR));
  105. if (ret < 0) {
  106. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  107. errno, "Can't receive packet");
  108. return -1;
  109. }
  110. pkth.caplen = ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header);
  111. pkth.len = pkth.caplen;
  112. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  113. if (cmsg->cmsg_level != SOL_SOCKET) continue;
  114. if (cmsg->cmsg_type == SCM_TIMESTAMP) {
  115. memcpy(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts));
  116. }
  117. }
  118. bthdr->adapter_id = htons(hdr.index);
  119. bthdr->opcode = htons(hdr.opcode);
  120. if (handle->fcode.bf_insns == NULL ||
  121. bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
  122. callback(user, &pkth, pktd);
  123. return 1;
  124. }
  125. return 0; /* didn't pass filter */
  126. }
  127. static int
  128. bt_monitor_inject(pcap_t *handle, const void *buf _U_, size_t size _U_)
  129. {
  130. pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported yet");
  131. return -1;
  132. }
  133. static int
  134. bt_monitor_setdirection(pcap_t *p, pcap_direction_t d)
  135. {
  136. p->direction = d;
  137. return 0;
  138. }
  139. static int
  140. bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats)
  141. {
  142. stats->ps_recv = 0;
  143. stats->ps_drop = 0;
  144. stats->ps_ifdrop = 0;
  145. return 0;
  146. }
  147. static int
  148. bt_monitor_activate(pcap_t* handle)
  149. {
  150. struct sockaddr_hci addr;
  151. int err = PCAP_ERROR;
  152. int opt;
  153. if (handle->opt.rfmon) {
  154. /* monitor mode doesn't apply here */
  155. return PCAP_ERROR_RFMON_NOTSUP;
  156. }
  157. /*
  158. * Turn a negative snapshot value (invalid), a snapshot value of
  159. * 0 (unspecified), or a value bigger than the normal maximum
  160. * value, into the maximum allowed value.
  161. *
  162. * If some application really *needs* a bigger snapshot
  163. * length, we should just increase MAXIMUM_SNAPLEN.
  164. */
  165. if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
  166. handle->snapshot = MAXIMUM_SNAPLEN;
  167. handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot;
  168. handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR;
  169. handle->read_op = bt_monitor_read;
  170. handle->inject_op = bt_monitor_inject;
  171. handle->setfilter_op = install_bpf_program; /* no kernel filtering */
  172. handle->setdirection_op = bt_monitor_setdirection;
  173. handle->set_datalink_op = NULL; /* can't change data link type */
  174. handle->getnonblock_op = pcap_getnonblock_fd;
  175. handle->setnonblock_op = pcap_setnonblock_fd;
  176. handle->stats_op = bt_monitor_stats;
  177. handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
  178. if (handle->fd < 0) {
  179. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  180. errno, "Can't create raw socket");
  181. return PCAP_ERROR;
  182. }
  183. handle->buffer = malloc(handle->bufsize);
  184. if (!handle->buffer) {
  185. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  186. errno, "Can't allocate dump buffer");
  187. goto close_fail;
  188. }
  189. /* Bind socket to the HCI device */
  190. addr.hci_family = AF_BLUETOOTH;
  191. addr.hci_dev = HCI_DEV_NONE;
  192. addr.hci_channel = HCI_CHANNEL_MONITOR;
  193. if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  194. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  195. errno, "Can't attach to interface");
  196. goto close_fail;
  197. }
  198. opt = 1;
  199. if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
  200. pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
  201. errno, "Can't enable time stamp");
  202. goto close_fail;
  203. }
  204. handle->selectable_fd = handle->fd;
  205. return 0;
  206. close_fail:
  207. pcap_cleanup_live_common(handle);
  208. return err;
  209. }
  210. pcap_t *
  211. bt_monitor_create(const char *device, char *ebuf, int *is_ours)
  212. {
  213. pcap_t *p;
  214. const char *cp;
  215. cp = strrchr(device, '/');
  216. if (cp == NULL)
  217. cp = device;
  218. if (strcmp(cp, INTERFACE_NAME) != 0) {
  219. *is_ours = 0;
  220. return NULL;
  221. }
  222. *is_ours = 1;
  223. p = pcap_create_common(ebuf, 0);
  224. if (p == NULL)
  225. return NULL;
  226. p->activate_op = bt_monitor_activate;
  227. return p;
  228. }