Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / libpcap / pcap-bt-linux.c
blob0c6c08d199eb53cb06ca9984872adc401a3e6ad9
1 /*
2 * Copyright (c) 2006 Paolo Abeni (Italy)
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
30 * Bluetooth sniffing API implementation for Linux platform
31 * By Paolo Abeni <paolo.abeni@email.it>
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
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
43 #include "pcap-int.h"
44 #include "pcap-bt-linux.h"
45 #include "pcap/bluetooth.h"
47 #ifdef NEED_STRERROR_H
48 #include "strerror.h"
49 #endif
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>
60 #include <bluetooth/bluetooth.h>
61 #include <bluetooth/hci.h>
63 #define BT_IFACE "bluetooth"
64 #define BT_CTRL_SIZE 128
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 *);
73 int
74 bt_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
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;
82 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
83 if (sock < 0)
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,
89 "Can't open raw Bluetooth socket: %s", strerror(errno));
90 return -1;
93 dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
94 if (!dev_list)
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;
102 dev_list->dev_num = HCI_MAX_DEV;
104 if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
106 snprintf(err_str, PCAP_ERRBUF_SIZE,
107 "Can't get Bluetooth device list via ioctl: %s",
108 strerror(errno));
109 ret = -1;
110 goto free;
113 dev_req = dev_list->dev_req;
114 for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
115 char dev_name[20], dev_descr[30];
117 snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id);
118 snprintf(dev_descr, 30, "Bluetooth adapter number %d", i);
120 if (pcap_add_if(&found_dev, dev_name, 0,
121 dev_descr, err_str) < 0)
123 ret = -1;
124 break;
129 free:
130 free(dev_list);
132 done:
133 close(sock);
134 return ret;
137 pcap_t *
138 bt_create(const char *device, char *ebuf)
140 pcap_t *p;
142 p = pcap_create_common(device, ebuf);
143 if (p == NULL)
144 return (NULL);
146 p->activate_op = bt_activate;
147 return (p);
150 static int
151 bt_activate(pcap_t* handle)
153 struct sockaddr_hci addr;
154 int opt;
155 int dev_id;
156 struct hci_filter flt;
157 int err = PCAP_ERROR;
159 /* get bt interface id */
160 if (sscanf(handle->opt.source, BT_IFACE"%d", &dev_id) != 1)
162 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
163 "Can't get Bluetooth device index from %s",
164 handle->opt.source);
165 return PCAP_ERROR;
168 /* Initialize some components of the pcap structure. */
169 handle->bufsize = handle->snapshot+BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header);
170 handle->offset = BT_CTRL_SIZE;
171 handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
173 handle->read_op = bt_read_linux;
174 handle->inject_op = bt_inject_linux;
175 handle->setfilter_op = install_bpf_program; /* no kernel filtering */
176 handle->setdirection_op = bt_setdirection_linux;
177 handle->set_datalink_op = NULL; /* can't change data link type */
178 handle->getnonblock_op = pcap_getnonblock_fd;
179 handle->setnonblock_op = pcap_setnonblock_fd;
180 handle->stats_op = bt_stats_linux;
181 handle->md.ifindex = dev_id;
183 /* Create HCI socket */
184 handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
185 if (handle->fd < 0) {
186 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
187 "Can't create raw socket: %s", strerror(errno));
188 return PCAP_ERROR;
191 handle->buffer = malloc(handle->bufsize);
192 if (!handle->buffer) {
193 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
194 pcap_strerror(errno));
195 goto close_fail;
198 opt = 1;
199 if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
200 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
201 "Can't enable data direction info: %s", strerror(errno));
202 goto close_fail;
205 opt = 1;
206 if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
207 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
208 "Can't enable time stamp: %s", strerror(errno));
209 goto close_fail;
212 /* Setup filter, do not call hci function to avoid dependence on
213 * external libs */
214 memset(&flt, 0, sizeof(flt));
215 memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
216 memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
217 if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
218 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
219 "Can't set filter: %s", strerror(errno));
220 goto close_fail;
224 /* Bind socket to the HCI device */
225 addr.hci_family = AF_BLUETOOTH;
226 addr.hci_dev = handle->md.ifindex;
227 if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
228 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
229 "Can't attach to device %d: %s", handle->md.ifindex,
230 strerror(errno));
231 goto close_fail;
234 if (handle->opt.rfmon) {
236 * Monitor mode doesn't apply to Bluetooth devices.
238 err = PCAP_ERROR_RFMON_NOTSUP;
239 goto close_fail;
242 if (handle->opt.buffer_size != 0) {
244 * Set the socket buffer size to the specified value.
246 if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
247 &handle->opt.buffer_size,
248 sizeof(handle->opt.buffer_size)) == -1) {
249 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
250 "SO_RCVBUF: %s", pcap_strerror(errno));
251 goto close_fail;
255 handle->selectable_fd = handle->fd;
256 return 0;
258 close_fail:
259 pcap_cleanup_live_common(handle);
260 return err;
263 static int
264 bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
266 struct cmsghdr *cmsg;
267 struct msghdr msg;
268 struct iovec iv;
269 ssize_t ret;
270 struct pcap_pkthdr pkth;
271 pcap_bluetooth_h4_header* bthdr;
273 bthdr = (pcap_bluetooth_h4_header*) &handle->buffer[handle->offset];
274 iv.iov_base = &handle->buffer[handle->offset+sizeof(pcap_bluetooth_h4_header)];
275 iv.iov_len = handle->snapshot;
277 memset(&msg, 0, sizeof(msg));
278 msg.msg_iov = &iv;
279 msg.msg_iovlen = 1;
280 msg.msg_control = handle->buffer;
281 msg.msg_controllen = handle->offset;
283 /* ignore interrupt system call error */
284 do {
285 ret = recvmsg(handle->fd, &msg, 0);
286 if (handle->break_loop)
288 handle->break_loop = 0;
289 return -2;
291 } while ((ret == -1) && (errno == EINTR));
293 if (ret < 0) {
294 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
295 "Can't receive packet: %s", strerror(errno));
296 return -1;
299 pkth.caplen = ret;
301 /* get direction and timestamp*/
302 cmsg = CMSG_FIRSTHDR(&msg);
303 int in=0;
304 while (cmsg) {
305 switch (cmsg->cmsg_type) {
306 case HCI_CMSG_DIR:
307 memcpy(&in, CMSG_DATA(cmsg), sizeof in);
308 break;
309 case HCI_CMSG_TSTAMP:
310 memcpy(&pkth.ts, CMSG_DATA(cmsg),
311 sizeof pkth.ts);
312 break;
314 cmsg = CMSG_NXTHDR(&msg, cmsg);
316 if ((in && (handle->direction == PCAP_D_OUT)) ||
317 ((!in) && (handle->direction == PCAP_D_IN)))
318 return 0;
320 bthdr->direction = htonl(in != 0);
321 pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
322 pkth.len = pkth.caplen;
323 if (handle->fcode.bf_insns == NULL ||
324 bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset],
325 pkth.len, pkth.caplen)) {
326 callback(user, &pkth, &handle->buffer[handle->offset]);
327 return 1;
329 return 0; /* didn't pass filter */
332 static int
333 bt_inject_linux(pcap_t *handle, const void *buf, size_t size)
335 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
336 "bluetooth devices");
337 return (-1);
341 static int
342 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
344 int ret;
345 struct hci_dev_info dev_info;
346 struct hci_dev_stats * s = &dev_info.stat;
347 dev_info.dev_id = handle->md.ifindex;
349 /* ignore eintr */
350 do {
351 ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
352 } while ((ret == -1) && (errno == EINTR));
354 if (ret < 0) {
355 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
356 "Can't get stats via ioctl: %s", strerror(errno));
357 return (-1);
361 /* we receive both rx and tx frames, so comulate all stats */
362 stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
363 s->acl_tx +s->sco_tx;
364 stats->ps_drop = s->err_rx + s->err_tx;
365 stats->ps_ifdrop = 0;
366 return 0;
369 static int
370 bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
372 p->direction = d;
373 return 0;