1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "bdaddr"
18 
19 #include <stdint.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <sys/socket.h>
24 #include <log/log.h>
25 
26 #define HCI_DEV_NONE  0xffff
27 #define HCI_CHANNEL_CONTROL  3
28 #define BTPROTO_HCI  1
29 
30 struct sockaddr_hci {
31     sa_family_t hci_family;
32     uint16_t hci_dev;
33     uint16_t hci_channel;
34 };
35 
36 #define BTMGMT_CMD_READ_CONFIG_INFO  0x0037
37 #define BTMGMT_CMD_SET_PUBLIC_ADDR  0x0039
38 #define BTMGMT_EV_CMD_COMPLETE  0x0001
39 #define BTMGMT_EV_CMD_STATUS  0x0002
40 #define BTMGMT_EV_UNCONF_INDEX_ADDED  0x001d
41 #define BTMGMT_OPT_PUBLIC_ADDRESS  (1 << 1)
42 #define BTMGMT_ERR_INVALID_INDEX  0x11
43 
44 struct btmgmt_hdr {
45     uint16_t cmd;
46     uint16_t id;
47     uint16_t len;
48 } __attribute__((packed));
49 
50 struct btmgmt_cmd_set_public_addr {
51     struct btmgmt_hdr hdr;
52     uint8_t addr[6];
53 } __attribute__((packed));
54 
55 struct btmgmt_ev_cmd_status {
56     struct btmgmt_hdr hdr;
57     uint16_t cmd;
58     uint8_t status;
59 } __attribute__((packed));
60 
61 struct btmgmt_ev_cc_config_info {
62     struct btmgmt_ev_cmd_status ev;
63     uint16_t manufacturer;
64     uint32_t supported_options;
65     uint32_t missing_options;
66 } __attribute__((packed));
67 
68 // TODO: Make this configurable
69 #define HCI_CONTROLLER  0
70 
71 #define MAC_ADDRESS_SIZE    6
72 #define MAC_ADDRESS_LENGTH  (MAC_ADDRESS_SIZE*2 + MAC_ADDRESS_SIZE-1)
73 #define MAC_ADDRESS_FORMAT  "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"
74 #define MAC_ADDRESS_ARGS(addr) \
75     (addr)[5], (addr)[4], (addr)[3], (addr)[2], (addr)[1], (addr)[0]
76 
77 #define MESSAGE_BUFFER  512
78 
btmgmt_connect()79 static int btmgmt_connect() {
80     int s = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
81     if (s < 0)
82         return -1;
83 
84     struct sockaddr_hci addr = {
85         .hci_family = AF_BLUETOOTH,
86         .hci_dev = HCI_DEV_NONE,
87         .hci_channel = HCI_CHANNEL_CONTROL,
88     };
89     if (bind(s, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
90         close(s);
91         return -1;
92     }
93 
94     return s;
95 }
96 
btmgmt_request_config_info(int s)97 static void btmgmt_request_config_info(int s) {
98     struct btmgmt_hdr cmd = {
99         .cmd = BTMGMT_CMD_READ_CONFIG_INFO,
100         .id = HCI_CONTROLLER,
101         .len = 0,
102     };
103 
104     if (write(s, &cmd, sizeof(cmd)) < 0) {
105         ALOGE("Failed to request controller configuration information: %s",
106             strerror(errno));
107     }
108 }
109 
btmgmt_set_public_addr(int s,const uint8_t bdaddr[MAC_ADDRESS_SIZE])110 static void btmgmt_set_public_addr(int s, const uint8_t bdaddr[MAC_ADDRESS_SIZE]) {
111     struct btmgmt_cmd_set_public_addr cmd = {
112         .hdr = {
113             .cmd = BTMGMT_CMD_SET_PUBLIC_ADDR,
114             .id = HCI_CONTROLLER,
115             .len = sizeof(cmd) - sizeof(cmd.hdr),
116         },
117     };
118     memcpy(cmd.addr, bdaddr, sizeof(cmd.addr));
119 
120     if (write(s, &cmd, sizeof(cmd)) != sizeof(cmd)) {
121         ALOGE("Failed to write set public address command: %s", strerror(errno));
122     }
123 }
124 
btmgmt_complete_set_public_addr(struct btmgmt_ev_cmd_status * ev,const uint8_t bdaddr[MAC_ADDRESS_SIZE])125 static void btmgmt_complete_set_public_addr(struct btmgmt_ev_cmd_status* ev,
126         const uint8_t bdaddr[MAC_ADDRESS_SIZE]) {
127     if (ev->status == 0) {
128         ALOGI("Updated public address to " MAC_ADDRESS_FORMAT,
129             MAC_ADDRESS_ARGS(bdaddr));
130     } else {
131         ALOGE("Failed to update public address to " MAC_ADDRESS_FORMAT
132             ": error 0x%x", MAC_ADDRESS_ARGS(bdaddr), ev->status);
133     }
134 }
135 
btmgmt_config_needs_public_addr(struct btmgmt_ev_cmd_status * ev)136 static bool btmgmt_config_needs_public_addr(struct btmgmt_ev_cmd_status* ev) {
137     struct btmgmt_ev_cc_config_info* info = (struct btmgmt_ev_cc_config_info*) ev;
138     if (info->ev.status) {
139         if (info->ev.status != BTMGMT_ERR_INVALID_INDEX)
140             ALOGE("Failed to read controller configuration information: 0x%x",
141                 info->ev.status);
142         return false;
143     }
144 
145     if (info->ev.hdr.cmd != BTMGMT_EV_CMD_COMPLETE
146             || info->ev.hdr.len < sizeof(*info) - sizeof(info->ev.hdr))
147         return false;
148 
149     if (info->missing_options & BTMGMT_OPT_PUBLIC_ADDRESS) {
150         return true;
151     } else {
152         ALOGD("Controller is already configured with a public address");
153         return false;
154     }
155 }
156 
main(int argc,char * argv[])157 int main(int argc, char* argv[]) {
158     if (argc < 2) {
159         ALOGE("Usage: bdaddr <bdaddr>");
160         return 1;
161     }
162 
163     uint8_t bdaddr[MAC_ADDRESS_SIZE];
164     if (strlen(argv[1]) != MAC_ADDRESS_LENGTH
165         || sscanf(argv[1], MAC_ADDRESS_FORMAT,
166             &bdaddr[5], &bdaddr[4], &bdaddr[3],
167             &bdaddr[2], &bdaddr[1], &bdaddr[0]) != MAC_ADDRESS_SIZE) {
168         ALOGE("Invalid MAC address: %s", argv[1]);
169         return 1;
170     }
171 
172     int s = btmgmt_connect();
173     if (s < 0) {
174         ALOGE("Failed to create Bluetooth management socket: %s", strerror(errno));
175         return 1;
176     }
177 
178     btmgmt_request_config_info(s);
179 
180     char buf[MESSAGE_BUFFER];
181     struct btmgmt_hdr* hdr = (struct btmgmt_hdr*) buf;
182     struct btmgmt_ev_cmd_status* ev = (struct btmgmt_ev_cmd_status*) hdr;
183 
184     while (true) {
185         ssize_t len = read(s, buf, sizeof(buf));
186         if (len < (ssize_t) sizeof(struct btmgmt_hdr))
187             continue;
188         if (len < (ssize_t) sizeof(struct btmgmt_hdr) + hdr->len)
189             continue;
190         if (hdr->id != HCI_CONTROLLER)
191             continue;
192 
193         switch (hdr->cmd) {
194         case BTMGMT_EV_CMD_COMPLETE:
195         case BTMGMT_EV_CMD_STATUS:
196             if (hdr->len < sizeof(*ev) - sizeof(hdr))
197                 continue;
198 
199             switch (ev->cmd) {
200             case BTMGMT_CMD_READ_CONFIG_INFO:
201                 if (btmgmt_config_needs_public_addr(ev))
202                     btmgmt_set_public_addr(s, bdaddr);
203                 break;
204             case BTMGMT_CMD_SET_PUBLIC_ADDR:
205                 btmgmt_complete_set_public_addr(ev, bdaddr);
206                 break;
207             }
208 
209             break;
210         case BTMGMT_EV_UNCONF_INDEX_ADDED:
211             btmgmt_request_config_info(s);
212             break;
213         }
214     }
215 }
216