]> The Tcpdump Group git mirrors - libpcap/blob - pcap-bt-linux.c
From Paolo Abeni:
[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. 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 #ifndef lint
35 static const char rcsid[] _U_ =
36 "@(#) $Header: /tcpdump/master/libpcap/pcap-bt-linux.c,v 1.9 2007-09-22 02:10:17 guy Exp $ (LBL)";
37 #endif
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include "pcap-int.h"
44 #include "pcap-bt-linux.h"
45 #include "pcap/bluetooth.h"
46
47 #ifdef NEED_STRERROR_H
48 #include "strerror.h"
49 #endif
50
51 #include <errno.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <string.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <arpa/inet.h>
59
60 #include <bluetooth/bluetooth.h>
61 #include <bluetooth/hci.h>
62
63 #define BT_IFACE "bluetooth"
64 #define BT_CTRL_SIZE 128
65
66 /* forward declaration */
67 static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *);
68 static int bt_inject_linux(pcap_t *, const void *, size_t);
69 static int bt_setfilter_linux(pcap_t *, struct bpf_program *);
70 static int bt_setdirection_linux(pcap_t *, pcap_direction_t);
71 static int bt_stats_linux(pcap_t *, struct pcap_stat *);
72 static void bt_close_linux(pcap_t *);
73
74 int
75 bt_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
76 {
77 pcap_if_t *found_dev = *alldevsp;
78 struct hci_dev_list_req *dev_list;
79 struct hci_dev_req *dev_req;
80 int i, sock;
81 int ret = 0;
82
83 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
84 if (sock < 0)
85 {
86 /* if bluetooth is not supported this this is not fatal*/
87 if (errno == EAFNOSUPPORT)
88 return 0;
89 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't open raw Bluetooth socket %d:%s",
90 errno, strerror(errno));
91 return -1;
92 }
93
94 dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
95 if (!dev_list)
96 {
97 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %d bytes for Bluetooth device list",
98 HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
99 ret = -1;
100 goto done;
101 }
102
103 dev_list->dev_num = HCI_MAX_DEV;
104
105 if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
106 {
107 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't get Bluetooth device list via ioctl %d:%s",
108 errno, strerror(errno));
109 ret = -1;
110 goto free;
111 }
112
113 dev_req = dev_list->dev_req;
114 for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
115 char dev_name[20], dev_descr[30];
116
117 snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id);
118 snprintf(dev_descr, 30, "Bluetooth adapter number %d", i);
119
120 if (pcap_add_if(&found_dev, dev_name, 0,
121 dev_descr, err_str) < 0)
122 {
123 ret = -1;
124 break;
125 }
126
127 }
128
129 free:
130 free(dev_list);
131
132 done:
133 close(sock);
134 return ret;
135 }
136
137 pcap_t*
138 bt_open_live(const char* bus, int snaplen, int promisc , int to_ms, char* errmsg)
139 {
140 struct sockaddr_hci addr;
141 int opt;
142 pcap_t *handle;
143 int dev_id;
144 struct hci_filter flt;
145
146 /* get bt interface id */
147 if (sscanf(bus, BT_IFACE"%d", &dev_id) != 1)
148 {
149 snprintf(errmsg, PCAP_ERRBUF_SIZE,
150 "Can't get Bluetooth bus index from %s", bus);
151 return NULL;
152 }
153
154 /* Allocate a handle for this session. */
155 handle = malloc(sizeof(*handle));
156 if (handle == NULL) {
157 snprintf(errmsg, PCAP_ERRBUF_SIZE, "malloc: %s",
158 pcap_strerror(errno));
159 return NULL;
160 }
161
162 /* Initialize some components of the pcap structure. */
163 memset(handle, 0, sizeof(*handle));
164 handle->snapshot = snaplen;
165 handle->md.timeout = to_ms;
166 handle->bufsize = snaplen+BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header);
167 handle->offset = BT_CTRL_SIZE;
168 handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
169
170 handle->read_op = bt_read_linux;
171 handle->inject_op = bt_inject_linux;
172 handle->setfilter_op = bt_setfilter_linux;
173 handle->setdirection_op = bt_setdirection_linux;
174 handle->set_datalink_op = NULL; /* can't change data link type */
175 handle->getnonblock_op = pcap_getnonblock_fd;
176 handle->setnonblock_op = pcap_setnonblock_fd;
177 handle->stats_op = bt_stats_linux;
178 handle->close_op = bt_close_linux;
179 handle->md.ifindex = dev_id;
180
181 /* Create HCI socket */
182 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
183 if (handle->fd < 0) {
184 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s",
185 errno, strerror(errno));
186 free(handle);
187 return NULL;
188 }
189
190 handle->buffer = malloc(handle->bufsize);
191 if (!handle->buffer) {
192 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
193 pcap_strerror(errno));
194 pcap_close(handle);
195 return NULL;
196 }
197
198 opt = 1;
199 if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
200 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't enable data direction info %d:%s",
201 errno, strerror(errno));
202 pcap_close(handle);
203 return NULL;
204 }
205
206 opt = 1;
207 if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
208 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't enable time stamp %d:%s",
209 errno, strerror(errno));
210 pcap_close(handle);
211 return NULL;
212 }
213
214 /* Setup filter, do not call hci function to avoid dependence on
215 * external libs */
216 memset(&flt, 0, sizeof(flt));
217 memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
218 memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
219 if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
220 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't set filter %d:%s",
221 errno, strerror(errno));
222 pcap_close(handle);
223 return NULL;
224 }
225
226
227 /* Bind socket to the HCI device */
228 addr.hci_family = AF_BLUETOOTH;
229 addr.hci_dev = handle->md.ifindex;
230 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
231 snprintf(errmsg, PCAP_ERRBUF_SIZE, "Can't attach to device %d %d:%s",
232 handle->md.ifindex, errno, strerror(errno));
233 pcap_close(handle);
234 return NULL;
235 }
236 handle->selectable_fd = handle->fd;
237
238 return handle;
239 }
240
241 static int
242 bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
243 {
244 struct cmsghdr *cmsg;
245 struct msghdr msg;
246 struct iovec iv;
247 struct pcap_pkthdr pkth;
248 pcap_bluetooth_h4_header* bthdr;
249
250 bthdr = (pcap_bluetooth_h4_header*) &handle->buffer[handle->offset];
251 iv.iov_base = &handle->buffer[handle->offset+sizeof(pcap_bluetooth_h4_header)];
252 iv.iov_len = handle->snapshot;
253
254 memset(&msg, 0, sizeof(msg));
255 msg.msg_iov = &iv;
256 msg.msg_iovlen = 1;
257 msg.msg_control = handle->buffer;
258 msg.msg_controllen = handle->offset;
259
260 /* ignore interrupt system call error */
261 do {
262 pkth.caplen = recvmsg(handle->fd, &msg, 0);
263 if (handle->break_loop)
264 {
265 handle->break_loop = 0;
266 return -2;
267 }
268 } while ((pkth.caplen == -1) && (errno == EINTR));
269
270
271 if (pkth.caplen < 0) {
272 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s",
273 errno, strerror(errno));
274 return -1;
275 }
276
277 /* get direction and timestamp*/
278 cmsg = CMSG_FIRSTHDR(&msg);
279 int in=0;
280 while (cmsg) {
281 switch (cmsg->cmsg_type) {
282 case HCI_CMSG_DIR:
283 in = *((int *) CMSG_DATA(cmsg));
284 break;
285 case HCI_CMSG_TSTAMP:
286 pkth.ts = *((struct timeval *) CMSG_DATA(cmsg));
287 break;
288 }
289 cmsg = CMSG_NXTHDR(&msg, cmsg);
290 }
291 if ((in && (handle->direction == PCAP_D_OUT)) ||
292 ((!in) && (handle->direction == PCAP_D_IN)))
293 return 0;
294
295 bthdr->direction = htonl(in != 0);
296 pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
297 pkth.len = pkth.caplen;
298 callback(user, &pkth, &handle->buffer[handle->offset]);
299 return 1;
300 }
301
302 static int
303 bt_inject_linux(pcap_t *handle, const void *buf, size_t size)
304 {
305 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
306 "bluetooth devices");
307 return (-1);
308 }
309
310
311 static void
312 bt_close_linux(pcap_t* handle)
313 {
314 close(handle->fd);
315 free(handle->buffer);
316 }
317
318
319 static int
320 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
321 {
322 int ret;
323 struct hci_dev_info dev_info;
324 struct hci_dev_stats * s = &dev_info.stat;
325 dev_info.dev_id = handle->md.ifindex;
326
327 /* ingnore eintr */
328 do {
329 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
330 } while ((ret == -1) && (errno == EINTR));
331
332 if (ret < 0) {
333 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "can get stats"
334 " via ioctl %d:%s", errno, strerror(errno));
335 return (-1);
336
337 }
338
339 /* we receive both rx and tx frames, so comulate all stats */
340 stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
341 s->acl_tx +s->sco_tx;
342 stats->ps_drop = s->err_rx + s->err_tx;
343 stats->ps_ifdrop = 0;
344 return 0;
345 }
346
347 static int
348 bt_setfilter_linux(pcap_t *p, struct bpf_program *fp)
349 {
350 return 0;
351 }
352
353
354 static int
355 bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
356 {
357 p->direction = d;
358 return 0;
359 }