blob: c222c100edb23f86cda3e93265a5451a72cafb0e [file] [log] [blame]
Elliott Hughesd8845d72015-10-19 18:07:04 -07001/*
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
32#ifdef HAVE_CONFIG_H
Haibo Huang165065a2018-07-23 17:26:52 -070033#include <config.h>
Elliott Hughesd8845d72015-10-19 18:07:04 -070034#endif
35
36#include <errno.h>
Elliott Hughes965a4b52017-05-15 10:37:39 -070037#include <stdint.h>
Elliott Hughesd8845d72015-10-19 18:07:04 -070038#include <stdlib.h>
39#include <string.h>
40
41#include <bluetooth/bluetooth.h>
42#include <bluetooth/hci.h>
Elliott Hughesd8845d72015-10-19 18:07:04 -070043
44#include "pcap/bluetooth.h"
45#include "pcap-int.h"
46
47#include "pcap-bt-monitor-linux.h"
48
49#define BT_CONTROL_SIZE 32
50#define INTERFACE_NAME "bluetooth-monitor"
51
Elliott Hughes965a4b52017-05-15 10:37:39 -070052/*
53 * Fields and alignment must match the declaration in the Linux kernel 3.4+.
54 * See struct hci_mon_hdr in include/net/bluetooth/hci_mon.h.
55 */
56struct hci_mon_hdr {
57 uint16_t opcode;
58 uint16_t index;
59 uint16_t len;
60} __attribute__((packed));
61
Elliott Hughesd8845d72015-10-19 18:07:04 -070062int
Haibo Huang165065a2018-07-23 17:26:52 -070063bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str)
Elliott Hughesd8845d72015-10-19 18:07:04 -070064{
65 int ret = 0;
66
Haibo Huang165065a2018-07-23 17:26:52 -070067 /*
68 * Bluetooth is a wireless technology.
69 *
70 * This is a device to monitor all Bluetooth interfaces, so
71 * there's no notion of "connected" or "disconnected", any
72 * more than there's a notion of "connected" or "disconnected"
73 * for the "any" device.
74 */
75 if (add_dev(devlistp, INTERFACE_NAME,
76 PCAP_IF_WIRELESS|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
77 "Bluetooth Linux Monitor", err_str) == NULL)
Elliott Hughesd8845d72015-10-19 18:07:04 -070078 {
79 ret = -1;
80 }
81
82 return ret;
83}
84
85static int
86bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
87{
88 struct cmsghdr *cmsg;
89 struct msghdr msg;
90 struct iovec iv[2];
91 ssize_t ret;
92 struct pcap_pkthdr pkth;
93 pcap_bluetooth_linux_monitor_header *bthdr;
Elliott Hughes965a4b52017-05-15 10:37:39 -070094 u_char *pktd;
95 struct hci_mon_hdr hdr;
Elliott Hughesd8845d72015-10-19 18:07:04 -070096
Elliott Hughes965a4b52017-05-15 10:37:39 -070097 pktd = (u_char *)handle->buffer + BT_CONTROL_SIZE;
98 bthdr = (pcap_bluetooth_linux_monitor_header*)(void *)pktd;
Elliott Hughesd8845d72015-10-19 18:07:04 -070099
100 iv[0].iov_base = &hdr;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700101 iv[0].iov_len = sizeof(hdr);
102 iv[1].iov_base = pktd + sizeof(pcap_bluetooth_linux_monitor_header);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700103 iv[1].iov_len = handle->snapshot;
104
105 memset(&pkth.ts, 0, sizeof(pkth.ts));
106 memset(&msg, 0, sizeof(msg));
107 msg.msg_iov = iv;
108 msg.msg_iovlen = 2;
109 msg.msg_control = handle->buffer;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700110 msg.msg_controllen = BT_CONTROL_SIZE;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700111
112 do {
113 ret = recvmsg(handle->fd, &msg, 0);
114 if (handle->break_loop)
115 {
116 handle->break_loop = 0;
117 return -2;
118 }
119 } while ((ret == -1) && (errno == EINTR));
120
121 if (ret < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700122 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
123 errno, "Can't receive packet");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700124 return -1;
125 }
126
Elliott Hughes965a4b52017-05-15 10:37:39 -0700127 pkth.caplen = ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700128 pkth.len = pkth.caplen;
129
130 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
131 if (cmsg->cmsg_level != SOL_SOCKET) continue;
132
133 if (cmsg->cmsg_type == SCM_TIMESTAMP) {
134 memcpy(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts));
135 }
136 }
137
138 bthdr->adapter_id = htons(hdr.index);
139 bthdr->opcode = htons(hdr.opcode);
140
141 if (handle->fcode.bf_insns == NULL ||
Elliott Hughes965a4b52017-05-15 10:37:39 -0700142 bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
143 callback(user, &pkth, pktd);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700144 return 1;
145 }
146 return 0; /* didn't pass filter */
147}
148
149static int
150bt_monitor_inject(pcap_t *handle, const void *buf _U_, size_t size _U_)
151{
Elliott Hughes965a4b52017-05-15 10:37:39 -0700152 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported yet");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700153 return -1;
154}
155
156static int
157bt_monitor_setdirection(pcap_t *p, pcap_direction_t d)
158{
159 p->direction = d;
160 return 0;
161}
162
163static int
164bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats)
165{
166 stats->ps_recv = 0;
167 stats->ps_drop = 0;
168 stats->ps_ifdrop = 0;
169
170 return 0;
171}
172
173static int
174bt_monitor_activate(pcap_t* handle)
175{
176 struct sockaddr_hci addr;
177 int err = PCAP_ERROR;
178 int opt;
179
180 if (handle->opt.rfmon) {
181 /* monitor mode doesn't apply here */
182 return PCAP_ERROR_RFMON_NOTSUP;
183 }
184
Haibo Huang165065a2018-07-23 17:26:52 -0700185 /*
186 * Turn a negative snapshot value (invalid), a snapshot value of
187 * 0 (unspecified), or a value bigger than the normal maximum
188 * value, into the maximum allowed value.
189 *
190 * If some application really *needs* a bigger snapshot
191 * length, we should just increase MAXIMUM_SNAPLEN.
192 */
193 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
194 handle->snapshot = MAXIMUM_SNAPLEN;
195
Elliott Hughes965a4b52017-05-15 10:37:39 -0700196 handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700197 handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR;
198
199 handle->read_op = bt_monitor_read;
200 handle->inject_op = bt_monitor_inject;
201 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
202 handle->setdirection_op = bt_monitor_setdirection;
203 handle->set_datalink_op = NULL; /* can't change data link type */
204 handle->getnonblock_op = pcap_getnonblock_fd;
205 handle->setnonblock_op = pcap_setnonblock_fd;
206 handle->stats_op = bt_monitor_stats;
207
208 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
209 if (handle->fd < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700210 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
211 errno, "Can't create raw socket");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700212 return PCAP_ERROR;
213 }
214
215 handle->buffer = malloc(handle->bufsize);
216 if (!handle->buffer) {
Haibo Huang165065a2018-07-23 17:26:52 -0700217 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
218 errno, "Can't allocate dump buffer");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700219 goto close_fail;
220 }
221
222 /* Bind socket to the HCI device */
223 addr.hci_family = AF_BLUETOOTH;
224 addr.hci_dev = HCI_DEV_NONE;
225 addr.hci_channel = HCI_CHANNEL_MONITOR;
226
227 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700228 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
229 errno, "Can't attach to interface");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700230 goto close_fail;
231 }
232
233 opt = 1;
234 if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700235 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
236 errno, "Can't enable time stamp");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700237 goto close_fail;
238 }
239
240 handle->selectable_fd = handle->fd;
241
242 return 0;
243
244close_fail:
245 pcap_cleanup_live_common(handle);
246 return err;
247}
248
249pcap_t *
250bt_monitor_create(const char *device, char *ebuf, int *is_ours)
251{
252 pcap_t *p;
253 const char *cp;
254
255 cp = strrchr(device, '/');
256 if (cp == NULL)
257 cp = device;
258
259 if (strcmp(cp, INTERFACE_NAME) != 0) {
260 *is_ours = 0;
261 return NULL;
262 }
263
264 *is_ours = 1;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700265 p = pcap_create_common(ebuf, 0);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700266 if (p == NULL)
267 return NULL;
268
269 p->activate_op = bt_monitor_activate;
270
271 return p;
272}