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