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