xref: /freebsd/contrib/libpcap/pcap-bt-monitor-linux.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /*
2  * Copyright (c) 2014 Michal Labedzki for Tieto Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include <bluetooth/bluetooth.h>
41 #include <bluetooth/hci.h>
42 #include <bluetooth/mgmt.h>
43 
44 #include "pcap/bluetooth.h"
45 #include "pcap-int.h"
46 
47 #define BT_CONTROL_SIZE 32
48 #define INTERFACE_NAME "bluetooth-monitor"
49 
50 int
51 bt_monitor_findalldevs(pcap_if_t **alldevsp, char *err_str)
52 {
53     int         ret = 0;
54 
55     if (pcap_add_if(alldevsp, INTERFACE_NAME, 0,
56                "Bluetooth Linux Monitor", err_str) < 0)
57     {
58         ret = -1;
59     }
60 
61     return ret;
62 }
63 
64 static int
65 bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
66 {
67     struct cmsghdr *cmsg;
68     struct msghdr msg;
69     struct iovec  iv[2];
70     ssize_t ret;
71     struct pcap_pkthdr pkth;
72     pcap_bluetooth_linux_monitor_header *bthdr;
73     struct mgmt_hdr hdr;
74     int in = 0;
75 
76     bthdr = (pcap_bluetooth_linux_monitor_header*) &handle->buffer[handle->offset];
77 
78     iv[0].iov_base = &hdr;
79     iv[0].iov_len = MGMT_HDR_SIZE;
80     iv[1].iov_base = &handle->buffer[handle->offset + sizeof(pcap_bluetooth_linux_monitor_header)];
81     iv[1].iov_len = handle->snapshot;
82 
83     memset(&pkth.ts, 0, sizeof(pkth.ts));
84     memset(&msg, 0, sizeof(msg));
85     msg.msg_iov = iv;
86     msg.msg_iovlen = 2;
87     msg.msg_control = handle->buffer;
88     msg.msg_controllen = handle->offset;
89 
90     do {
91         ret = recvmsg(handle->fd, &msg, 0);
92         if (handle->break_loop)
93         {
94             handle->break_loop = 0;
95             return -2;
96         }
97     } while ((ret == -1) && (errno == EINTR));
98 
99     if (ret < 0) {
100         snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
101             "Can't receive packet: %s", strerror(errno));
102         return -1;
103     }
104 
105     pkth.caplen = ret - MGMT_HDR_SIZE + sizeof(pcap_bluetooth_linux_monitor_header);
106     pkth.len = pkth.caplen;
107 
108     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
109         if (cmsg->cmsg_level != SOL_SOCKET) continue;
110 
111         if (cmsg->cmsg_type == SCM_TIMESTAMP) {
112             memcpy(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts));
113         }
114     }
115 
116     bthdr->adapter_id = htons(hdr.index);
117     bthdr->opcode = htons(hdr.opcode);
118 
119     if (handle->fcode.bf_insns == NULL ||
120         bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset],
121           pkth.len, pkth.caplen)) {
122         callback(user, &pkth, &handle->buffer[handle->offset]);
123         return 1;
124     }
125     return 0;   /* didn't pass filter */
126 }
127 
128 static int
129 bt_monitor_inject(pcap_t *handle, const void *buf _U_, size_t size _U_)
130 {
131     snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported yet");
132     return -1;
133 }
134 
135 static int
136 bt_monitor_setdirection(pcap_t *p, pcap_direction_t d)
137 {
138     p->direction = d;
139     return 0;
140 }
141 
142 static int
143 bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats)
144 {
145     stats->ps_recv = 0;
146     stats->ps_drop = 0;
147     stats->ps_ifdrop = 0;
148 
149     return 0;
150 }
151 
152 static int
153 bt_monitor_activate(pcap_t* handle)
154 {
155     struct sockaddr_hci addr;
156     int err = PCAP_ERROR;
157     int opt;
158 
159     if (handle->opt.rfmon) {
160         /* monitor mode doesn't apply here */
161         return PCAP_ERROR_RFMON_NOTSUP;
162     }
163 
164     handle->bufsize = handle->snapshot + BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header);
165     handle->offset = BT_CONTROL_SIZE;
166     handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR;
167 
168     handle->read_op = bt_monitor_read;
169     handle->inject_op = bt_monitor_inject;
170     handle->setfilter_op = install_bpf_program; /* no kernel filtering */
171     handle->setdirection_op = bt_monitor_setdirection;
172     handle->set_datalink_op = NULL; /* can't change data link type */
173     handle->getnonblock_op = pcap_getnonblock_fd;
174     handle->setnonblock_op = pcap_setnonblock_fd;
175     handle->stats_op = bt_monitor_stats;
176 
177     handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
178     if (handle->fd < 0) {
179         snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
180             "Can't create raw socket: %s", strerror(errno));
181         return PCAP_ERROR;
182     }
183 
184     handle->buffer = malloc(handle->bufsize);
185     if (!handle->buffer) {
186         snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
187             pcap_strerror(errno));
188         goto close_fail;
189     }
190 
191     /* Bind socket to the HCI device */
192     addr.hci_family = AF_BLUETOOTH;
193     addr.hci_dev = HCI_DEV_NONE;
194     addr.hci_channel = HCI_CHANNEL_MONITOR;
195 
196     if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
197         snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
198             "Can't attach to interface: %s", strerror(errno));
199         goto close_fail;
200     }
201 
202     opt = 1;
203     if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
204         snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
205             "Can't enable time stamp: %s", strerror(errno));
206         goto close_fail;
207     }
208 
209     handle->selectable_fd = handle->fd;
210 
211     return 0;
212 
213 close_fail:
214     pcap_cleanup_live_common(handle);
215     return err;
216 }
217 
218 pcap_t *
219 bt_monitor_create(const char *device, char *ebuf, int *is_ours)
220 {
221     pcap_t      *p;
222     const char  *cp;
223 
224     cp = strrchr(device, '/');
225     if (cp == NULL)
226         cp = device;
227 
228     if (strcmp(cp, INTERFACE_NAME) != 0) {
229         *is_ours = 0;
230         return NULL;
231     }
232 
233     *is_ours = 1;
234     p = pcap_create_common(device, ebuf, 0);
235     if (p == NULL)
236         return NULL;
237 
238     p->activate_op = bt_monitor_activate;
239 
240     return p;
241 }
242