]> The Tcpdump Group git mirrors - libpcap/blob - pcap-bt-linux.c
924835b6bb8f8ce9554da3e2cd39f91214b1cc6c
[libpcap] / pcap-bt-linux.c
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. Neither the name of the Politecnico di Torino, CACE Technologies
15 * nor the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Bluetooth sniffing API implementation for Linux platform
32 * By Paolo Abeni <paolo.abeni@email.it>
33 *
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "pcap-int.h"
41 #include "pcap-bt-linux.h"
42
43 #ifdef NEED_STRERROR_H
44 #include "strerror.h"
45 #endif
46
47 #include <errno.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <string.h>
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
54
55 #include <bluetooth/bluetooth.h>
56 #include <bluetooth/hci.h>
57
58 #define BT_IFACE "bluetooth"
59 #define BT_CTRL_SIZE 128
60
61 /* forward declaration */
62 static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *);
63 static int bt_inject_linux(pcap_t *, const void *, size_t);
64 static int bt_setfilter_linux(pcap_t *, struct bpf_program *);
65 static int bt_setdirection_linux(pcap_t *, pcap_direction_t);
66 static int bt_stats_linux(pcap_t *, struct pcap_stat *);
67 static void bt_close_linux(pcap_t *);
68
69 int
70 bt_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
71 {
72 pcap_if_t *found_dev = *alldevsp;
73 struct hci_dev_list_req *dev_list;
74 struct hci_dev_req *dev_req;
75 int i, sock;
76 int ret = 0;
77
78 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
79 if (sock < 0)
80 {
81 /* if bluetooth is not supported this this is not fatal*/
82 if (errno == EAFNOSUPPORT)
83 return 0;
84 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't open raw Bluetooth socket %d:%s",
85 errno, strerror(errno));
86 return -1;
87 }
88
89 dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
90 if (!dev_list)
91 {
92 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %d bytes for Bluetooth device list",
93 HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
94 ret = -1;
95 goto done;
96 }
97
98 dev_list->dev_num = HCI_MAX_DEV;
99
100 if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
101 {
102 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't get Bluetooth device list via ioctl %d:%s",
103 errno, strerror(errno));
104 ret = -1;
105 goto free;
106 }
107
108 dev_req = dev_list->dev_req;
109 for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
110 char dev_name[20], dev_descr[30];
111
112 snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id);
113 snprintf(dev_descr, 30, "Bluetooth adapter number %d", i);
114
115 if (pcap_add_if(&found_dev, dev_name, 0,
116 dev_descr, err_str) < 0)
117 {
118 ret = -1;
119 break;
120 }
121
122 }
123
124 free:
125 free(dev_list);
126
127 done:
128 close(sock);
129 return ret;
130 }
131
132 pcap_t*
133 bt_open_live(const char* bus, int snaplen, int promisc , int to_ms, char* errmsg)
134 {
135 struct sockaddr_hci addr;
136 int opt;
137 pcap_t *handle;
138 int dev_id;
139 struct hci_filter flt;
140
141 /* get bt interface id */
142 if (sscanf(bus, BT_IFACE"%d", &dev_id) != 1)
143 {
144 snprintf(errmsg, PCAP_ERRBUF_SIZE,
145 "Can't get usb bus index from %s", bus);
146 return NULL;
147 }
148
149 /* Allocate a handle for this session. */
150 handle = malloc(sizeof(*handle));
151 if (handle == NULL) {
152 snprintf(errmsg, PCAP_ERRBUF_SIZE, "malloc: %s",
153 pcap_strerror(errno));
154 return NULL;
155 }
156
157 /* Initialize some components of the pcap structure. */
158 memset(handle, 0, sizeof(*handle));
159 handle->snapshot = snaplen;
160 handle->md.timeout = to_ms;
161 handle->bufsize = snaplen+BT_CTRL_SIZE;
162 handle->offset = BT_CTRL_SIZE;
163 handle->linktype = DLT_BLUETOOTH_HCI_H4;
164
165 handle->read_op = bt_read_linux;
166 handle->inject_op = bt_inject_linux;
167 handle->setfilter_op = bt_setfilter_linux;
168 handle->setdirection_op = bt_setdirection_linux;
169 handle->set_datalink_op = NULL; /* can't change data link type */
170 handle->getnonblock_op = pcap_getnonblock_fd;
171 handle->setnonblock_op = pcap_setnonblock_fd;
172 handle->stats_op = bt_stats_linux;
173 handle->close_op = bt_close_linux;
174 handle->md.ifindex = dev_id;
175
176 /* Create HCI socket */
177 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
178 if (handle->fd < 0) {
179 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s",
180 errno, strerror(errno));
181 free(handle);
182 return NULL;
183 }
184
185 handle->buffer = malloc(snaplen+BT_CTRL_SIZE);
186 if (!handle->buffer) {
187 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
188 pcap_strerror(errno));
189 pcap_close(handle);
190 return NULL;
191 }
192
193 opt = 1;
194 if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
195 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't enable data direction info %d:%s",
196 errno, strerror(errno));
197 pcap_close(handle);
198 return NULL;
199 }
200
201 opt = 1;
202 if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
203 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't enable time stamp %d:%s",
204 errno, strerror(errno));
205 pcap_close(handle);
206 return NULL;
207 }
208
209 /* Setup filter, do not call hci function to avoid dependence on
210 * external libs */
211 memset(&flt, 0, sizeof(flt));
212 memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
213 memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
214 if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
215 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't set filter %d:%s",
216 errno, strerror(errno));
217 pcap_close(handle);
218 return NULL;
219 }
220
221
222 /* Bind socket to the HCI device */
223 addr.hci_family = AF_BLUETOOTH;
224 addr.hci_dev = handle->md.ifindex;
225 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
226 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't attach to device %d %d:%s",
227 handle->md.ifindex, errno, strerror(errno));
228 pcap_close(handle);
229 return NULL;
230 }
231 handle->selectable_fd = handle->fd;
232
233 return handle;
234 }
235
236 static int
237 bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
238 {
239 struct cmsghdr *cmsg;
240 struct msghdr msg;
241 struct iovec iv;
242 struct pcap_pkthdr pkth;
243
244 iv.iov_base = &handle->buffer[handle->offset];
245 iv.iov_len = handle->snapshot;
246
247 memset(&msg, 0, sizeof(msg));
248 msg.msg_iov = &iv;
249 msg.msg_iovlen = 1;
250 msg.msg_control = handle->buffer;
251 msg.msg_controllen = handle->offset;
252
253 /* ignore interrupt system call error */
254 do {
255 pkth.caplen = recvmsg(handle->fd, &msg, 0);
256 if (handle->break_loop)
257 {
258 handle->break_loop = 0;
259 return -2;
260 }
261 } while ((pkth.caplen == -1) && (errno == EINTR));
262
263
264 if (pkth.caplen < 0) {
265 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s",
266 handle->md.ifindex, errno, strerror(errno));
267 return -1;
268 }
269
270 /* get direction and timestamp*/
271 cmsg = CMSG_FIRSTHDR(&msg);
272 while (cmsg) {
273 int in;
274 switch (cmsg->cmsg_type) {
275 case HCI_CMSG_DIR:
276 in = *((int *) CMSG_DATA(cmsg));
277 break;
278 case HCI_CMSG_TSTAMP:
279 pkth.ts = *((struct timeval *) CMSG_DATA(cmsg));
280 break;
281 }
282 cmsg = CMSG_NXTHDR(&msg, cmsg);
283 }
284 pkth.len = pkth.caplen;
285 callback(user, &pkth, iv.iov_base);
286 return 1;
287 }
288
289 static int
290 bt_inject_linux(pcap_t *handle, const void *buf, size_t size)
291 {
292 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
293 "bluetooth devices");
294 return (-1);
295 }
296
297
298 static void
299 bt_close_linux(pcap_t* handle)
300 {
301 close(handle->fd);
302 free(handle->buffer);
303 }
304
305
306 static int
307 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
308 {
309 int ret;
310 struct hci_dev_info dev_info;
311 struct hci_dev_stats * s = &dev_info.stat;
312 dev_info.dev_id = handle->md.ifindex;
313
314 /* ingnore eintr */
315 do {
316 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
317 } while ((ret == -1) && (errno == EINTR));
318
319 if (ret < 0) {
320 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "can get stats"
321 " via ioctl %d:%s", errno, strerror(errno));
322 return (-1);
323
324 }
325
326 /* we receive both rx and tx frames, so comulate all stats */
327 stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
328 s->acl_tx +s->sco_tx;
329 stats->ps_drop = s->err_rx + s->err_tx;
330 stats->ps_ifdrop = 0;
331 return 0;
332 }
333
334 static int
335 bt_setfilter_linux(pcap_t *p, struct bpf_program *fp)
336 {
337 return 0;
338 }
339
340
341 static int
342 bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
343 {
344 p->direction = d;
345 return 0;
346 }