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