1 /******************************************************************************
2 *
3 * Copyright (C) 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #define LOG_TAG "bt_snoop"
20
21 #include <arpa/inet.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <netinet/in.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34
35 #include "hci/include/btsnoop.h"
36 #include "hci/include/btsnoop_mem.h"
37 #include "bt_types.h"
38 #include "hci_layer.h"
39 #include "osi/include/log.h"
40 #include "stack_config.h"
41
42 typedef enum {
43 kCommandPacket = 1,
44 kAclPacket = 2,
45 kScoPacket = 3,
46 kEventPacket = 4
47 } packet_type_t;
48
49 // Epoch in microseconds since 01/01/0000.
50 static const uint64_t BTSNOOP_EPOCH_DELTA = 0x00dcddb30f2f8000ULL;
51
52 static const stack_config_t *stack_config;
53
54 static int logfile_fd = INVALID_FD;
55 static bool module_started;
56 static bool is_logging;
57 static bool logging_enabled_via_api;
58
59 // TODO(zachoverflow): merge btsnoop and btsnoop_net together
60 void btsnoop_net_open();
61 void btsnoop_net_close();
62 void btsnoop_net_write(const void *data, size_t length);
63
64 static void btsnoop_write_packet(packet_type_t type, const uint8_t *packet, bool is_received);
65 static void update_logging();
66
67 // Module lifecycle functions
68
start_up(void)69 static future_t *start_up(void) {
70 module_started = true;
71 update_logging();
72
73 return NULL;
74 }
75
shut_down(void)76 static future_t *shut_down(void) {
77 module_started = false;
78 update_logging();
79
80 return NULL;
81 }
82
83 const module_t btsnoop_module = {
84 .name = BTSNOOP_MODULE,
85 .init = NULL,
86 .start_up = start_up,
87 .shut_down = shut_down,
88 .clean_up = NULL,
89 .dependencies = {
90 STACK_CONFIG_MODULE,
91 NULL
92 }
93 };
94
95 // Interface functions
96
set_api_wants_to_log(bool value)97 static void set_api_wants_to_log(bool value) {
98 logging_enabled_via_api = value;
99 update_logging();
100 }
101
capture(const BT_HDR * buffer,bool is_received)102 static void capture(const BT_HDR *buffer, bool is_received) {
103 const uint8_t *p = buffer->data + buffer->offset;
104
105 btsnoop_mem_capture(buffer);
106
107 if (logfile_fd == INVALID_FD)
108 return;
109
110 switch (buffer->event & MSG_EVT_MASK) {
111 case MSG_HC_TO_STACK_HCI_EVT:
112 btsnoop_write_packet(kEventPacket, p, false);
113 break;
114 case MSG_HC_TO_STACK_HCI_ACL:
115 case MSG_STACK_TO_HC_HCI_ACL:
116 btsnoop_write_packet(kAclPacket, p, is_received);
117 break;
118 case MSG_HC_TO_STACK_HCI_SCO:
119 case MSG_STACK_TO_HC_HCI_SCO:
120 btsnoop_write_packet(kScoPacket, p, is_received);
121 break;
122 case MSG_STACK_TO_HC_HCI_CMD:
123 btsnoop_write_packet(kCommandPacket, p, true);
124 break;
125 }
126 }
127
128 static const btsnoop_t interface = {
129 set_api_wants_to_log,
130 capture
131 };
132
btsnoop_get_interface()133 const btsnoop_t *btsnoop_get_interface() {
134 stack_config = stack_config_get_interface();
135 return &interface;
136 }
137
138 // Internal functions
139
btsnoop_timestamp(void)140 static uint64_t btsnoop_timestamp(void) {
141 struct timeval tv;
142 gettimeofday(&tv, NULL);
143
144 // Timestamp is in microseconds.
145 uint64_t timestamp = tv.tv_sec * 1000 * 1000LL;
146 timestamp += tv.tv_usec;
147 timestamp += BTSNOOP_EPOCH_DELTA;
148 return timestamp;
149 }
150
update_logging()151 static void update_logging() {
152 bool should_log = module_started &&
153 (logging_enabled_via_api || stack_config->get_btsnoop_turned_on());
154
155 if (should_log == is_logging)
156 return;
157
158 is_logging = should_log;
159 if (should_log) {
160 btsnoop_net_open();
161
162 const char *log_path = stack_config->get_btsnoop_log_path();
163
164 // Save the old log if configured to do so
165 if (stack_config->get_btsnoop_should_save_last()) {
166 char last_log_path[PATH_MAX];
167 snprintf(last_log_path, PATH_MAX, "%s.%llu", log_path, btsnoop_timestamp());
168 if (!rename(log_path, last_log_path) && errno != ENOENT)
169 LOG_ERROR("%s unable to rename '%s' to '%s': %s", __func__, log_path, last_log_path, strerror(errno));
170 }
171
172 logfile_fd = TEMP_FAILURE_RETRY(open(log_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH));
173 if (logfile_fd == INVALID_FD) {
174 LOG_ERROR("%s unable to open '%s': %s", __func__, log_path, strerror(errno));
175 is_logging = false;
176 return;
177 }
178
179 TEMP_FAILURE_RETRY(write(logfile_fd, "btsnoop\0\0\0\0\1\0\0\x3\xea", 16));
180 } else {
181 if (logfile_fd != INVALID_FD)
182 close(logfile_fd);
183
184 logfile_fd = INVALID_FD;
185 btsnoop_net_close();
186 }
187 }
188
btsnoop_write(const void * data,size_t length)189 static void btsnoop_write(const void *data, size_t length) {
190 if (logfile_fd != INVALID_FD)
191 TEMP_FAILURE_RETRY(write(logfile_fd, data, length));
192
193 btsnoop_net_write(data, length);
194 }
195
btsnoop_write_packet(packet_type_t type,const uint8_t * packet,bool is_received)196 static void btsnoop_write_packet(packet_type_t type, const uint8_t *packet, bool is_received) {
197 int length_he = 0;
198 int length;
199 int flags;
200 int drops = 0;
201 switch (type) {
202 case kCommandPacket:
203 length_he = packet[2] + 4;
204 flags = 2;
205 break;
206 case kAclPacket:
207 length_he = (packet[3] << 8) + packet[2] + 5;
208 flags = is_received;
209 break;
210 case kScoPacket:
211 length_he = packet[2] + 4;
212 flags = is_received;
213 break;
214 case kEventPacket:
215 length_he = packet[1] + 3;
216 flags = 3;
217 break;
218 }
219
220 uint64_t timestamp = btsnoop_timestamp();
221 uint32_t time_hi = timestamp >> 32;
222 uint32_t time_lo = timestamp & 0xFFFFFFFF;
223
224 length = htonl(length_he);
225 flags = htonl(flags);
226 drops = htonl(drops);
227 time_hi = htonl(time_hi);
228 time_lo = htonl(time_lo);
229
230 btsnoop_write(&length, 4);
231 btsnoop_write(&length, 4);
232 btsnoop_write(&flags, 4);
233 btsnoop_write(&drops, 4);
234 btsnoop_write(&time_hi, 4);
235 btsnoop_write(&time_lo, 4);
236 btsnoop_write(&type, 1);
237 btsnoop_write(packet, length_he - 1);
238 }
239