libpcap  1.10.1
About: libpcap is a packet filter library used by tools like tcpdump.
  Fossies Dox: libpcap-1.10.1.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pcap-bt-monitor-linux.c
Go to the documentation of this file.
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  */
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
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,
85  "Bluetooth Linux Monitor", err_str) == NULL)
86  {
87  ret = -1;
88  }
89 
90  return ret;
91 }
92 
93 static int
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;
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  if (errno == EAGAIN || errno == EWOULDBLOCK) {
131  /* Nonblocking mode, no data */
132  return 0;
133  }
135  errno, "Can't receive packet");
136  return -1;
137  }
138 
139  pkth.caplen = (bpf_u_int32)(ret - sizeof(hdr) + sizeof(pcap_bluetooth_linux_monitor_header));
140  pkth.len = pkth.caplen;
141 
142  for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
143  if (cmsg->cmsg_level != SOL_SOCKET) continue;
144 
145  if (cmsg->cmsg_type == SCM_TIMESTAMP) {
146  memcpy(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts));
147  }
148  }
149 
150  bthdr->adapter_id = htons(hdr.index);
151  bthdr->opcode = htons(hdr.opcode);
152 
153  if (handle->fcode.bf_insns == NULL ||
154  pcap_filter(handle->fcode.bf_insns, pktd, pkth.len, pkth.caplen)) {
155  callback(user, &pkth, pktd);
156  return 1;
157  }
158  return 0; /* didn't pass filter */
159 }
160 
161 static int
162 bt_monitor_inject(pcap_t *handle, const void *buf _U_, int size _U_)
163 {
165  "Packet injection is not supported yet on Bluetooth monitor devices");
166  return -1;
167 }
168 
169 static int
170 bt_monitor_stats(pcap_t *handle _U_, struct pcap_stat *stats)
171 {
172  stats->ps_recv = 0;
173  stats->ps_drop = 0;
174  stats->ps_ifdrop = 0;
175 
176  return 0;
177 }
178 
179 static int
181 {
182  struct sockaddr_hci addr;
183  int err = PCAP_ERROR;
184  int opt;
185 
186  if (handle->opt.rfmon) {
187  /* monitor mode doesn't apply here */
189  }
190 
191  /*
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.
195  *
196  * If some application really *needs* a bigger snapshot
197  * length, we should just increase MAXIMUM_SNAPLEN.
198  */
199  if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
200  handle->snapshot = MAXIMUM_SNAPLEN;
201 
204 
205  handle->read_op = bt_monitor_read;
206  handle->inject_op = bt_monitor_inject;
207  handle->setfilter_op = 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 */
212  handle->stats_op = bt_monitor_stats;
213 
214  handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
215  if (handle->fd < 0) {
217  errno, "Can't create raw socket");
218  return PCAP_ERROR;
219  }
220 
221  handle->buffer = malloc(handle->bufsize);
222  if (!handle->buffer) {
224  errno, "Can't allocate dump buffer");
225  goto close_fail;
226  }
227 
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;
232 
233  if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
235  errno, "Can't attach to interface");
236  goto close_fail;
237  }
238 
239  opt = 1;
240  if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
242  errno, "Can't enable time stamp");
243  goto close_fail;
244  }
245 
246  handle->selectable_fd = handle->fd;
247 
248  return 0;
249 
250 close_fail:
251  pcap_cleanup_live_common(handle);
252  return err;
253 }
254 
255 pcap_t *
256 bt_monitor_create(const char *device, char *ebuf, int *is_ours)
257 {
258  pcap_t *p;
259  const char *cp;
260 
261  cp = strrchr(device, '/');
262  if (cp == NULL)
263  cp = device;
264 
265  if (strcmp(cp, INTERFACE_NAME) != 0) {
266  *is_ours = 0;
267  return NULL;
268  }
269 
270  *is_ours = 1;
271  p = PCAP_CREATE_COMMON(ebuf, struct pcap_bt_monitor);
272  if (p == NULL)
273  return NULL;
274 
276 
277  return p;
278 }
struct _pcap_bluetooth_linux_monitor_header pcap_bluetooth_linux_monitor_header
u_int bpf_u_int32
Definition: bpf.h:98
u_int pcap_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen, u_int buflen)
Definition: bpf_filter.c:391
#define DLT_BLUETOOTH_LINUX_MONITOR
Definition: dlt.h:1237
void pcap_fmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum, const char *fmt,...)
Definition: fmtutils.c:269
int install_bpf_program(pcap_t *p, struct bpf_program *fp)
Definition: optimize.c:2939
int snprintf(char *, size_t, const char *,...)
int setsockopt(int, int, int, char *, int)
int socket(int, int, int)
int bind(int, struct sockaddr *, int)
pcap_t * bt_monitor_create(const char *device, char *ebuf, int *is_ours)
static int bt_monitor_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
static int bt_monitor_inject(pcap_t *handle, const void *buf, int size)
static int bt_monitor_activate(pcap_t *handle)
static int bt_monitor_stats(pcap_t *handle, struct pcap_stat *stats)
#define BT_CONTROL_SIZE
#define INTERFACE_NAME
int bt_monitor_findalldevs(pcap_if_list_t *devlistp, char *err_str)
#define _U_
Definition: pcap-dos.h:93
int errno
#define MAXIMUM_SNAPLEN
Definition: pcap-int.h:131
pcap_if_t * add_dev(pcap_if_list_t *, const char *, bpf_u_int32, const char *, char *)
Definition: pcap.c:1308
void pcap_cleanup_live_common(pcap_t *)
Definition: pcap.c:3987
int pcap_getnonblock_fd(pcap_t *)
Definition: pcap.c:3537
int pcap_setnonblock_fd(pcap_t *p, int)
Definition: pcap.c:3583
#define PCAP_CREATE_COMMON(ebuf, type)
Definition: pcap-int.h:474
#define PCAP_ERROR_RFMON_NOTSUP
Definition: pcap.h:344
#define PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
Definition: pcap.h:317
void(* pcap_handler)(u_char *, const struct pcap_pkthdr *, const u_char *)
Definition: pcap.h:330
#define PCAP_IF_WIRELESS
Definition: pcap.h:312
#define PCAP_ERRBUF_SIZE
Definition: pcap.h:152
#define PCAP_ERROR
Definition: pcap.h:339
struct bpf_insn * bf_insns
Definition: bpf.h:119
int rfmon
Definition: pcap-int.h:150
bpf_u_int32 caplen
Definition: pcap.h:247
struct timeval ts
Definition: pcap.h:246
bpf_u_int32 len
Definition: pcap.h:248
u_int ps_drop
Definition: pcap.h:256
u_int ps_recv
Definition: pcap.h:255
u_int ps_ifdrop
Definition: pcap.h:257
Definition: pcap-int.h:200
stats_op_t stats_op
Definition: pcap-int.h:320
activate_op_t activate_op
Definition: pcap-int.h:311
setnonblock_op_t setnonblock_op
Definition: pcap-int.h:319
setfilter_op_t setfilter_op
Definition: pcap-int.h:315
sig_atomic_t break_loop
Definition: pcap-int.h:225
u_int bufsize
Definition: pcap-int.h:220
getnonblock_op_t getnonblock_op
Definition: pcap-int.h:318
read_op_t read_op
Definition: pcap-int.h:204
void * buffer
Definition: pcap-int.h:221
int snapshot
Definition: pcap-int.h:247
setdirection_op_t setdirection_op
Definition: pcap-int.h:316
inject_op_t inject_op
Definition: pcap-int.h:313
struct bpf_program fcode
Definition: pcap-int.h:293
set_datalink_op_t set_datalink_op
Definition: pcap-int.h:317
char errbuf[256+1]
Definition: pcap-int.h:295
int linktype
Definition: pcap-int.h:248
int selectable_fd
Definition: pcap-int.h:274
int fd
Definition: pcap-int.h:214
struct pcap_opt opt
Definition: pcap-int.h:254