bluez/android/bluetoothd-snoop.c
Tedd Ho-Jeong An 44b4662529 android: Add SPDX License Identifier
This patch adds SPDX License Identifier and removes the license text.

-------------------------------------
       License            COUNT
-------------------------------------
 Apache-2.0           :     80
 LGPL-2.1-or-later    :     42
 GPL-2.0-or-later     :      9

License: Apache-2.0
   android/bluetoothd-wrapper.c
   android/tester-hidhost.c
   android/tester-map-client.c
   android/hal-audio-aptx.c
   android/hal-audio.c
   android/hal-handsfree-client.c
   android/tester-gatt.c
   android/hal-pan.c
   android/hal-socket.c
   android/tester-hdp.c
   android/tester-pan.c
   android/tester-avrcp.c
   android/hal-a2dp-sink.c
   android/hal-gatt.c
   android/hal-avrcp.c
   android/tester-a2dp.c
   android/hal-audio.h
   android/hal-bluetooth.c
   android/tester-socket.c
   android/hal-health.c
   android/hal-a2dp.c
   android/hal-ipc.c
   android/hal-avrcp-ctrl.c
   android/hal-sco.c
   android/hal-ipc.h
   android/tester-main.c
   android/hal-audio-sbc.c
   android/hal-utils.c
   android/hal-map-client.c
   android/hal-handsfree.c
   android/hal-log.h
   android/hal.h
   android/hal-utils.h
   android/tester-bluetooth.c
   android/hal-hidhost.c
   android/audio_utils/resampler.h
   android/audio_utils/resampler.c
   android/system/audio.h
   android/hardware/bt_sock.h
   android/hardware/bt_gatt_client.h
   android/hardware/bt_hh.h
   android/hardware/bluetooth.h
   android/hardware/hardware.h
   android/hardware/bt_hf_client.h
   android/hardware/bt_rc.h
   android/hardware/bt_hf.h
   android/hardware/bt_pan.h
   android/hardware/bt_gatt_types.h
   android/hardware/hardware.c
   android/hardware/audio_effect.h
   android/hardware/audio.h
   android/hardware/bt_gatt_server.h
   android/hardware/bt_gatt.h
   android/hardware/bt_hl.h
   android/hardware/bt_mce.h
   android/hardware/bt_av.h
   android/client/if-hh.c
   android/client/if-sco.c
   android/client/pollhandler.h
   android/client/haltest.c
   android/client/tabcompletion.c
   android/client/if-hf-client.c
   android/client/history.h
   android/client/if-audio.c
   android/client/terminal.h
   android/client/if-av.c
   android/client/if-main.h
   android/client/if-av-sink.c
   android/client/terminal.c
   android/client/if-mce.c
   android/client/if-hf.c
   android/client/pollhandler.c
   android/client/if-sock.c
   android/client/if-rc-ctrl.c
   android/client/if-rc.c
   android/client/history.c
   android/client/if-pan.c
   android/client/if-gatt.c
   android/client/if-bt.c
   android/client/if-hl.c

License: LGPL-2.1-or-later
   android/sco.h
   android/socket.c
   android/tester-main.h
   android/hidhost.h
   android/map-client.h
   android/utils.h
   android/hal-msg.h
   android/health.h
   android/avrcp-lib.c
   android/bluetoothd-snoop.c
   android/a2dp.c
   android/bluetooth.h
   android/a2dp-sink.c
   android/handsfree-client.c
   android/audio-msg.h
   android/main.c
   android/pan.c
   android/ipc-tester.c
   android/health.c
   android/socket.h
   android/handsfree.h
   android/avrcp-lib.h
   android/a2dp-sink.h
   android/system-emulator.c
   android/gatt.c
   android/avrcp.h
   android/pan.h
   android/test-ipc.c
   android/hidhost.c
   android/a2dp.h
   android/avrcp.c
   android/sco-msg.h
   android/bluetooth.c
   android/map-client.c
   android/handsfree-client.h
   android/ipc.c
   android/handsfree.c
   android/gatt.h
   android/ipc.h
   android/ipc-common.h
   android/compat/wordexp.h
   android/cutils/properties.h

License: GPL-2.0-or-later
   android/avdtp.h
   android/avctp.c
   android/avdtptest.c
   android/sco.c
   android/log.c
   android/avdtp.c
   android/avctp.h
   android/compat/readline/history.h
   android/compat/readline/readline.h
2020-09-21 16:19:33 -07:00

243 lines
4.6 KiB
C

// SPDX-License-Identifier: LGPL-2.1-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#if defined(ANDROID)
#include <sys/capability.h>
#endif
#include "lib/bluetooth.h"
#include "lib/hci.h"
#include "lib/mgmt.h"
#include "src/shared/mainloop.h"
#include "src/shared/btsnoop.h"
#include "src/log.h"
#define DEFAULT_SNOOP_FILE "/sdcard/btsnoop_hci.log"
static struct btsnoop *snoop = NULL;
static uint8_t monitor_buf[BTSNOOP_MAX_PACKET_SIZE];
static int monitor_fd = -1;
static void signal_callback(int signum, void *user_data)
{
switch (signum) {
case SIGINT:
case SIGTERM:
mainloop_quit();
break;
}
}
static uint32_t get_flags_from_opcode(uint16_t opcode)
{
switch (opcode) {
case BTSNOOP_OPCODE_NEW_INDEX:
case BTSNOOP_OPCODE_DEL_INDEX:
break;
case BTSNOOP_OPCODE_COMMAND_PKT:
return 0x02;
case BTSNOOP_OPCODE_EVENT_PKT:
return 0x03;
case BTSNOOP_OPCODE_ACL_TX_PKT:
return 0x00;
case BTSNOOP_OPCODE_ACL_RX_PKT:
return 0x01;
case BTSNOOP_OPCODE_SCO_TX_PKT:
case BTSNOOP_OPCODE_SCO_RX_PKT:
break;
case BTSNOOP_OPCODE_OPEN_INDEX:
case BTSNOOP_OPCODE_CLOSE_INDEX:
break;
}
return 0xff;
}
static void data_callback(int fd, uint32_t events, void *user_data)
{
unsigned char control[32];
struct mgmt_hdr hdr;
struct msghdr msg;
struct iovec iov[2];
if (events & (EPOLLERR | EPOLLHUP)) {
mainloop_remove_fd(monitor_fd);
return;
}
iov[0].iov_base = &hdr;
iov[0].iov_len = MGMT_HDR_SIZE;
iov[1].iov_base = monitor_buf;
iov[1].iov_len = sizeof(monitor_buf);
memset(&msg, 0, sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 2;
msg.msg_control = control;
msg.msg_controllen = sizeof(control);
while (true) {
struct cmsghdr *cmsg;
struct timeval *tv = NULL;
struct timeval ctv;
uint16_t opcode, index, pktlen;
uint32_t flags;
ssize_t len;
len = recvmsg(monitor_fd, &msg, MSG_DONTWAIT);
if (len < 0)
break;
if (len < MGMT_HDR_SIZE)
break;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET)
continue;
if (cmsg->cmsg_type == SCM_TIMESTAMP) {
memcpy(&ctv, CMSG_DATA(cmsg), sizeof(ctv));
tv = &ctv;
}
}
opcode = btohs(hdr.opcode);
index = btohs(hdr.index);
pktlen = btohs(hdr.len);
if (index)
continue;
flags = get_flags_from_opcode(opcode);
if (flags != 0xff)
btsnoop_write(snoop, tv, flags, 0, monitor_buf, pktlen);
}
}
static int open_monitor(const char *path)
{
struct sockaddr_hci addr;
int opt = 1;
snoop = btsnoop_create(path, 0, 0, BTSNOOP_FORMAT_HCI);
if (!snoop)
return -1;
monitor_fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
if (monitor_fd < 0)
goto failed;
memset(&addr, 0, sizeof(addr));
addr.hci_family = AF_BLUETOOTH;
addr.hci_dev = HCI_DEV_NONE;
addr.hci_channel = HCI_CHANNEL_MONITOR;
if (bind(monitor_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
goto failed_close;
if (setsockopt(monitor_fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))
< 0)
goto failed_close;
mainloop_add_fd(monitor_fd, EPOLLIN, data_callback, NULL, NULL);
return 0;
failed_close:
close(monitor_fd);
monitor_fd = -1;
failed:
btsnoop_unref(snoop);
snoop = NULL;
return -1;
}
static void close_monitor(void)
{
btsnoop_unref(snoop);
snoop = NULL;
close(monitor_fd);
monitor_fd = -1;
}
static void set_capabilities(void)
{
#if defined(ANDROID)
struct __user_cap_header_struct header;
struct __user_cap_data_struct cap;
header.version = _LINUX_CAPABILITY_VERSION;
header.pid = 0;
/*
* CAP_NET_RAW: for snooping
* CAP_DAC_READ_SEARCH: override path search permissions
*/
cap.effective = cap.permitted =
CAP_TO_MASK(CAP_NET_RAW) |
CAP_TO_MASK(CAP_DAC_READ_SEARCH);
cap.inheritable = 0;
/* TODO: Move to cap_set_proc once bionic support it */
if (capset(&header, &cap) < 0)
exit(EXIT_FAILURE);
#endif
}
int main(int argc, char *argv[])
{
const char *path;
__btd_log_init(NULL, 0);
DBG("");
set_capabilities();
if (argc > 1)
path = argv[1];
else
path = DEFAULT_SNOOP_FILE;
mainloop_init();
if (!strcmp(DEFAULT_SNOOP_FILE, path))
rename(DEFAULT_SNOOP_FILE, DEFAULT_SNOOP_FILE ".old");
if (open_monitor(path) < 0) {
error("bluetoothd_snoop: start failed");
return EXIT_FAILURE;
}
info("bluetoothd_snoop: started");
mainloop_run_with_signal(signal_callback, NULL);
close_monitor();
info("bluetoothd_snoop: stopped");
__btd_log_cleanup();
return EXIT_SUCCESS;
}