xref: /freebsd/contrib/libpcap/pcap-bt-linux.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
1a8e07101SRui Paulo /*
2a8e07101SRui Paulo  * Copyright (c) 2006 Paolo Abeni (Italy)
3a8e07101SRui Paulo  * All rights reserved.
4a8e07101SRui Paulo  *
5a8e07101SRui Paulo  * Redistribution and use in source and binary forms, with or without
6a8e07101SRui Paulo  * modification, are permitted provided that the following conditions
7a8e07101SRui Paulo  * are met:
8a8e07101SRui Paulo  *
9a8e07101SRui Paulo  * 1. Redistributions of source code must retain the above copyright
10a8e07101SRui Paulo  * notice, this list of conditions and the following disclaimer.
11a8e07101SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
12a8e07101SRui Paulo  * notice, this list of conditions and the following disclaimer in the
13a8e07101SRui Paulo  * documentation and/or other materials provided with the distribution.
14a8e07101SRui Paulo  * 3. The name of the author may not be used to endorse or promote
15a8e07101SRui Paulo  * products derived from this software without specific prior written
16a8e07101SRui Paulo  * permission.
17a8e07101SRui Paulo  *
18a8e07101SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a8e07101SRui Paulo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a8e07101SRui Paulo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a8e07101SRui Paulo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22a8e07101SRui Paulo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23a8e07101SRui Paulo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24a8e07101SRui Paulo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a8e07101SRui Paulo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a8e07101SRui Paulo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a8e07101SRui Paulo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a8e07101SRui Paulo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a8e07101SRui Paulo  *
30a8e07101SRui Paulo  * Bluetooth sniffing API implementation for Linux platform
31a8e07101SRui Paulo  * By Paolo Abeni <paolo.abeni@email.it>
32a8e07101SRui Paulo  *
33a8e07101SRui Paulo  */
34a8e07101SRui Paulo 
35b00ab754SHans Petter Selasky #include <config.h>
36a8e07101SRui Paulo 
37a8e07101SRui Paulo #include "pcap-int.h"
38a8e07101SRui Paulo #include "pcap-bt-linux.h"
39a8e07101SRui Paulo #include "pcap/bluetooth.h"
40*afdbf109SJoseph Mingrone #include "diag-control.h"
41a8e07101SRui Paulo 
42a8e07101SRui Paulo #include <errno.h>
43a8e07101SRui Paulo #include <stdlib.h>
44a8e07101SRui Paulo #include <unistd.h>
45a8e07101SRui Paulo #include <fcntl.h>
46a8e07101SRui Paulo #include <string.h>
47a8e07101SRui Paulo #include <sys/ioctl.h>
48a8e07101SRui Paulo #include <sys/socket.h>
49a8e07101SRui Paulo #include <arpa/inet.h>
50a8e07101SRui Paulo 
51a8e07101SRui Paulo #include <bluetooth/bluetooth.h>
52a8e07101SRui Paulo #include <bluetooth/hci.h>
53a8e07101SRui Paulo 
54a8e07101SRui Paulo #define BT_IFACE "bluetooth"
55a8e07101SRui Paulo #define BT_CTRL_SIZE 128
56a8e07101SRui Paulo 
57a8e07101SRui Paulo /* forward declaration */
58a8e07101SRui Paulo static int bt_activate(pcap_t *);
59a8e07101SRui Paulo static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *);
606f9cba8fSJoseph Mingrone static int bt_inject_linux(pcap_t *, const void *, int);
61a8e07101SRui Paulo static int bt_setdirection_linux(pcap_t *, pcap_direction_t);
62a8e07101SRui Paulo static int bt_stats_linux(pcap_t *, struct pcap_stat *);
63a8e07101SRui Paulo 
64681ed54cSXin LI /*
65681ed54cSXin LI  * Private data for capturing on Linux Bluetooth devices.
66681ed54cSXin LI  */
67681ed54cSXin LI struct pcap_bt {
68681ed54cSXin LI 	int dev_id;		/* device ID of device we're bound to */
69681ed54cSXin LI };
70681ed54cSXin LI 
71a8e07101SRui Paulo int
bt_findalldevs(pcap_if_list_t * devlistp,char * err_str)72b00ab754SHans Petter Selasky bt_findalldevs(pcap_if_list_t *devlistp, char *err_str)
73a8e07101SRui Paulo {
74a8e07101SRui Paulo 	struct hci_dev_list_req *dev_list;
75a8e07101SRui Paulo 	struct hci_dev_req *dev_req;
7657e22627SCy Schubert 	int sock;
7757e22627SCy Schubert 	unsigned i;
78a8e07101SRui Paulo 	int ret = 0;
79a8e07101SRui Paulo 
80a8e07101SRui Paulo 	sock  = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
81a8e07101SRui Paulo 	if (sock < 0)
82a8e07101SRui Paulo 	{
8357e22627SCy Schubert 		/* if bluetooth is not supported this is not fatal*/
84a8e07101SRui Paulo 		if (errno == EAFNOSUPPORT)
85a8e07101SRui Paulo 			return 0;
86*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
87b00ab754SHans Petter Selasky 		    errno, "Can't open raw Bluetooth socket");
88*afdbf109SJoseph Mingrone 		return PCAP_ERROR;
89a8e07101SRui Paulo 	}
90a8e07101SRui Paulo 
91a8e07101SRui Paulo 	dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
92a8e07101SRui Paulo 	if (!dev_list)
93a8e07101SRui Paulo 	{
946f9cba8fSJoseph Mingrone 		snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list",
95a8e07101SRui Paulo 			HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
96*afdbf109SJoseph Mingrone 		ret = PCAP_ERROR;
97a8e07101SRui Paulo 		goto done;
98a8e07101SRui Paulo 	}
99a8e07101SRui Paulo 
1006f9cba8fSJoseph Mingrone 	/*
1016f9cba8fSJoseph Mingrone 	 * Zero the complete header, which is larger than dev_num because of tail
1026f9cba8fSJoseph Mingrone 	 * padding, to silence Valgrind, which overshoots validating that dev_num
1036f9cba8fSJoseph Mingrone 	 * has been set.
1046f9cba8fSJoseph Mingrone 	 * https://github.com/the-tcpdump-group/libpcap/issues/1083
1056f9cba8fSJoseph Mingrone 	 * https://bugs.kde.org/show_bug.cgi?id=448464
1066f9cba8fSJoseph Mingrone 	 */
1076f9cba8fSJoseph Mingrone 	memset(dev_list, 0, sizeof(*dev_list));
108a8e07101SRui Paulo 	dev_list->dev_num = HCI_MAX_DEV;
109a8e07101SRui Paulo 
110a8e07101SRui Paulo 	if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
111a8e07101SRui Paulo 	{
112*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE,
113b00ab754SHans Petter Selasky 		    errno, "Can't get Bluetooth device list via ioctl");
114*afdbf109SJoseph Mingrone 		ret = PCAP_ERROR;
115a8e07101SRui Paulo 		goto free;
116a8e07101SRui Paulo 	}
117a8e07101SRui Paulo 
118a8e07101SRui Paulo 	dev_req = dev_list->dev_req;
119a8e07101SRui Paulo 	for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
12057e22627SCy Schubert 		char dev_name[20], dev_descr[40];
121a8e07101SRui Paulo 
1226f9cba8fSJoseph Mingrone 		snprintf(dev_name, sizeof(dev_name), BT_IFACE"%u", dev_req->dev_id);
1236f9cba8fSJoseph Mingrone 		snprintf(dev_descr, sizeof(dev_descr), "Bluetooth adapter number %u", i);
124a8e07101SRui Paulo 
125b00ab754SHans Petter Selasky 		/*
126b00ab754SHans Petter Selasky 		 * Bluetooth is a wireless technology.
127b00ab754SHans Petter Selasky 		 * XXX - if there's the notion of associating with a
128b00ab754SHans Petter Selasky 		 * network, and we can determine whether the interface
129b00ab754SHans Petter Selasky 		 * is associated with a network, check that and set
130b00ab754SHans Petter Selasky 		 * the status to PCAP_IF_CONNECTION_STATUS_CONNECTED
131b00ab754SHans Petter Selasky 		 * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED.
132b00ab754SHans Petter Selasky 		 */
133*afdbf109SJoseph Mingrone 		if (pcapint_add_dev(devlistp, dev_name, PCAP_IF_WIRELESS, dev_descr, err_str)  == NULL)
134a8e07101SRui Paulo 		{
135*afdbf109SJoseph Mingrone 			ret = PCAP_ERROR;
136a8e07101SRui Paulo 			break;
137a8e07101SRui Paulo 		}
138a8e07101SRui Paulo 	}
139a8e07101SRui Paulo 
140a8e07101SRui Paulo free:
141a8e07101SRui Paulo 	free(dev_list);
142a8e07101SRui Paulo 
143a8e07101SRui Paulo done:
144a8e07101SRui Paulo 	close(sock);
145a8e07101SRui Paulo 	return ret;
146a8e07101SRui Paulo }
147a8e07101SRui Paulo 
148a8e07101SRui Paulo pcap_t *
bt_create(const char * device,char * ebuf,int * is_ours)149edc89b24SXin LI bt_create(const char *device, char *ebuf, int *is_ours)
150a8e07101SRui Paulo {
151edc89b24SXin LI 	const char *cp;
152edc89b24SXin LI 	char *cpend;
153edc89b24SXin LI 	long devnum;
154a8e07101SRui Paulo 	pcap_t *p;
155a8e07101SRui Paulo 
156edc89b24SXin LI 	/* Does this look like a Bluetooth device? */
157edc89b24SXin LI 	cp = strrchr(device, '/');
158edc89b24SXin LI 	if (cp == NULL)
159edc89b24SXin LI 		cp = device;
160edc89b24SXin LI 	/* Does it begin with BT_IFACE? */
161edc89b24SXin LI 	if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) {
162edc89b24SXin LI 		/* Nope, doesn't begin with BT_IFACE */
163edc89b24SXin LI 		*is_ours = 0;
164edc89b24SXin LI 		return NULL;
165edc89b24SXin LI 	}
166edc89b24SXin LI 	/* Yes - is BT_IFACE followed by a number? */
167edc89b24SXin LI 	cp += sizeof BT_IFACE - 1;
168edc89b24SXin LI 	devnum = strtol(cp, &cpend, 10);
169edc89b24SXin LI 	if (cpend == cp || *cpend != '\0') {
170edc89b24SXin LI 		/* Not followed by a number. */
171edc89b24SXin LI 		*is_ours = 0;
172edc89b24SXin LI 		return NULL;
173edc89b24SXin LI 	}
174edc89b24SXin LI 	if (devnum < 0) {
175edc89b24SXin LI 		/* Followed by a non-valid number. */
176edc89b24SXin LI 		*is_ours = 0;
177edc89b24SXin LI 		return NULL;
178edc89b24SXin LI 	}
179edc89b24SXin LI 
180edc89b24SXin LI 	/* OK, it's probably ours. */
181edc89b24SXin LI 	*is_ours = 1;
182edc89b24SXin LI 
1836f9cba8fSJoseph Mingrone 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_bt);
184a8e07101SRui Paulo 	if (p == NULL)
185a8e07101SRui Paulo 		return (NULL);
186a8e07101SRui Paulo 
187a8e07101SRui Paulo 	p->activate_op = bt_activate;
188a8e07101SRui Paulo 	return (p);
189a8e07101SRui Paulo }
190a8e07101SRui Paulo 
191a8e07101SRui Paulo static int
bt_activate(pcap_t * handle)192a8e07101SRui Paulo bt_activate(pcap_t* handle)
193a8e07101SRui Paulo {
194681ed54cSXin LI 	struct pcap_bt *handlep = handle->priv;
195a8e07101SRui Paulo 	struct sockaddr_hci addr;
196a8e07101SRui Paulo 	int opt;
197a8e07101SRui Paulo 	int		dev_id;
198a8e07101SRui Paulo 	struct hci_filter	flt;
199a8e07101SRui Paulo 	int err = PCAP_ERROR;
200a8e07101SRui Paulo 
201a8e07101SRui Paulo 	/* get bt interface id */
202ada6f083SXin LI 	if (sscanf(handle->opt.device, BT_IFACE"%d", &dev_id) != 1)
203a8e07101SRui Paulo 	{
2046f9cba8fSJoseph Mingrone 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
205a8e07101SRui Paulo 			"Can't get Bluetooth device index from %s",
206ada6f083SXin LI 			 handle->opt.device);
207a8e07101SRui Paulo 		return PCAP_ERROR;
208a8e07101SRui Paulo 	}
209a8e07101SRui Paulo 
210b00ab754SHans Petter Selasky 	/*
211b00ab754SHans Petter Selasky 	 * Turn a negative snapshot value (invalid), a snapshot value of
212b00ab754SHans Petter Selasky 	 * 0 (unspecified), or a value bigger than the normal maximum
213b00ab754SHans Petter Selasky 	 * value, into the maximum allowed value.
214b00ab754SHans Petter Selasky 	 *
215b00ab754SHans Petter Selasky 	 * If some application really *needs* a bigger snapshot
216b00ab754SHans Petter Selasky 	 * length, we should just increase MAXIMUM_SNAPLEN.
217b00ab754SHans Petter Selasky 	 */
218b00ab754SHans Petter Selasky 	if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
219b00ab754SHans Petter Selasky 		handle->snapshot = MAXIMUM_SNAPLEN;
220b00ab754SHans Petter Selasky 
221a8e07101SRui Paulo 	/* Initialize some components of the pcap structure. */
222ada6f083SXin LI 	handle->bufsize = BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header)+handle->snapshot;
223a8e07101SRui Paulo 	handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
224a8e07101SRui Paulo 
225a8e07101SRui Paulo 	handle->read_op = bt_read_linux;
226a8e07101SRui Paulo 	handle->inject_op = bt_inject_linux;
227*afdbf109SJoseph Mingrone 	handle->setfilter_op = pcapint_install_bpf_program; /* no kernel filtering */
228a8e07101SRui Paulo 	handle->setdirection_op = bt_setdirection_linux;
229a8e07101SRui Paulo 	handle->set_datalink_op = NULL;	/* can't change data link type */
230*afdbf109SJoseph Mingrone 	handle->getnonblock_op = pcapint_getnonblock_fd;
231*afdbf109SJoseph Mingrone 	handle->setnonblock_op = pcapint_setnonblock_fd;
232a8e07101SRui Paulo 	handle->stats_op = bt_stats_linux;
233681ed54cSXin LI 	handlep->dev_id = dev_id;
234a8e07101SRui Paulo 
235a8e07101SRui Paulo 	/* Create HCI socket */
236a8e07101SRui Paulo 	handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
237a8e07101SRui Paulo 	if (handle->fd < 0) {
238*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
239b00ab754SHans Petter Selasky 		    errno, "Can't create raw socket");
240a8e07101SRui Paulo 		return PCAP_ERROR;
241a8e07101SRui Paulo 	}
242a8e07101SRui Paulo 
243a8e07101SRui Paulo 	handle->buffer = malloc(handle->bufsize);
244a8e07101SRui Paulo 	if (!handle->buffer) {
245*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
246b00ab754SHans Petter Selasky 		    errno, "Can't allocate dump buffer");
247a8e07101SRui Paulo 		goto close_fail;
248a8e07101SRui Paulo 	}
249a8e07101SRui Paulo 
250a8e07101SRui Paulo 	opt = 1;
251a8e07101SRui Paulo 	if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
252*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
253b00ab754SHans Petter Selasky 		    errno, "Can't enable data direction info");
254a8e07101SRui Paulo 		goto close_fail;
255a8e07101SRui Paulo 	}
256a8e07101SRui Paulo 
257a8e07101SRui Paulo 	opt = 1;
258a8e07101SRui Paulo 	if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
259*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
260b00ab754SHans Petter Selasky 		    errno, "Can't enable time stamp");
261a8e07101SRui Paulo 		goto close_fail;
262a8e07101SRui Paulo 	}
263a8e07101SRui Paulo 
264a8e07101SRui Paulo 	/* Setup filter, do not call hci function to avoid dependence on
265a8e07101SRui Paulo 	 * external libs	*/
266a8e07101SRui Paulo 	memset(&flt, 0, sizeof(flt));
267a8e07101SRui Paulo 	memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
268a8e07101SRui Paulo 	memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
269a8e07101SRui Paulo 	if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
270*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
271b00ab754SHans Petter Selasky 		    errno, "Can't set filter");
272a8e07101SRui Paulo 		goto close_fail;
273a8e07101SRui Paulo 	}
274a8e07101SRui Paulo 
275a8e07101SRui Paulo 
276a8e07101SRui Paulo 	/* Bind socket to the HCI device */
277a8e07101SRui Paulo 	addr.hci_family = AF_BLUETOOTH;
278681ed54cSXin LI 	addr.hci_dev = handlep->dev_id;
279b00ab754SHans Petter Selasky #ifdef HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL
280edc89b24SXin LI 	addr.hci_channel = HCI_CHANNEL_RAW;
281edc89b24SXin LI #endif
282a8e07101SRui Paulo 	if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
283*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
284b00ab754SHans Petter Selasky 		    errno, "Can't attach to device %d", handlep->dev_id);
285a8e07101SRui Paulo 		goto close_fail;
286a8e07101SRui Paulo 	}
287a8e07101SRui Paulo 
288a8e07101SRui Paulo 	if (handle->opt.rfmon) {
289a8e07101SRui Paulo 		/*
290a8e07101SRui Paulo 		 * Monitor mode doesn't apply to Bluetooth devices.
291a8e07101SRui Paulo 		 */
292a8e07101SRui Paulo 		err = PCAP_ERROR_RFMON_NOTSUP;
293a8e07101SRui Paulo 		goto close_fail;
294a8e07101SRui Paulo 	}
295a8e07101SRui Paulo 
296d1e87331SXin LI 	if (handle->opt.buffer_size != 0) {
297a8e07101SRui Paulo 		/*
298a8e07101SRui Paulo 		 * Set the socket buffer size to the specified value.
299a8e07101SRui Paulo 		 */
300a8e07101SRui Paulo 		if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
301a8e07101SRui Paulo 		    &handle->opt.buffer_size,
302a8e07101SRui Paulo 		    sizeof(handle->opt.buffer_size)) == -1) {
303*afdbf109SJoseph Mingrone 			pcapint_fmt_errmsg_for_errno(handle->errbuf,
304b00ab754SHans Petter Selasky 			    errno, PCAP_ERRBUF_SIZE, "SO_RCVBUF");
305a8e07101SRui Paulo 			goto close_fail;
306a8e07101SRui Paulo 		}
307a8e07101SRui Paulo 	}
308a8e07101SRui Paulo 
309a8e07101SRui Paulo 	handle->selectable_fd = handle->fd;
310a8e07101SRui Paulo 	return 0;
311a8e07101SRui Paulo 
312a8e07101SRui Paulo close_fail:
313*afdbf109SJoseph Mingrone 	pcapint_cleanup_live_common(handle);
314a8e07101SRui Paulo 	return err;
315a8e07101SRui Paulo }
316a8e07101SRui Paulo 
317a8e07101SRui Paulo static int
bt_read_linux(pcap_t * handle,int max_packets _U_,pcap_handler callback,u_char * user)318b00ab754SHans Petter Selasky bt_read_linux(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
319a8e07101SRui Paulo {
320a8e07101SRui Paulo 	struct cmsghdr *cmsg;
321a8e07101SRui Paulo 	struct msghdr msg;
322a8e07101SRui Paulo 	struct iovec  iv;
323d1e87331SXin LI 	ssize_t ret;
324a8e07101SRui Paulo 	struct pcap_pkthdr pkth;
325a8e07101SRui Paulo 	pcap_bluetooth_h4_header* bthdr;
326ada6f083SXin LI 	u_char *pktd;
327ada6f083SXin LI 	int in = 0;
328a8e07101SRui Paulo 
329ada6f083SXin LI 	pktd = (u_char *)handle->buffer + BT_CTRL_SIZE;
330ada6f083SXin LI 	bthdr = (pcap_bluetooth_h4_header*)(void *)pktd;
331ada6f083SXin LI 	iv.iov_base = pktd + sizeof(pcap_bluetooth_h4_header);
332a8e07101SRui Paulo 	iv.iov_len  = handle->snapshot;
333a8e07101SRui Paulo 
334a8e07101SRui Paulo 	memset(&msg, 0, sizeof(msg));
335a8e07101SRui Paulo 	msg.msg_iov = &iv;
336a8e07101SRui Paulo 	msg.msg_iovlen = 1;
337a8e07101SRui Paulo 	msg.msg_control = handle->buffer;
338ada6f083SXin LI 	msg.msg_controllen = BT_CTRL_SIZE;
339a8e07101SRui Paulo 
340a8e07101SRui Paulo 	/* ignore interrupt system call error */
341a8e07101SRui Paulo 	do {
342a8e07101SRui Paulo 		if (handle->break_loop)
343a8e07101SRui Paulo 		{
344a8e07101SRui Paulo 			handle->break_loop = 0;
345*afdbf109SJoseph Mingrone 			return PCAP_ERROR_BREAK;
346a8e07101SRui Paulo 		}
347*afdbf109SJoseph Mingrone 		ret = recvmsg(handle->fd, &msg, 0);
348d1e87331SXin LI 	} while ((ret == -1) && (errno == EINTR));
349a8e07101SRui Paulo 
350d1e87331SXin LI 	if (ret < 0) {
3516f9cba8fSJoseph Mingrone 		if (errno == EAGAIN || errno == EWOULDBLOCK) {
3526f9cba8fSJoseph Mingrone 			/* Nonblocking mode, no data */
3536f9cba8fSJoseph Mingrone 			return 0;
3546f9cba8fSJoseph Mingrone 		}
355*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
356b00ab754SHans Petter Selasky 		    errno, "Can't receive packet");
357*afdbf109SJoseph Mingrone 		return PCAP_ERROR;
358a8e07101SRui Paulo 	}
359a8e07101SRui Paulo 
3606f9cba8fSJoseph Mingrone 	pkth.caplen = (bpf_u_int32)ret;
361d1e87331SXin LI 
362a8e07101SRui Paulo 	/* get direction and timestamp*/
363a8e07101SRui Paulo 	cmsg = CMSG_FIRSTHDR(&msg);
364a8e07101SRui Paulo 	while (cmsg) {
365a8e07101SRui Paulo 		switch (cmsg->cmsg_type) {
366a8e07101SRui Paulo 			case HCI_CMSG_DIR:
367d1e87331SXin LI 				memcpy(&in, CMSG_DATA(cmsg), sizeof in);
368a8e07101SRui Paulo 				break;
369a8e07101SRui Paulo 			case HCI_CMSG_TSTAMP:
370d1e87331SXin LI 				memcpy(&pkth.ts, CMSG_DATA(cmsg),
371d1e87331SXin LI 					sizeof pkth.ts);
372a8e07101SRui Paulo 				break;
373a8e07101SRui Paulo 		}
374*afdbf109SJoseph Mingrone 		// for musl libc CMSG_NXTHDR()
375*afdbf109SJoseph Mingrone DIAG_OFF_SIGN_COMPARE
376a8e07101SRui Paulo 		cmsg = CMSG_NXTHDR(&msg, cmsg);
377*afdbf109SJoseph Mingrone DIAG_ON_SIGN_COMPARE
378a8e07101SRui Paulo 	}
3796f9cba8fSJoseph Mingrone 	switch (handle->direction) {
3806f9cba8fSJoseph Mingrone 
3816f9cba8fSJoseph Mingrone 	case PCAP_D_IN:
3826f9cba8fSJoseph Mingrone 		if (!in)
383a8e07101SRui Paulo 			return 0;
3846f9cba8fSJoseph Mingrone 		break;
3856f9cba8fSJoseph Mingrone 
3866f9cba8fSJoseph Mingrone 	case PCAP_D_OUT:
3876f9cba8fSJoseph Mingrone 		if (in)
3886f9cba8fSJoseph Mingrone 			return 0;
3896f9cba8fSJoseph Mingrone 		break;
3906f9cba8fSJoseph Mingrone 
3916f9cba8fSJoseph Mingrone 	default:
3926f9cba8fSJoseph Mingrone 		break;
3936f9cba8fSJoseph Mingrone 	}
394a8e07101SRui Paulo 
395a8e07101SRui Paulo 	bthdr->direction = htonl(in != 0);
396a8e07101SRui Paulo 	pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
397a8e07101SRui Paulo 	pkth.len = pkth.caplen;
398d1e87331SXin LI 	if (handle->fcode.bf_insns == NULL ||
399*afdbf109SJoseph Mingrone 	    pcapint_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
400ada6f083SXin LI 		callback(user, &pkth, pktd);
401a8e07101SRui Paulo 		return 1;
402a8e07101SRui Paulo 	}
403d1e87331SXin LI 	return 0;	/* didn't pass filter */
404d1e87331SXin LI }
405a8e07101SRui Paulo 
406a8e07101SRui Paulo static int
bt_inject_linux(pcap_t * handle,const void * buf _U_,int size _U_)4076f9cba8fSJoseph Mingrone bt_inject_linux(pcap_t *handle, const void *buf _U_, int size _U_)
408a8e07101SRui Paulo {
4096f9cba8fSJoseph Mingrone 	snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
41057e22627SCy Schubert 	    "Packet injection is not supported on Bluetooth devices");
411a8e07101SRui Paulo 	return (-1);
412a8e07101SRui Paulo }
413a8e07101SRui Paulo 
414a8e07101SRui Paulo 
415a8e07101SRui Paulo static int
bt_stats_linux(pcap_t * handle,struct pcap_stat * stats)416a8e07101SRui Paulo bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
417a8e07101SRui Paulo {
418681ed54cSXin LI 	struct pcap_bt *handlep = handle->priv;
419a8e07101SRui Paulo 	int ret;
420a8e07101SRui Paulo 	struct hci_dev_info dev_info;
421a8e07101SRui Paulo 	struct hci_dev_stats * s = &dev_info.stat;
422681ed54cSXin LI 	dev_info.dev_id = handlep->dev_id;
423a8e07101SRui Paulo 
424d1e87331SXin LI 	/* ignore eintr */
425a8e07101SRui Paulo 	do {
426a8e07101SRui Paulo 		ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
427a8e07101SRui Paulo 	} while ((ret == -1) && (errno == EINTR));
428a8e07101SRui Paulo 
429a8e07101SRui Paulo 	if (ret < 0) {
430*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
431b00ab754SHans Petter Selasky 		    errno, "Can't get stats via ioctl");
432a8e07101SRui Paulo 		return (-1);
433a8e07101SRui Paulo 
434a8e07101SRui Paulo 	}
435a8e07101SRui Paulo 
436*afdbf109SJoseph Mingrone 	/* we receive both rx and tx frames, so cumulate all stats */
437a8e07101SRui Paulo 	stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
438a8e07101SRui Paulo 		s->acl_tx +s->sco_tx;
439a8e07101SRui Paulo 	stats->ps_drop = s->err_rx + s->err_tx;
440a8e07101SRui Paulo 	stats->ps_ifdrop = 0;
441a8e07101SRui Paulo 	return 0;
442a8e07101SRui Paulo }
443a8e07101SRui Paulo 
444a8e07101SRui Paulo static int
bt_setdirection_linux(pcap_t * p,pcap_direction_t d)445a8e07101SRui Paulo bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
446a8e07101SRui Paulo {
4476f9cba8fSJoseph Mingrone 	/*
4486f9cba8fSJoseph Mingrone 	 * It's guaranteed, at this point, that d is a valid
4496f9cba8fSJoseph Mingrone 	 * direction value.
4506f9cba8fSJoseph Mingrone 	 */
451a8e07101SRui Paulo 	p->direction = d;
452a8e07101SRui Paulo 	return 0;
453a8e07101SRui Paulo }
454