Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 17 | #include <binder_rpc_unstable.hpp> |
| 18 | |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 20 | #include <android-base/unique_fd.h> |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 21 | #include <android/binder_libbinder.h> |
| 22 | #include <binder/RpcServer.h> |
| 23 | #include <binder/RpcSession.h> |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 24 | #include <cutils/sockets.h> |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 25 | #include <linux/vm_sockets.h> |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 26 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 27 | using android::OK; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 28 | using android::RpcServer; |
| 29 | using android::RpcSession; |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 30 | using android::sp; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 31 | using android::status_t; |
| 32 | using android::statusToString; |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 33 | using android::base::unique_fd; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 34 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 35 | // Opaque handle for RpcServer. |
| 36 | struct ARpcServer {}; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 37 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 38 | static sp<RpcServer> toRpcServer(ARpcServer* handle) { |
| 39 | auto ref = reinterpret_cast<RpcServer*>(handle); |
| 40 | return sp<RpcServer>::fromExisting(ref); |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 41 | } |
| 42 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 43 | static ARpcServer* createRpcServerHandle(sp<RpcServer>& server) { |
| 44 | auto ref = server.get(); |
| 45 | ref->incStrong(ref); |
| 46 | return reinterpret_cast<ARpcServer*>(ref); |
| 47 | } |
| 48 | |
| 49 | static void freeRpcServerHandle(ARpcServer* handle) { |
| 50 | auto ref = reinterpret_cast<RpcServer*>(handle); |
| 51 | ref->decStrong(ref); |
| 52 | } |
| 53 | |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame^] | 54 | static unsigned int cidFromStructAddr(const void* addr, size_t addrlen) { |
| 55 | LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated"); |
| 56 | const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr); |
| 57 | LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock"); |
| 58 | return vaddr->svm_cid; |
| 59 | } |
| 60 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 61 | extern "C" { |
| 62 | |
Alice Wang | 7e93560 | 2022-10-17 08:06:16 +0000 | [diff] [blame] | 63 | bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context), |
| 64 | void* factoryContext, unsigned int port) { |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 65 | auto server = RpcServer::make(); |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame^] | 66 | if (status_t status = server->setupVsockServer(VMADDR_CID_ANY, port); status != OK) { |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 67 | LOG(ERROR) << "Failed to set up vsock server with port " << port |
| 68 | << " error: " << statusToString(status).c_str(); |
| 69 | return false; |
| 70 | } |
Andrei Homescu | 86124ca | 2022-04-21 22:22:48 +0000 | [diff] [blame] | 71 | server->setPerSessionRootObject([=](const void* addr, size_t addrlen) { |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame^] | 72 | unsigned int cid = cidFromStructAddr(addr, addrlen); |
| 73 | return AIBinder_toPlatformBinder(factory(cid, factoryContext)); |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 74 | }); |
| 75 | |
| 76 | server->join(); |
| 77 | |
| 78 | // Shutdown any open sessions since server failed. |
| 79 | (void)server->shutdown(); |
| 80 | return true; |
| 81 | } |
| 82 | |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame^] | 83 | ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 84 | auto server = RpcServer::make(); |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame^] | 85 | |
| 86 | unsigned int bindCid = VMADDR_CID_ANY; // bind to the remote interface |
| 87 | if (cid == VMADDR_CID_LOCAL) { |
| 88 | bindCid = VMADDR_CID_LOCAL; // bind to the local interface |
| 89 | cid = VMADDR_CID_ANY; // no need for a connection filter |
| 90 | } |
| 91 | |
| 92 | if (status_t status = server->setupVsockServer(bindCid, port); status != OK) { |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 93 | LOG(ERROR) << "Failed to set up vsock server with port " << port |
| 94 | << " error: " << statusToString(status).c_str(); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 95 | return nullptr; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 96 | } |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame^] | 97 | if (cid != VMADDR_CID_ANY) { |
| 98 | server->setConnectionFilter([=](const void* addr, size_t addrlen) { |
| 99 | unsigned int remoteCid = cidFromStructAddr(addr, addrlen); |
| 100 | if (cid != remoteCid) { |
| 101 | LOG(ERROR) << "Rejected vsock connection from CID " << remoteCid; |
| 102 | return false; |
| 103 | } |
| 104 | return true; |
| 105 | }); |
| 106 | } |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 107 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
| 108 | return createRpcServerHandle(server); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 109 | } |
| 110 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 111 | ARpcServer* ARpcServer_newInitUnixDomain(AIBinder* service, const char* name) { |
| 112 | auto server = RpcServer::make(); |
| 113 | auto fd = unique_fd(android_get_control_socket(name)); |
| 114 | if (!fd.ok()) { |
| 115 | LOG(ERROR) << "Failed to get fd for the socket:" << name; |
| 116 | return nullptr; |
| 117 | } |
| 118 | if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) { |
| 119 | LOG(ERROR) << "Failed to set up Unix Domain RPC server with name " << name |
| 120 | << " error: " << statusToString(status).c_str(); |
| 121 | return nullptr; |
| 122 | } |
| 123 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
| 124 | return createRpcServerHandle(server); |
| 125 | } |
| 126 | |
| 127 | void ARpcServer_start(ARpcServer* handle) { |
| 128 | toRpcServer(handle)->start(); |
| 129 | } |
| 130 | |
| 131 | void ARpcServer_join(ARpcServer* handle) { |
| 132 | toRpcServer(handle)->join(); |
| 133 | } |
| 134 | |
| 135 | void ARpcServer_shutdown(ARpcServer* handle) { |
| 136 | toRpcServer(handle)->shutdown(); |
| 137 | } |
| 138 | |
| 139 | void ARpcServer_free(ARpcServer* handle) { |
| 140 | freeRpcServerHandle(handle); |
Inseob Kim | 172473f | 2021-09-07 21:01:33 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Alice Wang | 0746668 | 2022-10-19 15:28:29 +0000 | [diff] [blame] | 143 | AIBinder* VsockRpcClient(unsigned int cid, unsigned int port) { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 144 | auto session = RpcSession::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 145 | if (status_t status = session->setupVsockClient(cid, port); status != OK) { |
| 146 | LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port |
| 147 | << " error: " << statusToString(status).c_str(); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 148 | return nullptr; |
| 149 | } |
| 150 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 151 | } |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 152 | |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 153 | AIBinder* UnixDomainRpcClient(const char* name) { |
| 154 | std::string pathname(name); |
| 155 | pathname = ANDROID_SOCKET_DIR "/" + pathname; |
| 156 | auto session = RpcSession::make(); |
| 157 | if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) { |
| 158 | LOG(ERROR) << "Failed to set up Unix Domain RPC client with path: " << pathname |
| 159 | << " error: " << statusToString(status).c_str(); |
| 160 | return nullptr; |
| 161 | } |
| 162 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 163 | } |
| 164 | |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 165 | AIBinder* RpcPreconnectedClient(int (*requestFd)(void* param), void* param) { |
| 166 | auto session = RpcSession::make(); |
| 167 | auto request = [=] { return unique_fd{requestFd(param)}; }; |
| 168 | if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) { |
| 169 | LOG(ERROR) << "Failed to set up vsock client. error: " << statusToString(status).c_str(); |
| 170 | return nullptr; |
| 171 | } |
| 172 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 173 | } |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 174 | } |