]> The Tcpdump Group git mirrors - libpcap/blob - pcap-bt-linux.c
Rename the "private" member of a pcap_t to avoid C++ issues.
[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.15 2008-07-01 07:05:54 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_activate(pcap_t *);
68 static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *);
69 static int bt_inject_linux(pcap_t *, const void *, size_t);
70 static int bt_setdirection_linux(pcap_t *, pcap_direction_t);
71 static int bt_stats_linux(pcap_t *, struct pcap_stat *);
72
73 /*
74 * Private data for capturing on Linux Bluetooth devices.
75 */
76 struct pcap_bt {
77 int dev_id; /* device ID of device we're bound to */
78 };
79
80 int
81 bt_findalldevs(pcap_if_t **alldevsp, char *err_str)
82 {
83 pcap_if_t *found_dev = *alldevsp;
84 struct hci_dev_list_req *dev_list;
85 struct hci_dev_req *dev_req;
86 int i, sock;
87 int ret = 0;
88
89 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
90 if (sock < 0)
91 {
92 /* if bluetooth is not supported this this is not fatal*/
93 if (errno == EAFNOSUPPORT)
94 return 0;
95 snprintf(err_str, PCAP_ERRBUF_SIZE,
96 "Can't open raw Bluetooth socket: %s", strerror(errno));
97 return -1;
98 }
99
100 dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
101 if (!dev_list)
102 {
103 snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list",
104 HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
105 ret = -1;
106 goto done;
107 }
108
109 dev_list->dev_num = HCI_MAX_DEV;
110
111 if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
112 {
113 snprintf(err_str, PCAP_ERRBUF_SIZE,
114 "Can't get Bluetooth device list via ioctl: %s",
115 strerror(errno));
116 ret = -1;
117 goto free;
118 }
119
120 dev_req = dev_list->dev_req;
121 for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
122 char dev_name[20], dev_descr[30];
123
124 snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id);
125 snprintf(dev_descr, 30, "Bluetooth adapter number %d", i);
126
127 if (pcap_add_if(&found_dev, dev_name, 0,
128 dev_descr, err_str) < 0)
129 {
130 ret = -1;
131 break;
132 }
133
134 }
135
136 free:
137 free(dev_list);
138
139 done:
140 close(sock);
141 return ret;
142 }
143
144 pcap_t *
145 bt_create(const char *device, char *ebuf, int *is_ours)
146 {
147 const char *cp;
148 char *cpend;
149 long devnum;
150 pcap_t *p;
151
152 /* Does this look like a Bluetooth device? */
153 cp = strrchr(device, '/');
154 if (cp == NULL)
155 cp = device;
156 /* Does it begin with BT_IFACE? */
157 if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) {
158 /* Nope, doesn't begin with BT_IFACE */
159 *is_ours = 0;
160 return NULL;
161 }
162 /* Yes - is BT_IFACE followed by a number? */
163 cp += sizeof BT_IFACE - 1;
164 devnum = strtol(cp, &cpend, 10);
165 if (cpend == cp || *cpend != '\0') {
166 /* Not followed by a number. */
167 *is_ours = 0;
168 return NULL;
169 }
170 if (devnum < 0) {
171 /* Followed by a non-valid number. */
172 *is_ours = 0;
173 return NULL;
174 }
175
176 /* OK, it's probably ours. */
177 *is_ours = 1;
178
179 p = pcap_create_common(device, ebuf, sizeof (struct pcap_bt));
180 if (p == NULL)
181 return (NULL);
182
183 p->activate_op = bt_activate;
184 return (p);
185 }
186
187 static int
188 bt_activate(pcap_t* handle)
189 {
190 struct pcap_bt *handlep = handle->priv;
191 struct sockaddr_hci addr;
192 int opt;
193 int dev_id;
194 struct hci_filter flt;
195 int err = PCAP_ERROR;
196
197 /* get bt interface id */
198 if (sscanf(handle->opt.source, BT_IFACE"%d", &dev_id) != 1)
199 {
200 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
201 "Can't get Bluetooth device index from %s",
202 handle->opt.source);
203 return PCAP_ERROR;
204 }
205
206 /* Initialize some components of the pcap structure. */
207 handle->bufsize = handle->snapshot+BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header);
208 handle->offset = BT_CTRL_SIZE;
209 handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
210
211 handle->read_op = bt_read_linux;
212 handle->inject_op = bt_inject_linux;
213 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
214 handle->setdirection_op = bt_setdirection_linux;
215 handle->set_datalink_op = NULL; /* can't change data link type */
216 handle->getnonblock_op = pcap_getnonblock_fd;
217 handle->setnonblock_op = pcap_setnonblock_fd;
218 handle->stats_op = bt_stats_linux;
219 handlep->dev_id = dev_id;
220
221 /* Create HCI socket */
222 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
223 if (handle->fd < 0) {
224 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
225 "Can't create raw socket: %s", strerror(errno));
226 return PCAP_ERROR;
227 }
228
229 handle->buffer = malloc(handle->bufsize);
230 if (!handle->buffer) {
231 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
232 pcap_strerror(errno));
233 goto close_fail;
234 }
235
236 opt = 1;
237 if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
238 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
239 "Can't enable data direction info: %s", strerror(errno));
240 goto close_fail;
241 }
242
243 opt = 1;
244 if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
245 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
246 "Can't enable time stamp: %s", strerror(errno));
247 goto close_fail;
248 }
249
250 /* Setup filter, do not call hci function to avoid dependence on
251 * external libs */
252 memset(&flt, 0, sizeof(flt));
253 memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
254 memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
255 if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
256 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
257 "Can't set filter: %s", strerror(errno));
258 goto close_fail;
259 }
260
261
262 /* Bind socket to the HCI device */
263 addr.hci_family = AF_BLUETOOTH;
264 addr.hci_dev = handlep->dev_id;
265 #ifdef SOCKADDR_HCI_HAS_HCI_CHANNEL
266 addr.hci_channel = HCI_CHANNEL_RAW;
267 #endif
268 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
269 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
270 "Can't attach to device %d: %s", handlep->dev_id,
271 strerror(errno));
272 goto close_fail;
273 }
274
275 if (handle->opt.rfmon) {
276 /*
277 * Monitor mode doesn't apply to Bluetooth devices.
278 */
279 err = PCAP_ERROR_RFMON_NOTSUP;
280 goto close_fail;
281 }
282
283 if (handle->opt.buffer_size != 0) {
284 /*
285 * Set the socket buffer size to the specified value.
286 */
287 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
288 &handle->opt.buffer_size,
289 sizeof(handle->opt.buffer_size)) == -1) {
290 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
291 "SO_RCVBUF: %s", pcap_strerror(errno));
292 goto close_fail;
293 }
294 }
295
296 handle->selectable_fd = handle->fd;
297 return 0;
298
299 close_fail:
300 pcap_cleanup_live_common(handle);
301 return err;
302 }
303
304 static int
305 bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
306 {
307 struct cmsghdr *cmsg;
308 struct msghdr msg;
309 struct iovec iv;
310 ssize_t ret;
311 struct pcap_pkthdr pkth;
312 pcap_bluetooth_h4_header* bthdr;
313
314 bthdr = (pcap_bluetooth_h4_header*) &handle->buffer[handle->offset];
315 iv.iov_base = &handle->buffer[handle->offset+sizeof(pcap_bluetooth_h4_header)];
316 iv.iov_len = handle->snapshot;
317
318 memset(&msg, 0, sizeof(msg));
319 msg.msg_iov = &iv;
320 msg.msg_iovlen = 1;
321 msg.msg_control = handle->buffer;
322 msg.msg_controllen = handle->offset;
323
324 /* ignore interrupt system call error */
325 do {
326 ret = recvmsg(handle->fd, &msg, 0);
327 if (handle->break_loop)
328 {
329 handle->break_loop = 0;
330 return -2;
331 }
332 } while ((ret == -1) && (errno == EINTR));
333
334 if (ret < 0) {
335 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
336 "Can't receive packet: %s", strerror(errno));
337 return -1;
338 }
339
340 pkth.caplen = ret;
341
342 /* get direction and timestamp*/
343 cmsg = CMSG_FIRSTHDR(&msg);
344 int in=0;
345 while (cmsg) {
346 switch (cmsg->cmsg_type) {
347 case HCI_CMSG_DIR:
348 memcpy(&in, CMSG_DATA(cmsg), sizeof in);
349 break;
350 case HCI_CMSG_TSTAMP:
351 memcpy(&pkth.ts, CMSG_DATA(cmsg),
352 sizeof pkth.ts);
353 break;
354 }
355 cmsg = CMSG_NXTHDR(&msg, cmsg);
356 }
357 if ((in && (handle->direction == PCAP_D_OUT)) ||
358 ((!in) && (handle->direction == PCAP_D_IN)))
359 return 0;
360
361 bthdr->direction = htonl(in != 0);
362 pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
363 pkth.len = pkth.caplen;
364 if (handle->fcode.bf_insns == NULL ||
365 bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset],
366 pkth.len, pkth.caplen)) {
367 callback(user, &pkth, &handle->buffer[handle->offset]);
368 return 1;
369 }
370 return 0; /* didn't pass filter */
371 }
372
373 static int
374 bt_inject_linux(pcap_t *handle, const void *buf, size_t size)
375 {
376 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
377 "bluetooth devices");
378 return (-1);
379 }
380
381
382 static int
383 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
384 {
385 struct pcap_bt *handlep = handle->priv;
386 int ret;
387 struct hci_dev_info dev_info;
388 struct hci_dev_stats * s = &dev_info.stat;
389 dev_info.dev_id = handlep->dev_id;
390
391 /* ignore eintr */
392 do {
393 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
394 } while ((ret == -1) && (errno == EINTR));
395
396 if (ret < 0) {
397 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
398 "Can't get stats via ioctl: %s", strerror(errno));
399 return (-1);
400
401 }
402
403 /* we receive both rx and tx frames, so comulate all stats */
404 stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
405 s->acl_tx +s->sco_tx;
406 stats->ps_drop = s->err_rx + s->err_tx;
407 stats->ps_ifdrop = 0;
408 return 0;
409 }
410
411 static int
412 bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
413 {
414 p->direction = d;
415 return 0;
416 }