xref: /freebsd/contrib/libpcap/pcap-bt-monitor-linux.c (revision 6f9cba8f8b5efd16249633e52483ea351876b67b)
1681ed54cSXin LI /*
2681ed54cSXin LI  * Copyright (c) 2014 Michal Labedzki for Tieto Corporation
3681ed54cSXin LI  * All rights reserved.
4681ed54cSXin LI  *
5681ed54cSXin LI  * Redistribution and use in source and binary forms, with or without
6681ed54cSXin LI  * modification, are permitted provided that the following conditions
7681ed54cSXin LI  * are met:
8681ed54cSXin LI  *
9681ed54cSXin LI  * 1. Redistributions of source code must retain the above copyright
10681ed54cSXin LI  * notice, this list of conditions and the following disclaimer.
11681ed54cSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
12681ed54cSXin LI  * notice, this list of conditions and the following disclaimer in the
13681ed54cSXin LI  * documentation and/or other materials provided with the distribution.
14681ed54cSXin LI  * 3. The name of the author may not be used to endorse or promote
15681ed54cSXin LI  * products derived from this software without specific prior written
16681ed54cSXin LI  * permission.
17681ed54cSXin LI  *
18681ed54cSXin LI  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19681ed54cSXin LI  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20681ed54cSXin LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21681ed54cSXin LI  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22681ed54cSXin LI  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23681ed54cSXin LI  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24681ed54cSXin LI  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25681ed54cSXin LI  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26681ed54cSXin LI  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27681ed54cSXin LI  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28681ed54cSXin LI  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29681ed54cSXin LI  *
30681ed54cSXin LI  */
31681ed54cSXin LI 
32681ed54cSXin LI #ifdef HAVE_CONFIG_H
33b00ab754SHans Petter Selasky #include <config.h>
34681ed54cSXin LI #endif
35681ed54cSXin LI 
36681ed54cSXin LI #include <errno.h>
37ada6f083SXin LI #include <stdint.h>
38681ed54cSXin LI #include <stdlib.h>
39681ed54cSXin LI #include <string.h>
40681ed54cSXin LI 
41681ed54cSXin LI #include <bluetooth/bluetooth.h>
42681ed54cSXin LI #include <bluetooth/hci.h>
43681ed54cSXin LI 
44681ed54cSXin LI #include "pcap/bluetooth.h"
45681ed54cSXin LI #include "pcap-int.h"
46681ed54cSXin LI 
47ada6f083SXin LI #include "pcap-bt-monitor-linux.h"
48ada6f083SXin LI 
49681ed54cSXin LI #define BT_CONTROL_SIZE 32
50681ed54cSXin LI #define INTERFACE_NAME "bluetooth-monitor"
51681ed54cSXin LI 
52ada6f083SXin LI /*
53*6f9cba8fSJoseph Mingrone  * Private data.
54*6f9cba8fSJoseph Mingrone  * Currently contains nothing.
55*6f9cba8fSJoseph Mingrone  */
56*6f9cba8fSJoseph Mingrone struct pcap_bt_monitor {
57*6f9cba8fSJoseph Mingrone 	int	dummy;
58*6f9cba8fSJoseph Mingrone };
59*6f9cba8fSJoseph Mingrone 
60*6f9cba8fSJoseph Mingrone /*
61ada6f083SXin LI  * Fields and alignment must match the declaration in the Linux kernel 3.4+.
62ada6f083SXin LI  * See struct hci_mon_hdr in include/net/bluetooth/hci_mon.h.
63ada6f083SXin LI  */
64ada6f083SXin LI struct hci_mon_hdr {
65ada6f083SXin LI     uint16_t opcode;
66ada6f083SXin LI     uint16_t index;
67ada6f083SXin LI     uint16_t len;
68ada6f083SXin LI } __attribute__((packed));
69ada6f083SXin LI 
70681ed54cSXin LI int
71b00ab754SHans Petter Selasky bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str)
72681ed54cSXin LI {
73681ed54cSXin LI     int         ret = 0;
74681ed54cSXin LI 
75b00ab754SHans Petter Selasky     /*
76b00ab754SHans Petter Selasky      * Bluetooth is a wireless technology.
77b00ab754SHans Petter Selasky      *
78b00ab754SHans Petter Selasky      * This is a device to monitor all Bluetooth interfaces, so
79b00ab754SHans Petter Selasky      * there's no notion of "connected" or "disconnected", any
80b00ab754SHans Petter Selasky      * more than there's a notion of "connected" or "disconnected"
81b00ab754SHans Petter Selasky      * for the "any" device.
82b00ab754SHans Petter Selasky      */
83b00ab754SHans Petter Selasky     if (add_dev(devlistp, INTERFACE_NAME,
84b00ab754SHans Petter Selasky                 PCAP_IF_WIRELESS|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
85b00ab754SHans Petter Selasky                 "Bluetooth Linux Monitor", err_str) == NULL)
86681ed54cSXin LI     {
87681ed54cSXin LI         ret = -1;
88681ed54cSXin LI     }
89681ed54cSXin LI 
90681ed54cSXin LI     return ret;
91681ed54cSXin LI }
92681ed54cSXin LI 
93681ed54cSXin LI static int
94681ed54cSXin LI bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
95681ed54cSXin LI {
96681ed54cSXin LI     struct cmsghdr *cmsg;
97681ed54cSXin LI     struct msghdr msg;
98681ed54cSXin LI     struct iovec  iv[2];
99681ed54cSXin LI     ssize_t ret;
100681ed54cSXin LI     struct pcap_pkthdr pkth;
101681ed54cSXin LI     pcap_bluetooth_linux_monitor_header *bthdr;
102ada6f083SXin LI     u_char *pktd;
103ada6f083SXin LI     struct hci_mon_hdr hdr;
104681ed54cSXin LI 
105ada6f083SXin LI     pktd = (u_char *)handle->buffer + BT_CONTROL_SIZE;
106ada6f083SXin LI     bthdr = (pcap_bluetooth_linux_monitor_header*)(void *)pktd;
107681ed54cSXin LI 
108681ed54cSXin LI     iv[0].iov_base = &hdr;
109ada6f083SXin LI     iv[0].iov_len = sizeof(hdr);
110ada6f083SXin LI     iv[1].iov_base = pktd + sizeof(pcap_bluetooth_linux_monitor_header);
111681ed54cSXin LI     iv[1].iov_len = handle->snapshot;
112681ed54cSXin LI 
113681ed54cSXin LI     memset(&pkth.ts, 0, sizeof(pkth.ts));
114681ed54cSXin LI     memset(&msg, 0, sizeof(msg));
115681ed54cSXin LI     msg.msg_iov = iv;
116681ed54cSXin LI     msg.msg_iovlen = 2;
117681ed54cSXin LI     msg.msg_control = handle->buffer;
118ada6f083SXin LI     msg.msg_controllen = BT_CONTROL_SIZE;
119681ed54cSXin LI 
120681ed54cSXin LI     do {
121681ed54cSXin LI         ret = recvmsg(handle->fd, &msg, 0);
122681ed54cSXin LI         if (handle->break_loop)
123681ed54cSXin LI         {
124681ed54cSXin LI             handle->break_loop = 0;
125681ed54cSXin LI             return -2;
126681ed54cSXin LI         }
127681ed54cSXin LI     } while ((ret == -1) && (errno == EINTR));
128681ed54cSXin LI 
129681ed54cSXin LI     if (ret < 0) {
130*6f9cba8fSJoseph Mingrone         if (errno == EAGAIN || errno == EWOULDBLOCK) {
131*6f9cba8fSJoseph Mingrone             /* Nonblocking mode, no data */
132*6f9cba8fSJoseph Mingrone             return 0;
133*6f9cba8fSJoseph Mingrone         }
134b00ab754SHans Petter Selasky         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
135b00ab754SHans Petter Selasky             errno, "Can't receive packet");
136681ed54cSXin LI         return -1;
137681ed54cSXin LI     }
138681ed54cSXin LI 
139*6f9cba8fSJoseph Mingrone     pkth.caplen = (bpf_u_int32)(ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header));
140681ed54cSXin LI     pkth.len = pkth.caplen;
141681ed54cSXin LI 
142681ed54cSXin LI     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
143681ed54cSXin LI         if (cmsg->cmsg_level != SOL_SOCKET) continue;
144681ed54cSXin LI 
145681ed54cSXin LI         if (cmsg->cmsg_type == SCM_TIMESTAMP) {
146681ed54cSXin LI             memcpy(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts));
147681ed54cSXin LI         }
148681ed54cSXin LI     }
149681ed54cSXin LI 
150681ed54cSXin LI     bthdr->adapter_id = htons(hdr.index);
151681ed54cSXin LI     bthdr->opcode = htons(hdr.opcode);
152681ed54cSXin LI 
153681ed54cSXin LI     if (handle->fcode.bf_insns == NULL ||
154*6f9cba8fSJoseph Mingrone         pcap_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
155ada6f083SXin LI         callback(user, &pkth, pktd);
156681ed54cSXin LI         return 1;
157681ed54cSXin LI     }
158681ed54cSXin LI     return 0;   /* didn't pass filter */
159681ed54cSXin LI }
160681ed54cSXin LI 
161681ed54cSXin LI static int
162*6f9cba8fSJoseph Mingrone bt_monitor_inject(pcap_t *handle, const void *buf _U_, int size _U_)
163681ed54cSXin LI {
164*6f9cba8fSJoseph Mingrone     snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
16557e22627SCy Schubert         "Packet injection is not supported yet on Bluetooth monitor devices");
166681ed54cSXin LI     return -1;
167681ed54cSXin LI }
168681ed54cSXin LI 
169681ed54cSXin LI static int
170681ed54cSXin LI bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats)
171681ed54cSXin LI {
172681ed54cSXin LI     stats->ps_recv = 0;
173681ed54cSXin LI     stats->ps_drop = 0;
174681ed54cSXin LI     stats->ps_ifdrop = 0;
175681ed54cSXin LI 
176681ed54cSXin LI     return 0;
177681ed54cSXin LI }
178681ed54cSXin LI 
179681ed54cSXin LI static int
180681ed54cSXin LI bt_monitor_activate(pcap_t* handle)
181681ed54cSXin LI {
182681ed54cSXin LI     struct sockaddr_hci addr;
183681ed54cSXin LI     int err = PCAP_ERROR;
184681ed54cSXin LI     int opt;
185681ed54cSXin LI 
186681ed54cSXin LI     if (handle->opt.rfmon) {
187681ed54cSXin LI         /* monitor mode doesn't apply here */
188681ed54cSXin LI         return PCAP_ERROR_RFMON_NOTSUP;
189681ed54cSXin LI     }
190681ed54cSXin LI 
191b00ab754SHans Petter Selasky     /*
192b00ab754SHans Petter Selasky      * Turn a negative snapshot value (invalid), a snapshot value of
193b00ab754SHans Petter Selasky      * 0 (unspecified), or a value bigger than the normal maximum
194b00ab754SHans Petter Selasky      * value, into the maximum allowed value.
195b00ab754SHans Petter Selasky      *
196b00ab754SHans Petter Selasky      * If some application really *needs* a bigger snapshot
197b00ab754SHans Petter Selasky      * length, we should just increase MAXIMUM_SNAPLEN.
198b00ab754SHans Petter Selasky      */
199b00ab754SHans Petter Selasky     if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
200b00ab754SHans Petter Selasky         handle->snapshot = MAXIMUM_SNAPLEN;
201b00ab754SHans Petter Selasky 
202ada6f083SXin LI     handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot;
203681ed54cSXin LI     handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR;
204681ed54cSXin LI 
205681ed54cSXin LI     handle->read_op = bt_monitor_read;
206681ed54cSXin LI     handle->inject_op = bt_monitor_inject;
207681ed54cSXin LI     handle->setfilter_op = install_bpf_program; /* no kernel filtering */
208*6f9cba8fSJoseph Mingrone     handle->setdirection_op = NULL; /* Not implemented */
209681ed54cSXin LI     handle->set_datalink_op = NULL; /* can't change data link type */
210681ed54cSXin LI     handle->getnonblock_op = pcap_getnonblock_fd;
211681ed54cSXin LI     handle->setnonblock_op = pcap_setnonblock_fd;
212681ed54cSXin LI     handle->stats_op = bt_monitor_stats;
213681ed54cSXin LI 
214681ed54cSXin LI     handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
215681ed54cSXin LI     if (handle->fd < 0) {
216b00ab754SHans Petter Selasky         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
217b00ab754SHans Petter Selasky             errno, "Can't create raw socket");
218681ed54cSXin LI         return PCAP_ERROR;
219681ed54cSXin LI     }
220681ed54cSXin LI 
221681ed54cSXin LI     handle->buffer = malloc(handle->bufsize);
222681ed54cSXin LI     if (!handle->buffer) {
223b00ab754SHans Petter Selasky         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
224b00ab754SHans Petter Selasky             errno, "Can't allocate dump buffer");
225681ed54cSXin LI         goto close_fail;
226681ed54cSXin LI     }
227681ed54cSXin LI 
228681ed54cSXin LI     /* Bind socket to the HCI device */
229681ed54cSXin LI     addr.hci_family = AF_BLUETOOTH;
230681ed54cSXin LI     addr.hci_dev = HCI_DEV_NONE;
231681ed54cSXin LI     addr.hci_channel = HCI_CHANNEL_MONITOR;
232681ed54cSXin LI 
233681ed54cSXin LI     if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
234b00ab754SHans Petter Selasky         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
235b00ab754SHans Petter Selasky             errno, "Can't attach to interface");
236681ed54cSXin LI         goto close_fail;
237681ed54cSXin LI     }
238681ed54cSXin LI 
239681ed54cSXin LI     opt = 1;
240681ed54cSXin LI     if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
241b00ab754SHans Petter Selasky         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
242b00ab754SHans Petter Selasky             errno, "Can't enable time stamp");
243681ed54cSXin LI         goto close_fail;
244681ed54cSXin LI     }
245681ed54cSXin LI 
246681ed54cSXin LI     handle->selectable_fd = handle->fd;
247681ed54cSXin LI 
248681ed54cSXin LI     return 0;
249681ed54cSXin LI 
250681ed54cSXin LI close_fail:
251681ed54cSXin LI     pcap_cleanup_live_common(handle);
252681ed54cSXin LI     return err;
253681ed54cSXin LI }
254681ed54cSXin LI 
255681ed54cSXin LI pcap_t *
256681ed54cSXin LI bt_monitor_create(const char *device, char *ebuf, int *is_ours)
257681ed54cSXin LI {
258681ed54cSXin LI     pcap_t      *p;
259681ed54cSXin LI     const char  *cp;
260681ed54cSXin LI 
261681ed54cSXin LI     cp = strrchr(device, '/');
262681ed54cSXin LI     if (cp == NULL)
263681ed54cSXin LI         cp = device;
264681ed54cSXin LI 
265681ed54cSXin LI     if (strcmp(cp, INTERFACE_NAME) != 0) {
266681ed54cSXin LI         *is_ours = 0;
267681ed54cSXin LI         return NULL;
268681ed54cSXin LI     }
269681ed54cSXin LI 
270681ed54cSXin LI     *is_ours = 1;
271*6f9cba8fSJoseph Mingrone     p = PCAP_CREATE_COMMON(ebuf, struct pcap_bt_monitor);
272681ed54cSXin LI     if (p == NULL)
273681ed54cSXin LI         return NULL;
274681ed54cSXin LI 
275681ed54cSXin LI     p->activate_op = bt_monitor_activate;
276681ed54cSXin LI 
277681ed54cSXin LI     return p;
278681ed54cSXin LI }
279