1 /*
2  * Copyright (C) 2019 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 #include "adb_wifi.h"
18 
19 #include <unistd.h>
20 #include <optional>
21 
22 #include <adbd_auth.h>
23 #include <android-base/properties.h>
24 
25 #include "adb.h"
26 #include "daemon/mdns.h"
27 #include "sysdeps.h"
28 #include "transport.h"
29 
30 using namespace android::base;
31 
32 namespace {
33 
34 static AdbdAuthContext* auth_ctx;
35 
36 static void adb_disconnected(void* unused, atransport* t);
37 static struct adisconnect adb_disconnect = {adb_disconnected, nullptr};
38 
adb_disconnected(void * unused,atransport * t)39 static void adb_disconnected(void* unused, atransport* t) {
40     LOG(INFO) << "ADB wifi device disconnected";
41     CHECK(t->auth_id.has_value());
42     adbd_auth_tls_device_disconnected(auth_ctx, kAdbTransportTypeWifi, t->auth_id.value());
43 }
44 
45 // TODO(b/31559095): need bionic host so that we can use 'prop_info' returned
46 // from WaitForProperty
47 #if defined(__ANDROID__)
48 
49 class TlsServer {
50   public:
51     explicit TlsServer(int port);
52     virtual ~TlsServer();
53     bool Start();
port()54     uint16_t port() { return port_; };
55 
56   private:
57     void OnFdEvent(int fd, unsigned ev);
58     static void StaticOnFdEvent(int fd, unsigned ev, void* opaque);
59 
60     fdevent* fd_event_ = nullptr;
61     uint16_t port_;
62 };  // TlsServer
63 
TlsServer(int port)64 TlsServer::TlsServer(int port) : port_(port) {}
65 
~TlsServer()66 TlsServer::~TlsServer() {
67     fdevent* fde = fd_event_;
68     fdevent_run_on_looper([fde]() {
69         if (fde != nullptr) {
70             fdevent_destroy(fde);
71         }
72     });
73 }
74 
Start()75 bool TlsServer::Start() {
76     std::condition_variable cv;
77     std::mutex mutex;
78     std::optional<bool> success;
79     auto callback = [&](bool result) {
80         {
81             std::lock_guard<std::mutex> lock(mutex);
82             success = result;
83         }
84         cv.notify_one();
85     };
86 
87     std::string err;
88     unique_fd fd(network_inaddr_any_server(port_, SOCK_STREAM, &err));
89     if (fd.get() == -1) {
90         LOG(ERROR) << "Failed to start TLS server [" << err << "]";
91         return false;
92     }
93     close_on_exec(fd.get());
94     int port = socket_get_local_port(fd.get());
95     if (port <= 0 || port > 65535) {
96         LOG(ERROR) << "Invalid port for tls server";
97         return false;
98     }
99     port_ = static_cast<uint16_t>(port);
100     LOG(INFO) << "adbwifi started on port " << port_;
101 
102     std::unique_lock<std::mutex> lock(mutex);
103     fdevent_run_on_looper([&]() {
104         fd_event_ = fdevent_create(fd.release(), &TlsServer::StaticOnFdEvent, this);
105         if (fd_event_ == nullptr) {
106             LOG(ERROR) << "Failed to create fd event for TlsServer.";
107             callback(false);
108             return;
109         }
110         callback(true);
111     });
112 
113     cv.wait(lock, [&]() { return success.has_value(); });
114     if (!*success) {
115         LOG(INFO) << "TlsServer fdevent_create failed";
116         return false;
117     }
118     fdevent_set(fd_event_, FDE_READ);
119     LOG(INFO) << "TlsServer running on port " << port_;
120 
121     return *success;
122 }
123 
124 // static
StaticOnFdEvent(int fd,unsigned ev,void * opaque)125 void TlsServer::StaticOnFdEvent(int fd, unsigned ev, void* opaque) {
126     auto server = reinterpret_cast<TlsServer*>(opaque);
127     server->OnFdEvent(fd, ev);
128 }
129 
OnFdEvent(int fd,unsigned ev)130 void TlsServer::OnFdEvent(int fd, unsigned ev) {
131     if ((ev & FDE_READ) == 0 || fd != fd_event_->fd.get()) {
132         LOG(INFO) << __func__ << ": No read [ev=" << ev << " fd=" << fd << "]";
133         return;
134     }
135 
136     unique_fd new_fd(adb_socket_accept(fd, nullptr, nullptr));
137     if (new_fd >= 0) {
138         LOG(INFO) << "New TLS connection [fd=" << new_fd.get() << "]";
139         close_on_exec(new_fd.get());
140         disable_tcp_nagle(new_fd.get());
141         std::string serial = android::base::StringPrintf("host-%d", new_fd.get());
142         register_socket_transport(
143                 std::move(new_fd), std::move(serial), port_, 1,
144                 [](atransport*) { return ReconnectResult::Abort; }, true);
145     }
146 }
147 
148 TlsServer* sTlsServer = nullptr;
149 const char kWifiPortProp[] = "service.adb.tls.port";
150 
151 const char kWifiEnabledProp[] = "persist.adb.tls_server.enable";
152 
enable_wifi_debugging()153 static void enable_wifi_debugging() {
154     start_mdnsd();
155 
156     if (sTlsServer != nullptr) {
157         delete sTlsServer;
158     }
159     sTlsServer = new TlsServer(0);
160     if (!sTlsServer->Start()) {
161         LOG(ERROR) << "Failed to start TlsServer";
162         delete sTlsServer;
163         sTlsServer = nullptr;
164         return;
165     }
166 
167     // Start mdns connect service for discovery
168     register_adb_secure_connect_service(sTlsServer->port());
169     LOG(INFO) << "adb wifi started on port " << sTlsServer->port();
170     SetProperty(kWifiPortProp, std::to_string(sTlsServer->port()));
171 }
172 
disable_wifi_debugging()173 static void disable_wifi_debugging() {
174     if (sTlsServer != nullptr) {
175         delete sTlsServer;
176         sTlsServer = nullptr;
177     }
178     if (is_adb_secure_connect_service_registered()) {
179         unregister_adb_secure_connect_service();
180     }
181     kick_all_tcp_tls_transports();
182     LOG(INFO) << "adb wifi stopped";
183     SetProperty(kWifiPortProp, "");
184 }
185 
186 // Watches for the #kWifiEnabledProp property to toggle the TlsServer
start_wifi_enabled_observer()187 static void start_wifi_enabled_observer() {
188     std::thread([]() {
189         bool wifi_enabled = false;
190         while (true) {
191             std::string toggled_val = wifi_enabled ? "0" : "1";
192             LOG(INFO) << "Waiting for " << kWifiEnabledProp << "=" << toggled_val;
193 
194             if (WaitForProperty(kWifiEnabledProp, toggled_val)) {
195                 wifi_enabled = !wifi_enabled;
196                 LOG(INFO) << kWifiEnabledProp << " changed to " << toggled_val;
197                 if (wifi_enabled) {
198                     enable_wifi_debugging();
199                 } else {
200                     disable_wifi_debugging();
201                 }
202             }
203         }
204     }).detach();
205 }
206 #endif  //__ANDROID__
207 
208 }  // namespace
209 
adbd_wifi_init(AdbdAuthContext * ctx)210 void adbd_wifi_init(AdbdAuthContext* ctx) {
211     auth_ctx = ctx;
212 #if defined(__ANDROID__)
213     start_wifi_enabled_observer();
214 #endif  //__ANDROID__
215 }
216 
adbd_wifi_secure_connect(atransport * t)217 void adbd_wifi_secure_connect(atransport* t) {
218     t->AddDisconnect(&adb_disconnect);
219     handle_online(t);
220     send_connect(t);
221     LOG(INFO) << __func__ << ": connected " << t->serial;
222     t->auth_id = adbd_auth_tls_device_connected(auth_ctx, kAdbTransportTypeWifi, t->auth_key.data(),
223                                                 t->auth_key.size());
224 }
225