2 * Copyright (c) 2014 Michal Labedzki for Tieto Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
41 #include <bluetooth/bluetooth.h>
42 #include <bluetooth/hci.h>
44 #include "pcap/bluetooth.h"
47 #include "pcap-bt-monitor-linux.h"
49 #define BT_CONTROL_SIZE 32
50 #define INTERFACE_NAME "bluetooth-monitor"
54 * Currently contains nothing.
56 struct pcap_bt_monitor
{
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.
68 } __attribute__((packed
));
71 bt_monitor_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
76 * Bluetooth is a wireless technology.
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.
83 if (pcap_add_dev(devlistp
, INTERFACE_NAME
,
84 PCAP_IF_WIRELESS
|PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
,
85 "Bluetooth Linux Monitor", err_str
) == NULL
)
94 bt_monitor_read(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
100 struct pcap_pkthdr pkth
;
101 pcap_bluetooth_linux_monitor_header
*bthdr
;
103 struct hci_mon_hdr hdr
;
105 pktd
= handle
->buffer
+ BT_CONTROL_SIZE
;
106 bthdr
= (pcap_bluetooth_linux_monitor_header
*)(void *)pktd
;
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
;
113 memset(&pkth
.ts
, 0, sizeof(pkth
.ts
));
114 memset(&msg
, 0, sizeof(msg
));
117 msg
.msg_control
= handle
->buffer
;
118 msg
.msg_controllen
= BT_CONTROL_SIZE
;
121 ret
= recvmsg(handle
->fd
, &msg
, 0);
122 if (handle
->break_loop
)
124 handle
->break_loop
= 0;
127 } while ((ret
== -1) && (errno
== EINTR
));
130 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
131 /* Nonblocking mode, no data */
134 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
135 errno
, "Can't receive packet");
139 pkth
.caplen
= (bpf_u_int32
)(ret
- sizeof(hdr
) + sizeof(pcap_bluetooth_linux_monitor_header
));
140 pkth
.len
= pkth
.caplen
;
142 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
!= NULL
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
143 if (cmsg
->cmsg_level
!= SOL_SOCKET
) continue;
145 if (cmsg
->cmsg_type
== SCM_TIMESTAMP
) {
146 memcpy(&pkth
.ts
, CMSG_DATA(cmsg
), sizeof(pkth
.ts
));
150 bthdr
->adapter_id
= htons(hdr
.index
);
151 bthdr
->opcode
= htons(hdr
.opcode
);
153 if (handle
->fcode
.bf_insns
== NULL
||
154 pcap_filter(handle
->fcode
.bf_insns
, pktd
, pkth
.len
, pkth
.caplen
)) {
155 callback(user
, &pkth
, pktd
);
158 return 0; /* didn't pass filter */
162 bt_monitor_inject(pcap_t
*handle
, const void *buf _U_
, int size _U_
)
164 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
165 "Packet injection is not supported yet on Bluetooth monitor devices");
170 bt_monitor_stats(pcap_t
*handle _U_
, struct pcap_stat
*stats
)
174 stats
->ps_ifdrop
= 0;
180 bt_monitor_activate(pcap_t
* handle
)
182 struct sockaddr_hci addr
;
183 int err
= PCAP_ERROR
;
186 if (handle
->opt
.rfmon
) {
187 /* monitor mode doesn't apply here */
188 return PCAP_ERROR_RFMON_NOTSUP
;
192 * Turn a negative snapshot value (invalid), a snapshot value of
193 * 0 (unspecified), or a value bigger than the normal maximum
194 * value, into the maximum allowed value.
196 * If some application really *needs* a bigger snapshot
197 * length, we should just increase MAXIMUM_SNAPLEN.
199 if (handle
->snapshot
<= 0 || handle
->snapshot
> MAXIMUM_SNAPLEN
)
200 handle
->snapshot
= MAXIMUM_SNAPLEN
;
202 handle
->bufsize
= BT_CONTROL_SIZE
+ sizeof(pcap_bluetooth_linux_monitor_header
) + handle
->snapshot
;
203 handle
->linktype
= DLT_BLUETOOTH_LINUX_MONITOR
;
205 handle
->read_op
= bt_monitor_read
;
206 handle
->inject_op
= bt_monitor_inject
;
207 handle
->setfilter_op
= pcap_install_bpf_program
; /* no kernel filtering */
208 handle
->setdirection_op
= NULL
; /* Not implemented */
209 handle
->set_datalink_op
= NULL
; /* can't change data link type */
210 handle
->getnonblock_op
= pcap_getnonblock_fd
;
211 handle
->setnonblock_op
= pcap_setnonblock_fd
;
212 handle
->stats_op
= bt_monitor_stats
;
214 handle
->fd
= socket(AF_BLUETOOTH
, SOCK_RAW
, BTPROTO_HCI
);
215 if (handle
->fd
< 0) {
216 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
217 errno
, "Can't create raw socket");
221 handle
->buffer
= malloc(handle
->bufsize
);
222 if (!handle
->buffer
) {
223 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
224 errno
, "Can't allocate dump buffer");
228 /* Bind socket to the HCI device */
229 addr
.hci_family
= AF_BLUETOOTH
;
230 addr
.hci_dev
= HCI_DEV_NONE
;
231 addr
.hci_channel
= HCI_CHANNEL_MONITOR
;
233 if (bind(handle
->fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
234 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
235 errno
, "Can't attach to interface");
240 if (setsockopt(handle
->fd
, SOL_SOCKET
, SO_TIMESTAMP
, &opt
, sizeof(opt
)) < 0) {
241 pcap_fmt_errmsg_for_errno(handle
->errbuf
, PCAP_ERRBUF_SIZE
,
242 errno
, "Can't enable time stamp");
246 handle
->selectable_fd
= handle
->fd
;
251 pcap_cleanup_live_common(handle
);
256 bt_monitor_create(const char *device
, char *ebuf
, int *is_ours
)
261 cp
= strrchr(device
, '/');
265 if (strcmp(cp
, INTERFACE_NAME
) != 0) {
271 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_bt_monitor
);
275 p
->activate_op
= bt_monitor_activate
;