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