blob: 88f8c943a4245b38607ec4290ad25588aea4fe93 [file] [log] [blame]
Victor Hsiehd35d31d2021-06-03 11:24:31 -07001/*
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 Brazdilefb56832022-11-16 11:47:00 +000017#include <binder_rpc_unstable.hpp>
18
Victor Hsiehd35d31d2021-06-03 11:24:31 -070019#include <android-base/logging.h>
Inseob Kime12fccc2021-09-02 17:23:33 +090020#include <android-base/unique_fd.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070021#include <android/binder_libbinder.h>
22#include <binder/RpcServer.h>
23#include <binder/RpcSession.h>
Alice Wang893a9912022-10-24 10:44:09 +000024#include <cutils/sockets.h>
Inseob Kim091050a2021-10-25 14:25:25 +000025#include <linux/vm_sockets.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070026
Steven Moreland2372f9d2021-08-05 15:42:01 -070027using android::OK;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070028using android::RpcServer;
29using android::RpcSession;
David Brazdilefb56832022-11-16 11:47:00 +000030using android::sp;
Steven Moreland2372f9d2021-08-05 15:42:01 -070031using android::status_t;
32using android::statusToString;
Inseob Kime12fccc2021-09-02 17:23:33 +090033using android::base::unique_fd;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070034
David Brazdilefb56832022-11-16 11:47:00 +000035// Opaque handle for RpcServer.
36struct ARpcServer {};
Victor Hsiehd35d31d2021-06-03 11:24:31 -070037
David Brazdilefb56832022-11-16 11:47:00 +000038static sp<RpcServer> toRpcServer(ARpcServer* handle) {
39 auto ref = reinterpret_cast<RpcServer*>(handle);
40 return sp<RpcServer>::fromExisting(ref);
Alice Wang893a9912022-10-24 10:44:09 +000041}
42
David Brazdilefb56832022-11-16 11:47:00 +000043static ARpcServer* createRpcServerHandle(sp<RpcServer>& server) {
44 auto ref = server.get();
45 ref->incStrong(ref);
46 return reinterpret_cast<ARpcServer*>(ref);
47}
48
49static void freeRpcServerHandle(ARpcServer* handle) {
50 auto ref = reinterpret_cast<RpcServer*>(handle);
51 ref->decStrong(ref);
52}
53
David Brazdila47dfda2022-11-22 22:52:19 +000054static 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 Brazdilefb56832022-11-16 11:47:00 +000061extern "C" {
62
Alice Wang7e935602022-10-17 08:06:16 +000063bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context),
64 void* factoryContext, unsigned int port) {
Inseob Kim091050a2021-10-25 14:25:25 +000065 auto server = RpcServer::make();
David Brazdila47dfda2022-11-22 22:52:19 +000066 if (status_t status = server->setupVsockServer(VMADDR_CID_ANY, port); status != OK) {
Inseob Kim091050a2021-10-25 14:25:25 +000067 LOG(ERROR) << "Failed to set up vsock server with port " << port
68 << " error: " << statusToString(status).c_str();
69 return false;
70 }
Andrei Homescu86124ca2022-04-21 22:22:48 +000071 server->setPerSessionRootObject([=](const void* addr, size_t addrlen) {
David Brazdila47dfda2022-11-22 22:52:19 +000072 unsigned int cid = cidFromStructAddr(addr, addrlen);
73 return AIBinder_toPlatformBinder(factory(cid, factoryContext));
Inseob Kim091050a2021-10-25 14:25:25 +000074 });
75
76 server->join();
77
78 // Shutdown any open sessions since server failed.
79 (void)server->shutdown();
80 return true;
81}
82
David Brazdila47dfda2022-11-22 22:52:19 +000083ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070084 auto server = RpcServer::make();
David Brazdila47dfda2022-11-22 22:52:19 +000085
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 Moreland2372f9d2021-08-05 15:42:01 -070093 LOG(ERROR) << "Failed to set up vsock server with port " << port
94 << " error: " << statusToString(status).c_str();
David Brazdilefb56832022-11-16 11:47:00 +000095 return nullptr;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070096 }
David Brazdila47dfda2022-11-22 22:52:19 +000097 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 Brazdilefb56832022-11-16 11:47:00 +0000107 server->setRootObject(AIBinder_toPlatformBinder(service));
108 return createRpcServerHandle(server);
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700109}
110
David Brazdilefb56832022-11-16 11:47:00 +0000111ARpcServer* 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
127void ARpcServer_start(ARpcServer* handle) {
128 toRpcServer(handle)->start();
129}
130
131void ARpcServer_join(ARpcServer* handle) {
132 toRpcServer(handle)->join();
133}
134
135void ARpcServer_shutdown(ARpcServer* handle) {
136 toRpcServer(handle)->shutdown();
137}
138
139void ARpcServer_free(ARpcServer* handle) {
140 freeRpcServerHandle(handle);
Inseob Kim172473f2021-09-07 21:01:33 +0900141}
142
Alice Wang07466682022-10-19 15:28:29 +0000143AIBinder* VsockRpcClient(unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700144 auto session = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -0700145 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 Hsiehd35d31d2021-06-03 11:24:31 -0700148 return nullptr;
149 }
150 return AIBinder_fromPlatformBinder(session->getRootObject());
151}
Inseob Kime12fccc2021-09-02 17:23:33 +0900152
Alice Wang893a9912022-10-24 10:44:09 +0000153AIBinder* 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 Kime12fccc2021-09-02 17:23:33 +0900165AIBinder* 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 Hsiehd35d31d2021-06-03 11:24:31 -0700174}