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