xref: /netbsd-src/external/bsd/libpcap/dist/pcap-bt-monitor-linux.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: pcap-bt-monitor-linux.c,v 1.4 2017/01/24 22:29:28 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2014 Michal Labedzki for Tieto Corporation
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  */
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: pcap-bt-monitor-linux.c,v 1.4 2017/01/24 22:29:28 christos Exp $");
36 
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 
41 #include <errno.h>
42 #include <stdint.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include <bluetooth/bluetooth.h>
47 #include <bluetooth/hci.h>
48 
49 #include "pcap/bluetooth.h"
50 #include "pcap-int.h"
51 
52 #include "pcap-bt-monitor-linux.h"
53 
54 #define BT_CONTROL_SIZE 32
55 #define INTERFACE_NAME "bluetooth-monitor"
56 
57 /*
58  * Fields and alignment must match the declaration in the Linux kernel 3.4+.
59  * See struct hci_mon_hdr in include/net/bluetooth/hci_mon.h.
60  */
61 struct hci_mon_hdr {
62     uint16_t opcode;
63     uint16_t index;
64     uint16_t len;
65 } __attribute__((packed));
66 
67 int
68 bt_monitor_findalldevs(pcap_if_t **alldevsp, char *err_str)
69 {
70     int         ret = 0;
71 
72     if (pcap_add_if(alldevsp, INTERFACE_NAME, 0,
73                "Bluetooth Linux Monitor", err_str) < 0)
74     {
75         ret = -1;
76     }
77 
78     return ret;
79 }
80 
81 static int
82 bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
83 {
84     struct cmsghdr *cmsg;
85     struct msghdr msg;
86     struct iovec  iv[2];
87     ssize_t ret;
88     struct pcap_pkthdr pkth;
89     pcap_bluetooth_linux_monitor_header *bthdr;
90     u_char *pktd;
91     struct hci_mon_hdr hdr;
92 
93     pktd = (u_char *)handle->buffer + BT_CONTROL_SIZE;
94     bthdr = (pcap_bluetooth_linux_monitor_header*)(void *)pktd;
95 
96     iv[0].iov_base = &hdr;
97     iv[0].iov_len = sizeof(hdr);
98     iv[1].iov_base = pktd + sizeof(pcap_bluetooth_linux_monitor_header);
99     iv[1].iov_len = handle->snapshot;
100 
101     memset(&pkth.ts, 0, sizeof(pkth.ts));
102     memset(&msg, 0, sizeof(msg));
103     msg.msg_iov = iv;
104     msg.msg_iovlen = 2;
105     msg.msg_control = handle->buffer;
106     msg.msg_controllen = BT_CONTROL_SIZE;
107 
108     do {
109         ret = recvmsg(handle->fd, &msg, 0);
110         if (handle->break_loop)
111         {
112             handle->break_loop = 0;
113             return -2;
114         }
115     } while ((ret == -1) && (errno == EINTR));
116 
117     if (ret < 0) {
118         pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
119             "Can't receive packet: %s", strerror(errno));
120         return -1;
121     }
122 
123     pkth.caplen = ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header);
124     pkth.len = pkth.caplen;
125 
126     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
127         if (cmsg->cmsg_level != SOL_SOCKET) continue;
128 
129         if (cmsg->cmsg_type == SCM_TIMESTAMP) {
130             memcpy(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts));
131         }
132     }
133 
134     bthdr->adapter_id = htons(hdr.index);
135     bthdr->opcode = htons(hdr.opcode);
136 
137     if (handle->fcode.bf_insns == NULL ||
138         bpf_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
139         callback(user, &pkth, pktd);
140         return 1;
141     }
142     return 0;   /* didn't pass filter */
143 }
144 
145 static int
146 bt_monitor_inject(pcap_t *handle, const void *buf _U_, size_t size _U_)
147 {
148     pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported yet");
149     return -1;
150 }
151 
152 static int
153 bt_monitor_setdirection(pcap_t *p, pcap_direction_t d)
154 {
155     p->direction = d;
156     return 0;
157 }
158 
159 static int
160 bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats)
161 {
162     stats->ps_recv = 0;
163     stats->ps_drop = 0;
164     stats->ps_ifdrop = 0;
165 
166     return 0;
167 }
168 
169 static int
170 bt_monitor_activate(pcap_t* handle)
171 {
172     struct sockaddr_hci addr;
173     int err = PCAP_ERROR;
174     int opt;
175 
176     if (handle->opt.rfmon) {
177         /* monitor mode doesn't apply here */
178         return PCAP_ERROR_RFMON_NOTSUP;
179     }
180 
181     handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot;
182     handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR;
183 
184     handle->read_op = bt_monitor_read;
185     handle->inject_op = bt_monitor_inject;
186     handle->setfilter_op = install_bpf_program; /* no kernel filtering */
187     handle->setdirection_op = bt_monitor_setdirection;
188     handle->set_datalink_op = NULL; /* can't change data link type */
189     handle->getnonblock_op = pcap_getnonblock_fd;
190     handle->setnonblock_op = pcap_setnonblock_fd;
191     handle->stats_op = bt_monitor_stats;
192 
193     handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
194     if (handle->fd < 0) {
195         pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
196             "Can't create raw socket: %s", strerror(errno));
197         return PCAP_ERROR;
198     }
199 
200     handle->buffer = malloc(handle->bufsize);
201     if (!handle->buffer) {
202         pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
203             pcap_strerror(errno));
204         goto close_fail;
205     }
206 
207     /* Bind socket to the HCI device */
208     addr.hci_family = AF_BLUETOOTH;
209     addr.hci_dev = HCI_DEV_NONE;
210     addr.hci_channel = HCI_CHANNEL_MONITOR;
211 
212     if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
213         pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
214             "Can't attach to interface: %s", strerror(errno));
215         goto close_fail;
216     }
217 
218     opt = 1;
219     if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
220         pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
221             "Can't enable time stamp: %s", strerror(errno));
222         goto close_fail;
223     }
224 
225     handle->selectable_fd = handle->fd;
226 
227     return 0;
228 
229 close_fail:
230     pcap_cleanup_live_common(handle);
231     return err;
232 }
233 
234 pcap_t *
235 bt_monitor_create(const char *device, char *ebuf, int *is_ours)
236 {
237     pcap_t      *p;
238     const char  *cp;
239 
240     cp = strrchr(device, '/');
241     if (cp == NULL)
242         cp = device;
243 
244     if (strcmp(cp, INTERFACE_NAME) != 0) {
245         *is_ours = 0;
246         return NULL;
247     }
248 
249     *is_ours = 1;
250     p = pcap_create_common(ebuf, 0);
251     if (p == NULL)
252         return NULL;
253 
254     p->activate_op = bt_monitor_activate;
255 
256     return p;
257 }
258