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