• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include <bluetooth/bluetooth.h>
42 #include <bluetooth/hci.h>
43 
44 #include "pcap/bluetooth.h"
45 #include "pcap-int.h"
46 
47 #include "pcap-bt-monitor-linux.h"
48 
49 #define BT_CONTROL_SIZE 32
50 #define INTERFACE_NAME "bluetooth-monitor"
51 
52 /*
53  * Private data.
54  * Currently contains nothing.
55  */
56 struct pcap_bt_monitor {
57 	int	dummy;
58 };
59 
60 /*
61  * Fields and alignment must match the declaration in the Linux kernel 3.4+.
62  * See struct hci_mon_hdr in include/net/bluetooth/hci_mon.h.
63  */
64 struct hci_mon_hdr {
65     uint16_t opcode;
66     uint16_t index;
67     uint16_t len;
68 } __attribute__((packed));
69 
70 int
bt_monitor_findalldevs(pcap_if_list_t * devlistp,char * err_str)71 bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str)
72 {
73     int         ret = 0;
74 
75     /*
76      * Bluetooth is a wireless technology.
77      *
78      * This is a device to monitor all Bluetooth interfaces, so
79      * there's no notion of "connected" or "disconnected", any
80      * more than there's a notion of "connected" or "disconnected"
81      * for the "any" device.
82      */
83     if (add_dev(devlistp, INTERFACE_NAME,
84                 PCAP_IF_WIRELESS|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
85                 "Bluetooth Linux Monitor", err_str) == NULL)
86     {
87         ret = -1;
88     }
89 
90     return ret;
91 }
92 
93 static int
bt_monitor_read(pcap_t * handle,int max_packets _U_,pcap_handler callback,u_char * user)94 bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
95 {
96     struct cmsghdr *cmsg;
97     struct msghdr msg;
98     struct iovec  iv[2];
99     ssize_t ret;
100     struct pcap_pkthdr pkth;
101     pcap_bluetooth_linux_monitor_header *bthdr;
102     u_char *pktd;
103     struct hci_mon_hdr hdr;
104 
105     pktd = (u_char *)handle->buffer + BT_CONTROL_SIZE;
106     bthdr = (pcap_bluetooth_linux_monitor_header*)(void *)pktd;
107 
108     iv[0].iov_base = &hdr;
109     iv[0].iov_len = sizeof(hdr);
110     iv[1].iov_base = pktd + sizeof(pcap_bluetooth_linux_monitor_header);
111     iv[1].iov_len = handle->snapshot;
112 
113     memset(&pkth.ts, 0, sizeof(pkth.ts));
114     memset(&msg, 0, sizeof(msg));
115     msg.msg_iov = iv;
116     msg.msg_iovlen = 2;
117     msg.msg_control = handle->buffer;
118     msg.msg_controllen = BT_CONTROL_SIZE;
119 
120     do {
121         ret = recvmsg(handle->fd, &msg, 0);
122         if (handle->break_loop)
123         {
124             handle->break_loop = 0;
125             return -2;
126         }
127     } while ((ret == -1) && (errno == EINTR));
128 
129     if (ret < 0) {
130         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
131             errno, "Can't receive packet");
132         return -1;
133     }
134 
135     pkth.caplen = (bpf_u_int32)(ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header));
136     pkth.len = pkth.caplen;
137 
138     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
139         if (cmsg->cmsg_level != SOL_SOCKET) continue;
140 
141         if (cmsg->cmsg_type == SCM_TIMESTAMP) {
142             memcpy(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts));
143         }
144     }
145 
146     bthdr->adapter_id = htons(hdr.index);
147     bthdr->opcode = htons(hdr.opcode);
148 
149     if (handle->fcode.bf_insns == NULL ||
150         pcap_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
151         callback(user, &pkth, pktd);
152         return 1;
153     }
154     return 0;   /* didn't pass filter */
155 }
156 
157 static int
bt_monitor_inject(pcap_t * handle,const void * buf _U_,int size _U_)158 bt_monitor_inject(pcap_t *handle, const void *buf _U_, int size _U_)
159 {
160     snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
161         "Packet injection is not supported yet on Bluetooth monitor devices");
162     return -1;
163 }
164 
165 static int
bt_monitor_stats(pcap_t * handle _U_,struct pcap_stat * stats)166 bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats)
167 {
168     stats->ps_recv = 0;
169     stats->ps_drop = 0;
170     stats->ps_ifdrop = 0;
171 
172     return 0;
173 }
174 
175 static int
bt_monitor_activate(pcap_t * handle)176 bt_monitor_activate(pcap_t* handle)
177 {
178     struct sockaddr_hci addr;
179     int err = PCAP_ERROR;
180     int opt;
181 
182     if (handle->opt.rfmon) {
183         /* monitor mode doesn't apply here */
184         return PCAP_ERROR_RFMON_NOTSUP;
185     }
186 
187     /*
188      * Turn a negative snapshot value (invalid), a snapshot value of
189      * 0 (unspecified), or a value bigger than the normal maximum
190      * value, into the maximum allowed value.
191      *
192      * If some application really *needs* a bigger snapshot
193      * length, we should just increase MAXIMUM_SNAPLEN.
194      */
195     if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
196         handle->snapshot = MAXIMUM_SNAPLEN;
197 
198     handle->bufsize = BT_CONTROL_SIZE + sizeof(pcap_bluetooth_linux_monitor_header) + handle->snapshot;
199     handle->linktype = DLT_BLUETOOTH_LINUX_MONITOR;
200 
201     handle->read_op = bt_monitor_read;
202     handle->inject_op = bt_monitor_inject;
203     handle->setfilter_op = install_bpf_program; /* no kernel filtering */
204     handle->setdirection_op = NULL; /* Not implemented */
205     handle->set_datalink_op = NULL; /* can't change data link type */
206     handle->getnonblock_op = pcap_getnonblock_fd;
207     handle->setnonblock_op = pcap_setnonblock_fd;
208     handle->stats_op = bt_monitor_stats;
209 
210     handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
211     if (handle->fd < 0) {
212         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
213             errno, "Can't create raw socket");
214         return PCAP_ERROR;
215     }
216 
217     handle->buffer = malloc(handle->bufsize);
218     if (!handle->buffer) {
219         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
220             errno, "Can't allocate dump buffer");
221         goto close_fail;
222     }
223 
224     /* Bind socket to the HCI device */
225     addr.hci_family = AF_BLUETOOTH;
226     addr.hci_dev = HCI_DEV_NONE;
227     addr.hci_channel = HCI_CHANNEL_MONITOR;
228 
229     if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
230         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
231             errno, "Can't attach to interface");
232         goto close_fail;
233     }
234 
235     opt = 1;
236     if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
237         pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
238             errno, "Can't enable time stamp");
239         goto close_fail;
240     }
241 
242     handle->selectable_fd = handle->fd;
243 
244     return 0;
245 
246 close_fail:
247     pcap_cleanup_live_common(handle);
248     return err;
249 }
250 
251 pcap_t *
bt_monitor_create(const char * device,char * ebuf,int * is_ours)252 bt_monitor_create(const char *device, char *ebuf, int *is_ours)
253 {
254     pcap_t      *p;
255     const char  *cp;
256 
257     cp = strrchr(device, '/');
258     if (cp == NULL)
259         cp = device;
260 
261     if (strcmp(cp, INTERFACE_NAME) != 0) {
262         *is_ours = 0;
263         return NULL;
264     }
265 
266     *is_ours = 1;
267     p = PCAP_CREATE_COMMON(ebuf, struct pcap_bt_monitor);
268     if (p == NULL)
269         return NULL;
270 
271     p->activate_op = bt_monitor_activate;
272 
273     return p;
274 }
275