]> The Tcpdump Group git mirrors - libpcap/blob - pcap-bt-linux.c
Handle kernels that don't have ETHTOOL_GFLAGS.
[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 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 %zu 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_create(const char *device, char *ebuf)
138 {
139 pcap_t *p;
140
141 p = pcap_create_common(device, ebuf);
142 if (p == NULL)
143 return (NULL);
144
145 p->activate_op = bt_activate;
146 return (p);
147 }
148
149 static int
150 bt_activate(pcap_t* handle)
151 {
152 struct sockaddr_hci addr;
153 int opt;
154 int dev_id;
155 struct hci_filter flt;
156 int err = PCAP_ERROR;
157
158 /* get bt interface id */
159 if (sscanf(handle->opt.source, BT_IFACE"%d", &dev_id) != 1)
160 {
161 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
162 "Can't get Bluetooth device index from %s",
163 handle->opt.source);
164 return PCAP_ERROR;
165 }
166
167 /* Initialize some components of the pcap structure. */
168 handle->bufsize = handle->snapshot+BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header);
169 handle->offset = BT_CTRL_SIZE;
170 handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
171
172 handle->read_op = bt_read_linux;
173 handle->inject_op = bt_inject_linux;
174 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
175 handle->setdirection_op = bt_setdirection_linux;
176 handle->set_datalink_op = NULL; /* can't change data link type */
177 handle->getnonblock_op = pcap_getnonblock_fd;
178 handle->setnonblock_op = pcap_setnonblock_fd;
179 handle->stats_op = bt_stats_linux;
180 handle->md.ifindex = dev_id;
181
182 /* Create HCI socket */
183 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
184 if (handle->fd < 0) {
185 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s",
186 errno, strerror(errno));
187 return PCAP_ERROR;
188 }
189
190 handle->buffer = malloc(handle->bufsize);
191 if (!handle->buffer) {
192 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
193 pcap_strerror(errno));
194 goto close_fail;
195 }
196
197 opt = 1;
198 if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
199 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't enable data direction info %d:%s",
200 errno, strerror(errno));
201 goto close_fail;
202 }
203
204 opt = 1;
205 if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
206 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't enable time stamp %d:%s",
207 errno, strerror(errno));
208 goto close_fail;
209 }
210
211 /* Setup filter, do not call hci function to avoid dependence on
212 * external libs */
213 memset(&flt, 0, sizeof(flt));
214 memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
215 memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
216 if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
217 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't set filter %d:%s",
218 errno, strerror(errno));
219 goto close_fail;
220 }
221
222
223 /* Bind socket to the HCI device */
224 addr.hci_family = AF_BLUETOOTH;
225 addr.hci_dev = handle->md.ifindex;
226 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
227 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't attach to device %d %d:%s",
228 handle->md.ifindex, errno, strerror(errno));
229 goto close_fail;
230 }
231
232 if (handle->opt.rfmon) {
233 /*
234 * Monitor mode doesn't apply to Bluetooth devices.
235 */
236 err = PCAP_ERROR_RFMON_NOTSUP;
237 goto close_fail;
238 }
239
240 if (handle->opt.buffer_size == 0) {
241 /*
242 * Set the socket buffer size to the specified value.
243 */
244 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
245 &handle->opt.buffer_size,
246 sizeof(handle->opt.buffer_size)) == -1) {
247 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
248 "SO_RCVBUF: %s", pcap_strerror(errno));
249 goto close_fail;
250 }
251 }
252
253 handle->selectable_fd = handle->fd;
254 return 0;
255
256 close_fail:
257 pcap_cleanup_live_common(handle);
258 return err;
259 }
260
261 static int
262 bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
263 {
264 struct cmsghdr *cmsg;
265 struct msghdr msg;
266 struct iovec iv;
267 struct pcap_pkthdr pkth;
268 pcap_bluetooth_h4_header* bthdr;
269
270 bthdr = (pcap_bluetooth_h4_header*) &handle->buffer[handle->offset];
271 iv.iov_base = &handle->buffer[handle->offset+sizeof(pcap_bluetooth_h4_header)];
272 iv.iov_len = handle->snapshot;
273
274 memset(&msg, 0, sizeof(msg));
275 msg.msg_iov = &iv;
276 msg.msg_iovlen = 1;
277 msg.msg_control = handle->buffer;
278 msg.msg_controllen = handle->offset;
279
280 /* ignore interrupt system call error */
281 do {
282 pkth.caplen = recvmsg(handle->fd, &msg, 0);
283 if (handle->break_loop)
284 {
285 handle->break_loop = 0;
286 return -2;
287 }
288 } while ((pkth.caplen == -1) && (errno == EINTR));
289
290
291 if (pkth.caplen < 0) {
292 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s",
293 errno, strerror(errno));
294 return -1;
295 }
296
297 /* get direction and timestamp*/
298 cmsg = CMSG_FIRSTHDR(&msg);
299 int in=0;
300 while (cmsg) {
301 switch (cmsg->cmsg_type) {
302 case HCI_CMSG_DIR:
303 memcpy(&in, CMSG_DATA(cmsg), sizeof in);
304 break;
305 case HCI_CMSG_TSTAMP:
306 memcpy(&pkth.ts, CMSG_DATA(cmsg),
307 sizeof pkth.ts);
308 break;
309 }
310 cmsg = CMSG_NXTHDR(&msg, cmsg);
311 }
312 if ((in && (handle->direction == PCAP_D_OUT)) ||
313 ((!in) && (handle->direction == PCAP_D_IN)))
314 return 0;
315
316 bthdr->direction = htonl(in != 0);
317 pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
318 pkth.len = pkth.caplen;
319 if (handle->fcode.bf_insns == NULL ||
320 bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset],
321 pkth.len, pkth.caplen)) {
322 callback(user, &pkth, &handle->buffer[handle->offset]);
323 return 1;
324 }
325 return 0; /* didn't pass filter */
326 }
327
328 static int
329 bt_inject_linux(pcap_t *handle, const void *buf, size_t size)
330 {
331 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
332 "bluetooth devices");
333 return (-1);
334 }
335
336
337 static int
338 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
339 {
340 int ret;
341 struct hci_dev_info dev_info;
342 struct hci_dev_stats * s = &dev_info.stat;
343 dev_info.dev_id = handle->md.ifindex;
344
345 /* ingnore eintr */
346 do {
347 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
348 } while ((ret == -1) && (errno == EINTR));
349
350 if (ret < 0) {
351 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "can get stats"
352 " via ioctl %d:%s", errno, strerror(errno));
353 return (-1);
354
355 }
356
357 /* we receive both rx and tx frames, so comulate all stats */
358 stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
359 s->acl_tx +s->sco_tx;
360 stats->ps_drop = s->err_rx + s->err_tx;
361 stats->ps_ifdrop = 0;
362 return 0;
363 }
364
365 static int
366 bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
367 {
368 p->direction = d;
369 return 0;
370 }