xref: /netbsd-src/external/bsd/libpcap/dist/pcap-bt-linux.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: pcap-bt-linux.c,v 1.1.1.4 2013/12/31 16:57:28 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Paolo Abeni (Italy)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote
17  * products derived from this software without specific prior written
18  * permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Bluetooth sniffing API implementation for Linux platform
33  * By Paolo Abeni <paolo.abeni@email.it>
34  *
35  */
36 #ifndef lint
37 static const char rcsid[] _U_ =
38     "@(#) Header: /tcpdump/master/libpcap/pcap-bt-linux.c,v 1.15 2008-07-01 07:05:54 guy Exp  (LBL)";
39 #endif
40 
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 
45 #include "pcap-int.h"
46 #include "pcap-bt-linux.h"
47 #include "pcap/bluetooth.h"
48 
49 #ifdef NEED_STRERROR_H
50 #include "strerror.h"
51 #endif
52 
53 #include <errno.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <fcntl.h>
57 #include <string.h>
58 #include <sys/ioctl.h>
59 #include <sys/socket.h>
60 #include <arpa/inet.h>
61 
62 #include <bluetooth/bluetooth.h>
63 #include <bluetooth/hci.h>
64 
65 #define BT_IFACE "bluetooth"
66 #define BT_CTRL_SIZE 128
67 
68 /* forward declaration */
69 static int bt_activate(pcap_t *);
70 static int bt_read_linux(pcap_t *, int , pcap_handler , u_char *);
71 static int bt_inject_linux(pcap_t *, const void *, size_t);
72 static int bt_setdirection_linux(pcap_t *, pcap_direction_t);
73 static int bt_stats_linux(pcap_t *, struct pcap_stat *);
74 
75 /*
76  * Private data for capturing on Linux Bluetooth devices.
77  */
78 struct pcap_bt {
79 	int dev_id;		/* device ID of device we're bound to */
80 };
81 
82 int
83 bt_findalldevs(pcap_if_t **alldevsp, char *err_str)
84 {
85 	struct hci_dev_list_req *dev_list;
86 	struct hci_dev_req *dev_req;
87 	int i, sock;
88 	int ret = 0;
89 
90 	sock  = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
91 	if (sock < 0)
92 	{
93 		/* if bluetooth is not supported this this is not fatal*/
94 		if (errno == EAFNOSUPPORT)
95 			return 0;
96 		snprintf(err_str, PCAP_ERRBUF_SIZE,
97 		    "Can't open raw Bluetooth socket: %s", strerror(errno));
98 		return -1;
99 	}
100 
101 	dev_list = malloc(HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
102 	if (!dev_list)
103 	{
104 		snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't allocate %zu bytes for Bluetooth device list",
105 			HCI_MAX_DEV * sizeof(*dev_req) + sizeof(*dev_list));
106 		ret = -1;
107 		goto done;
108 	}
109 
110 	dev_list->dev_num = HCI_MAX_DEV;
111 
112 	if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0)
113 	{
114 		snprintf(err_str, PCAP_ERRBUF_SIZE,
115 		    "Can't get Bluetooth device list via ioctl: %s",
116 		    strerror(errno));
117 		ret = -1;
118 		goto free;
119 	}
120 
121 	dev_req = dev_list->dev_req;
122 	for (i = 0; i < dev_list->dev_num; i++, dev_req++) {
123 		char dev_name[20], dev_descr[30];
124 
125 		snprintf(dev_name, 20, BT_IFACE"%d", dev_req->dev_id);
126 		snprintf(dev_descr, 30, "Bluetooth adapter number %d", i);
127 
128 		if (pcap_add_if(alldevsp, dev_name, 0,
129 		       dev_descr, err_str) < 0)
130 		{
131 			ret = -1;
132 			break;
133 		}
134 
135 	}
136 
137 free:
138 	free(dev_list);
139 
140 done:
141 	close(sock);
142 	return ret;
143 }
144 
145 pcap_t *
146 bt_create(const char *device, char *ebuf, int *is_ours)
147 {
148 	const char *cp;
149 	char *cpend;
150 	long devnum;
151 	pcap_t *p;
152 
153 	/* Does this look like a Bluetooth device? */
154 	cp = strrchr(device, '/');
155 	if (cp == NULL)
156 		cp = device;
157 	/* Does it begin with BT_IFACE? */
158 	if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) {
159 		/* Nope, doesn't begin with BT_IFACE */
160 		*is_ours = 0;
161 		return NULL;
162 	}
163 	/* Yes - is BT_IFACE followed by a number? */
164 	cp += sizeof BT_IFACE - 1;
165 	devnum = strtol(cp, &cpend, 10);
166 	if (cpend == cp || *cpend != '\0') {
167 		/* Not followed by a number. */
168 		*is_ours = 0;
169 		return NULL;
170 	}
171 	if (devnum < 0) {
172 		/* Followed by a non-valid number. */
173 		*is_ours = 0;
174 		return NULL;
175 	}
176 
177 	/* OK, it's probably ours. */
178 	*is_ours = 1;
179 
180 	p = pcap_create_common(device, ebuf, sizeof (struct pcap_bt));
181 	if (p == NULL)
182 		return (NULL);
183 
184 	p->activate_op = bt_activate;
185 	return (p);
186 }
187 
188 static int
189 bt_activate(pcap_t* handle)
190 {
191 	struct pcap_bt *handlep = handle->priv;
192 	struct sockaddr_hci addr;
193 	int opt;
194 	int		dev_id;
195 	struct hci_filter	flt;
196 	int err = PCAP_ERROR;
197 
198 	/* get bt interface id */
199 	if (sscanf(handle->opt.source, BT_IFACE"%d", &dev_id) != 1)
200 	{
201 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
202 			"Can't get Bluetooth device index from %s",
203 			 handle->opt.source);
204 		return PCAP_ERROR;
205 	}
206 
207 	/* Initialize some components of the pcap structure. */
208 	handle->bufsize = handle->snapshot+BT_CTRL_SIZE+sizeof(pcap_bluetooth_h4_header);
209 	handle->offset = BT_CTRL_SIZE;
210 	handle->linktype = DLT_BLUETOOTH_HCI_H4_WITH_PHDR;
211 
212 	handle->read_op = bt_read_linux;
213 	handle->inject_op = bt_inject_linux;
214 	handle->setfilter_op = install_bpf_program; /* no kernel filtering */
215 	handle->setdirection_op = bt_setdirection_linux;
216 	handle->set_datalink_op = NULL;	/* can't change data link type */
217 	handle->getnonblock_op = pcap_getnonblock_fd;
218 	handle->setnonblock_op = pcap_setnonblock_fd;
219 	handle->stats_op = bt_stats_linux;
220 	handlep->dev_id = dev_id;
221 
222 	/* Create HCI socket */
223 	handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
224 	if (handle->fd < 0) {
225 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
226 		    "Can't create raw socket: %s", strerror(errno));
227 		return PCAP_ERROR;
228 	}
229 
230 	handle->buffer = malloc(handle->bufsize);
231 	if (!handle->buffer) {
232 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
233 			pcap_strerror(errno));
234 		goto close_fail;
235 	}
236 
237 	opt = 1;
238 	if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
239 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
240 		    "Can't enable data direction info: %s", strerror(errno));
241 		goto close_fail;
242 	}
243 
244 	opt = 1;
245 	if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
246 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
247 		    "Can't enable time stamp: %s", strerror(errno));
248 		goto close_fail;
249 	}
250 
251 	/* Setup filter, do not call hci function to avoid dependence on
252 	 * external libs	*/
253 	memset(&flt, 0, sizeof(flt));
254 	memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask));
255 	memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask));
256 	if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
257 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
258 		    "Can't set filter: %s", strerror(errno));
259 		goto close_fail;
260 	}
261 
262 
263 	/* Bind socket to the HCI device */
264 	addr.hci_family = AF_BLUETOOTH;
265 	addr.hci_dev = handlep->dev_id;
266 #ifdef SOCKADDR_HCI_HAS_HCI_CHANNEL
267 	addr.hci_channel = HCI_CHANNEL_RAW;
268 #endif
269 	if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
270 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
271 		    "Can't attach to device %d: %s", handlep->dev_id,
272 		    strerror(errno));
273 		goto close_fail;
274 	}
275 
276 	if (handle->opt.rfmon) {
277 		/*
278 		 * Monitor mode doesn't apply to Bluetooth devices.
279 		 */
280 		err = PCAP_ERROR_RFMON_NOTSUP;
281 		goto close_fail;
282 	}
283 
284 	if (handle->opt.buffer_size != 0) {
285 		/*
286 		 * Set the socket buffer size to the specified value.
287 		 */
288 		if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
289 		    &handle->opt.buffer_size,
290 		    sizeof(handle->opt.buffer_size)) == -1) {
291 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
292 				 "SO_RCVBUF: %s", pcap_strerror(errno));
293 			goto close_fail;
294 		}
295 	}
296 
297 	handle->selectable_fd = handle->fd;
298 	return 0;
299 
300 close_fail:
301 	pcap_cleanup_live_common(handle);
302 	return err;
303 }
304 
305 static int
306 bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
307 {
308 	struct cmsghdr *cmsg;
309 	struct msghdr msg;
310 	struct iovec  iv;
311 	ssize_t ret;
312 	struct pcap_pkthdr pkth;
313 	pcap_bluetooth_h4_header* bthdr;
314 
315 	bthdr = (pcap_bluetooth_h4_header*) &handle->buffer[handle->offset];
316 	iv.iov_base = &handle->buffer[handle->offset+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 = handle->offset;
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 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
337 		    "Can't receive packet: %s", strerror(errno));
338 		return -1;
339 	}
340 
341 	pkth.caplen = ret;
342 
343 	/* get direction and timestamp*/
344 	cmsg = CMSG_FIRSTHDR(&msg);
345 	int in=0;
346 	while (cmsg) {
347 		switch (cmsg->cmsg_type) {
348 			case HCI_CMSG_DIR:
349 				memcpy(&in, CMSG_DATA(cmsg), sizeof in);
350 				break;
351                       	case HCI_CMSG_TSTAMP:
352                       		memcpy(&pkth.ts, CMSG_DATA(cmsg),
353                       		    sizeof pkth.ts);
354 				break;
355 		}
356 		cmsg = CMSG_NXTHDR(&msg, cmsg);
357 	}
358 	if ((in && (handle->direction == PCAP_D_OUT)) ||
359 				((!in) && (handle->direction == PCAP_D_IN)))
360 		return 0;
361 
362 	bthdr->direction = htonl(in != 0);
363 	pkth.caplen+=sizeof(pcap_bluetooth_h4_header);
364 	pkth.len = pkth.caplen;
365 	if (handle->fcode.bf_insns == NULL ||
366 	    bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset],
367 	      pkth.len, pkth.caplen)) {
368 		callback(user, &pkth, &handle->buffer[handle->offset]);
369 		return 1;
370 	}
371 	return 0;	/* didn't pass filter */
372 }
373 
374 static int
375 bt_inject_linux(pcap_t *handle, const void *buf, size_t size)
376 {
377 	snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
378     		"bluetooth devices");
379 	return (-1);
380 }
381 
382 
383 static int
384 bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
385 {
386 	struct pcap_bt *handlep = handle->priv;
387 	int ret;
388 	struct hci_dev_info dev_info;
389 	struct hci_dev_stats * s = &dev_info.stat;
390 	dev_info.dev_id = handlep->dev_id;
391 
392 	/* ignore eintr */
393 	do {
394 		ret = ioctl(handle->fd, HCIGETDEVINFO, (void *)&dev_info);
395 	} while ((ret == -1) && (errno == EINTR));
396 
397 	if (ret < 0) {
398 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
399 		    "Can't get stats via ioctl: %s", strerror(errno));
400 		return (-1);
401 
402 	}
403 
404 	/* we receive both rx and tx frames, so comulate all stats */
405 	stats->ps_recv = s->evt_rx + s->acl_rx + s->sco_rx + s->cmd_tx +
406 		s->acl_tx +s->sco_tx;
407 	stats->ps_drop = s->err_rx + s->err_tx;
408 	stats->ps_ifdrop = 0;
409 	return 0;
410 }
411 
412 static int
413 bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
414 {
415 	p->direction = d;
416 	return 0;
417 }
418