1335640Shselasky/*
2335640Shselasky * Copyright (c) 2006 Paolo Abeni (Italy)
3335640Shselasky * All rights reserved.
4335640Shselasky *
5335640Shselasky * Redistribution and use in source and binary forms, with or without
6335640Shselasky * modification, are permitted provided that the following conditions
7335640Shselasky * are met:
8335640Shselasky *
9335640Shselasky * 1. Redistributions of source code must retain the above copyright
10335640Shselasky * notice, this list of conditions and the following disclaimer.
11335640Shselasky * 2. Redistributions in binary form must reproduce the above copyright
12335640Shselasky * notice, this list of conditions and the following disclaimer in the
13335640Shselasky * documentation and/or other materials provided with the distribution.
14335640Shselasky * 3. The name of the author may not be used to endorse or promote
15335640Shselasky * products derived from this software without specific prior written
16335640Shselasky * permission.
17335640Shselasky *
18335640Shselasky * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19335640Shselasky * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20335640Shselasky * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21335640Shselasky * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22335640Shselasky * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23335640Shselasky * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24335640Shselasky * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25335640Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26335640Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27335640Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28335640Shselasky * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29335640Shselasky *
30335640Shselasky * Bluetooth sniffing API implementation for Linux platform
31335640Shselasky * By Paolo Abeni <paolo.abeni@email.it>
32335640Shselasky *
33335640Shselasky */
34335640Shselasky
35335640Shselasky#ifdef HAVE_CONFIG_H
36335640Shselasky#include <config.h>
37335640Shselasky#endif
38335640Shselasky
39335640Shselasky#include "pcap-int.h"
40335640Shselasky#include "pcap-bt-linux.h"
41335640Shselasky#include "pcap/bluetooth.h"
42335640Shselasky
43335640Shselasky#include <errno.h>
44335640Shselasky#include <stdlib.h>
45335640Shselasky#include <unistd.h>
46335640Shselasky#include <fcntl.h>
47335640Shselasky#include <string.h>
48335640Shselasky#include <sys/ioctl.h>
49335640Shselasky#include <sys/socket.h>
50335640Shselasky#include <arpa/inet.h>
51335640Shselasky
52335640Shselasky#include <bluetooth/bluetooth.h>
53335640Shselasky#include <bluetooth/hci.h>
54335640Shselasky
55335640Shselasky#define BT_IFACE "bluetooth"
56335640Shselasky#define BT_CTRL_SIZE 128
57335640Shselasky
58335640Shselasky/* forward declaration */
59335640Shselaskystatic int bt_activate(pcap_t *);
60335640Shselaskystatic int bt_read_linux(pcap_t *, int , pcap_handler , u_char *);
61335640Shselaskystatic int bt_inject_linux(pcap_t *, const void *, size_t);
62335640Shselaskystatic int bt_setdirection_linux(pcap_t *, pcap_direction_t);
63335640Shselaskystatic int bt_stats_linux(pcap_t *, struct pcap_stat *);
64335640Shselasky
65335640Shselasky/*
66335640Shselasky * Private data for capturing on Linux Bluetooth devices.
67335640Shselasky */
68335640Shselaskystruct pcap_bt {
69335640Shselasky	int dev_id;		/* device ID of device we're bound to */
70335640Shselasky};
71335640Shselasky
72335640Shselaskyint
73335640Shselaskybt_findalldevs(pcap_if_list_t *devlistp, char *err_str)
74335640Shselasky{
75335640Shselasky	struct hci_dev_list_req *dev_list;
76335640Shselasky	struct hci_dev_req *dev_req;
77356341Scy	int sock;
78356341Scy	unsigned i;
79335640Shselasky	int ret = 0;
80335640Shselasky
81335640Shselasky	sock  = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
82335640Shselasky	if (sock < 0)
83335640Shselasky	{
84356341Scy		/* if bluetooth is not supported this is not fatal*/
85335640Shselasky		if (errno == EAFNOSUPPORT)
86335640Shselasky			return 0;
87335640Shselasky		pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
88335640Shselasky		    errno, "Can't open raw Bluetooth socket");
89335640Shselasky		return -1;
90335640Shselasky	}
91335640Shselasky
92335640Shselasky	dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
93335640Shselasky	if (!dev_list)
94335640Shselasky	{
95335640Shselasky		pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list",
96335640Shselasky			HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
97335640Shselasky		ret = -1;
98335640Shselasky		goto done;
99335640Shselasky	}
100335640Shselasky
101335640Shselasky	dev_list->dev_num = HCI_MAX_DEV;
102335640Shselasky
103335640Shselasky	if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
104335640Shselasky	{
105335640Shselasky		pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
106335640Shselasky		    errno, "Can't get Bluetooth device list via ioctl");
107335640Shselasky		ret = -1;
108335640Shselasky		goto free;
109335640Shselasky	}
110335640Shselasky
111335640Shselasky	dev_req = dev_list->dev_req;
112335640Shselasky	for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
113356341Scy		char dev_name[20], dev_descr[40];
114335640Shselasky
115356341Scy		pcap_snprintf(dev_name, sizeof(dev_name), BT_IFACE"%u", dev_req->dev_id);
116356341Scy		pcap_snprintf(dev_descr, sizeof(dev_descr), "Bluetooth adapter number %u", i);
117335640Shselasky
118335640Shselasky		/*
119335640Shselasky		 * Bluetooth is a wireless technology.
120335640Shselasky		 * XXX - if there's the notion of associating with a
121335640Shselasky		 * network, and we can determine whether the interface
122335640Shselasky		 * is associated with a network, check that and set
123335640Shselasky		 * the status to PCAP_IF_CONNECTION_STATUS_CONNECTED
124335640Shselasky		 * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED.
125335640Shselasky		 */
126335640Shselasky		if (add_dev(devlistp, dev_name, PCAP_IF_WIRELESS, dev_descr, err_str)  == NULL)
127335640Shselasky		{
128335640Shselasky			ret = -1;
129335640Shselasky			break;
130335640Shselasky		}
131335640Shselasky	}
132335640Shselasky
133335640Shselaskyfree:
134335640Shselasky	free(dev_list);
135335640Shselasky
136335640Shselaskydone:
137335640Shselasky	close(sock);
138335640Shselasky	return ret;
139335640Shselasky}
140335640Shselasky
141335640Shselaskypcap_t *
142335640Shselaskybt_create(const char *device, char *ebuf, int *is_ours)
143335640Shselasky{
144335640Shselasky	const char *cp;
145335640Shselasky	char *cpend;
146335640Shselasky	long devnum;
147335640Shselasky	pcap_t *p;
148335640Shselasky
149335640Shselasky	/* Does this look like a Bluetooth device? */
150335640Shselasky	cp = strrchr(device, '/');
151335640Shselasky	if (cp == NULL)
152335640Shselasky		cp = device;
153335640Shselasky	/* Does it begin with BT_IFACE? */
154335640Shselasky	if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) {
155335640Shselasky		/* Nope, doesn't begin with BT_IFACE */
156335640Shselasky		*is_ours = 0;
157335640Shselasky		return NULL;
158335640Shselasky	}
159335640Shselasky	/* Yes - is BT_IFACE followed by a number? */
160335640Shselasky	cp += sizeof BT_IFACE - 1;
161335640Shselasky	devnum = strtol(cp, &cpend, 10);
162335640Shselasky	if (cpend == cp || *cpend != '\0') {
163335640Shselasky		/* Not followed by a number. */
164335640Shselasky		*is_ours = 0;
165335640Shselasky		return NULL;
166335640Shselasky	}
167335640Shselasky	if (devnum < 0) {
168335640Shselasky		/* Followed by a non-valid number. */
169335640Shselasky		*is_ours = 0;
170335640Shselasky		return NULL;
171335640Shselasky	}
172335640Shselasky
173335640Shselasky	/* OK, it's probably ours. */
174335640Shselasky	*is_ours = 1;
175335640Shselasky
176335640Shselasky	p = pcap_create_common(ebuf, sizeof (struct pcap_bt));
177335640Shselasky	if (p == NULL)
178335640Shselasky		return (NULL);
179335640Shselasky
180335640Shselasky	p->activate_op = bt_activate;
181335640Shselasky	return (p);
182335640Shselasky}
183335640Shselasky
184335640Shselaskystatic int
185335640Shselaskybt_activate(pcap_t* handle)
186335640Shselasky{
187335640Shselasky	struct pcap_bt *handlep = handle->priv;
188335640Shselasky	struct sockaddr_hci addr;
189335640Shselasky	int opt;
190335640Shselasky	int		dev_id;
191335640Shselasky	struct hci_filter	flt;
192335640Shselasky	int err = PCAP_ERROR;
193335640Shselasky
194335640Shselasky	/* get bt interface id */
195335640Shselasky	if (sscanf(handle->opt.device, BT_IFACE"%d", &dev_id) != 1)
196335640Shselasky	{
197335640Shselasky		pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
198335640Shselasky			"Can't get Bluetooth device index from %s",
199335640Shselasky			 handle->opt.device);
200335640Shselasky		return PCAP_ERROR;
201335640Shselasky	}
202335640Shselasky
203335640Shselasky	/*
204335640Shselasky	 * Turn a negative snapshot value (invalid), a snapshot value of
205335640Shselasky	 * 0 (unspecified), or a value bigger than the normal maximum
206335640Shselasky	 * value, into the maximum allowed value.
207335640Shselasky	 *
208335640Shselasky	 * If some application really *needs* a bigger snapshot
209335640Shselasky	 * length, we should just increase MAXIMUM_SNAPLEN.
210335640Shselasky	 */
211335640Shselasky	if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
212335640Shselasky		handle->snapshot = MAXIMUM_SNAPLEN;
213335640Shselasky
214335640Shselasky	/* Initialize some components of the pcap structure. */
215335640Shselasky	handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot;
216335640Shselasky	handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
217335640Shselasky
218335640Shselasky	handle->read_op = bt_read_linux;
219335640Shselasky	handle->inject_op = bt_inject_linux;
220335640Shselasky	handle->setfilter_op = install_bpf_program; /* no kernel filtering */
221335640Shselasky	handle->setdirection_op = bt_setdirection_linux;
222335640Shselasky	handle->set_datalink_op = NULL;	/* can't change data link type */
223335640Shselasky	handle->getnonblock_op = pcap_getnonblock_fd;
224335640Shselasky	handle->setnonblock_op = pcap_setnonblock_fd;
225335640Shselasky	handle->stats_op = bt_stats_linux;
226335640Shselasky	handlep->dev_id = dev_id;
227335640Shselasky
228335640Shselasky	/* Create HCI socket */
229335640Shselasky	handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
230335640Shselasky	if (handle->fd < 0) {
231335640Shselasky		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
232335640Shselasky		    errno, "Can't create raw socket");
233335640Shselasky		return PCAP_ERROR;
234335640Shselasky	}
235335640Shselasky
236335640Shselasky	handle->buffer = malloc(handle->bufsize);
237335640Shselasky	if (!handle->buffer) {
238335640Shselasky		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
239335640Shselasky		    errno, "Can't allocate dump buffer");
240335640Shselasky		goto close_fail;
241335640Shselasky	}
242335640Shselasky
243335640Shselasky	opt = 1;
244335640Shselasky	if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
245335640Shselasky		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
246335640Shselasky		    errno, "Can't enable data direction info");
247335640Shselasky		goto close_fail;
248335640Shselasky	}
249335640Shselasky
250335640Shselasky	opt = 1;
251335640Shselasky	if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
252335640Shselasky		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
253335640Shselasky		    errno, "Can't enable time stamp");
254335640Shselasky		goto close_fail;
255335640Shselasky	}
256335640Shselasky
257335640Shselasky	/* Setup filter, do not call hci function to avoid dependence on
258335640Shselasky	 * external libs	*/
259335640Shselasky	memset(&flt, 0, sizeof(flt));
260335640Shselasky	memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
261335640Shselasky	memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
262335640Shselasky	if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
263335640Shselasky		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
264335640Shselasky		    errno, "Can't set filter");
265335640Shselasky		goto close_fail;
266335640Shselasky	}
267335640Shselasky
268335640Shselasky
269335640Shselasky	/* Bind socket to the HCI device */
270335640Shselasky	addr.hci_family = AF_BLUETOOTH;
271335640Shselasky	addr.hci_dev = handlep->dev_id;
272335640Shselasky#ifdef HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL
273335640Shselasky	addr.hci_channel = HCI_CHANNEL_RAW;
274335640Shselasky#endif
275335640Shselasky	if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
276335640Shselasky		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
277335640Shselasky		    errno, "Can't attach to device %d", handlep->dev_id);
278335640Shselasky		goto close_fail;
279335640Shselasky	}
280335640Shselasky
281335640Shselasky	if (handle->opt.rfmon) {
282335640Shselasky		/*
283335640Shselasky		 * Monitor mode doesn't apply to Bluetooth devices.
284335640Shselasky		 */
285335640Shselasky		err = PCAP_ERROR_RFMON_NOTSUP;
286335640Shselasky		goto close_fail;
287335640Shselasky	}
288335640Shselasky
289335640Shselasky	if (handle->opt.buffer_size != 0) {
290335640Shselasky		/*
291335640Shselasky		 * Set the socket buffer size to the specified value.
292335640Shselasky		 */
293335640Shselasky		if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
294335640Shselasky		    &handle->opt.buffer_size,
295335640Shselasky		    sizeof(handle->opt.buffer_size)) == -1) {
296335640Shselasky			pcap_fmt_errmsg_for_errno(handle->errbuf,
297335640Shselasky			    errno, PCAP_ERRBUF_SIZE, "SO_RCVBUF");
298335640Shselasky			goto close_fail;
299335640Shselasky		}
300335640Shselasky	}
301335640Shselasky
302335640Shselasky	handle->selectable_fd = handle->fd;
303335640Shselasky	return 0;
304335640Shselasky
305335640Shselaskyclose_fail:
306335640Shselasky	pcap_cleanup_live_common(handle);
307335640Shselasky	return err;
308335640Shselasky}
309335640Shselasky
310335640Shselaskystatic int
311335640Shselaskybt_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
312335640Shselasky{
313335640Shselasky	struct cmsghdr *cmsg;
314335640Shselasky	struct msghdr msg;
315335640Shselasky	struct iovec  iv;
316335640Shselasky	ssize_t ret;
317335640Shselasky	struct pcap_pkthdr pkth;
318335640Shselasky	pcap_bluetooth_h4_header* bthdr;
319335640Shselasky	u_char *pktd;
320335640Shselasky	int in = 0;
321335640Shselasky
322335640Shselasky	pktd = (u_char *)handle->buffer + BT_CTRL_SIZE;
323335640Shselasky	bthdr = (pcap_bluetooth_h4_header*)(void *)pktd;
324335640Shselasky	iv.iov_base = pktd + sizeof(pcap_bluetooth_h4_header);
325335640Shselasky	iv.iov_len  = handle->snapshot;
326335640Shselasky
327335640Shselasky	memset(&msg, 0, sizeof(msg));
328335640Shselasky	msg.msg_iov = &iv;
329335640Shselasky	msg.msg_iovlen = 1;
330335640Shselasky	msg.msg_control = handle->buffer;
331335640Shselasky	msg.msg_controllen = BT_CTRL_SIZE;
332335640Shselasky
333335640Shselasky	/* ignore interrupt system call error */
334335640Shselasky	do {
335335640Shselasky		ret = recvmsg(handle->fd, &msg, 0);
336335640Shselasky		if (handle->break_loop)
337335640Shselasky		{
338335640Shselasky			handle->break_loop = 0;
339335640Shselasky			return -2;
340335640Shselasky		}
341335640Shselasky	} while ((ret == -1) && (errno == EINTR));
342335640Shselasky
343335640Shselasky	if (ret < 0) {
344335640Shselasky		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
345335640Shselasky		    errno, "Can't receive packet");
346335640Shselasky		return -1;
347335640Shselasky	}
348335640Shselasky
349335640Shselasky	pkth.caplen = ret;
350335640Shselasky
351335640Shselasky	/* get direction and timestamp*/
352335640Shselasky	cmsg = CMSG_FIRSTHDR(&msg);
353335640Shselasky	while (cmsg) {
354335640Shselasky		switch (cmsg->cmsg_type) {
355335640Shselasky			case HCI_CMSG_DIR:
356335640Shselasky				memcpy(&in, CMSG_DATA(cmsg), sizeof in);
357335640Shselasky				break;
358335640Shselasky                      	case HCI_CMSG_TSTAMP:
359335640Shselasky                      		memcpy(&pkth.ts, CMSG_DATA(cmsg),
360335640Shselasky                      		    sizeof pkth.ts);
361335640Shselasky				break;
362335640Shselasky		}
363335640Shselasky		cmsg = CMSG_NXTHDR(&msg, cmsg);
364335640Shselasky	}
365335640Shselasky	if ((in && (handle->direction == PCAP_D_OUT)) ||
366335640Shselasky				((!in) && (handle->direction == PCAP_D_IN)))
367335640Shselasky		return 0;
368335640Shselasky
369335640Shselasky	bthdr->direction = htonl(in != 0);
370335640Shselasky	pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
371335640Shselasky	pkth.len = pkth.caplen;
372335640Shselasky	if (handle->fcode.bf_insns == NULL ||
373335640Shselasky	    bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
374335640Shselasky		callback(user, &pkth, pktd);
375335640Shselasky		return 1;
376335640Shselasky	}
377335640Shselasky	return 0;	/* didn't pass filter */
378335640Shselasky}
379335640Shselasky
380335640Shselaskystatic int
381335640Shselaskybt_inject_linux(pcap_t *handle, const void *buf _U_, size_t size _U_)
382335640Shselasky{
383356341Scy	pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
384356341Scy	    "Packet injection is not supported on Bluetooth devices");
385335640Shselasky	return (-1);
386335640Shselasky}
387335640Shselasky
388335640Shselasky
389335640Shselaskystatic int
390335640Shselaskybt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
391335640Shselasky{
392335640Shselasky	struct pcap_bt *handlep = handle->priv;
393335640Shselasky	int ret;
394335640Shselasky	struct hci_dev_info dev_info;
395335640Shselasky	struct hci_dev_stats * s = &dev_info.stat;
396335640Shselasky	dev_info.dev_id = handlep->dev_id;
397335640Shselasky
398335640Shselasky	/* ignore eintr */
399335640Shselasky	do {
400335640Shselasky		ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
401335640Shselasky	} while ((ret == -1) && (errno == EINTR));
402335640Shselasky
403335640Shselasky	if (ret < 0) {
404335640Shselasky		pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
405335640Shselasky		    errno, "Can't get stats via ioctl");
406335640Shselasky		return (-1);
407335640Shselasky
408335640Shselasky	}
409335640Shselasky
410335640Shselasky	/* we receive both rx and tx frames, so comulate all stats */
411335640Shselasky	stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
412335640Shselasky		s->acl_tx +s->sco_tx;
413335640Shselasky	stats->ps_drop = s->err_rx + s->err_tx;
414335640Shselasky	stats->ps_ifdrop = 0;
415335640Shselasky	return 0;
416335640Shselasky}
417335640Shselasky
418335640Shselaskystatic int
419335640Shselaskybt_setdirection_linux(pcap_t *p, pcap_direction_t d)
420335640Shselasky{
421335640Shselasky	p->direction = d;
422335640Shselasky	return 0;
423335640Shselasky}
424