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