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