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