blob: e8b91e7a4c917fea6a32ce2237214ae34e1efdb9 [file] [log] [blame]
Andrei Homescu74a54452021-12-10 05:30:21 +00001/*
2 * Copyright (C) 2022 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 "RpcServerTrusty"
18
19#include <binder/Parcel.h>
20#include <binder/RpcServer.h>
21#include <binder/RpcServerTrusty.h>
22#include <binder/RpcThreads.h>
23#include <binder/RpcTransportTipcTrusty.h>
24#include <log/log.h>
25
26#include "../FdTrigger.h"
27#include "../RpcState.h"
28#include "TrustyStatus.h"
29
30using android::base::unexpected;
31
32namespace android {
33
34android::base::expected<sp<RpcServerTrusty>, int> RpcServerTrusty::make(
35 tipc_hset* handleSet, std::string&& portName, std::shared_ptr<const PortAcl>&& portAcl,
36 size_t msgMaxSize, std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory) {
37 // Default is without TLS.
38 if (rpcTransportCtxFactory == nullptr)
39 rpcTransportCtxFactory = RpcTransportCtxFactoryTipcTrusty::make();
40 auto ctx = rpcTransportCtxFactory->newServerCtx();
41 if (ctx == nullptr) {
42 return unexpected(ERR_NO_MEMORY);
43 }
44
45 auto srv = sp<RpcServerTrusty>::make(std::move(ctx), std::move(portName), std::move(portAcl),
46 msgMaxSize);
47 if (srv == nullptr) {
48 return unexpected(ERR_NO_MEMORY);
49 }
50
51 int rc = tipc_add_service(handleSet, &srv->mTipcPort, 1, 0, &kTipcOps);
52 if (rc != NO_ERROR) {
53 return unexpected(rc);
54 }
55 return srv;
56}
57
58RpcServerTrusty::RpcServerTrusty(std::unique_ptr<RpcTransportCtx> ctx, std::string&& portName,
59 std::shared_ptr<const PortAcl>&& portAcl, size_t msgMaxSize)
60 : mRpcServer(sp<RpcServer>::make(std::move(ctx))),
61 mPortName(std::move(portName)),
62 mPortAcl(std::move(portAcl)) {
63 mTipcPort.name = mPortName.c_str();
64 mTipcPort.msg_max_size = msgMaxSize;
65 mTipcPort.msg_queue_len = 6; // Three each way
66 mTipcPort.priv = this;
67
68 if (mPortAcl) {
69 // Initialize the array of pointers to uuids.
70 // The pointers in mUuidPtrs should stay valid across moves of
71 // RpcServerTrusty (the addresses of a std::vector's elements
72 // shouldn't change when the vector is moved), but would be invalidated
73 // by a copy which is why we disable the copy constructor and assignment
74 // operator for RpcServerTrusty.
75 auto numUuids = mPortAcl->uuids.size();
76 mUuidPtrs.resize(numUuids);
77 for (size_t i = 0; i < numUuids; i++) {
78 mUuidPtrs[i] = &mPortAcl->uuids[i];
79 }
80
81 // Copy the contents of portAcl into the tipc_port_acl structure that we
82 // pass to tipc_add_service
83 mTipcPortAcl.flags = mPortAcl->flags;
84 mTipcPortAcl.uuid_num = numUuids;
85 mTipcPortAcl.uuids = mUuidPtrs.data();
86 mTipcPortAcl.extra_data = mPortAcl->extraData;
87
88 mTipcPort.acl = &mTipcPortAcl;
89 } else {
90 mTipcPort.acl = nullptr;
91 }
92}
93
94int RpcServerTrusty::handleConnect(const tipc_port* port, handle_t chan, const uuid* peer,
95 void** ctx_p) {
96 auto* server = reinterpret_cast<RpcServerTrusty*>(const_cast<void*>(port->priv));
97 server->mRpcServer->mShutdownTrigger = FdTrigger::make();
98 server->mRpcServer->mConnectingThreads[rpc_this_thread::get_id()] = RpcMaybeThread();
99
100 int rc = NO_ERROR;
101 auto joinFn = [&](sp<RpcSession>&& session, RpcSession::PreJoinSetupResult&& result) {
102 if (result.status != OK) {
103 rc = statusToTrusty(result.status);
104 return;
105 }
106
107 /* Save the session for easy access */
108 *ctx_p = session.get();
109 };
110
111 base::unique_fd clientFd(chan);
112 std::array<uint8_t, RpcServer::kRpcAddressSize> addr;
113 constexpr size_t addrLen = sizeof(*peer);
114 memcpy(addr.data(), peer, addrLen);
115 RpcServer::establishConnection(sp(server->mRpcServer), std::move(clientFd), addr, addrLen,
116 joinFn);
117
118 return rc;
119}
120
121int RpcServerTrusty::handleMessage(const tipc_port* port, handle_t chan, void* ctx) {
122 auto* session = reinterpret_cast<RpcSession*>(ctx);
123 status_t status = session->state()->drainCommands(session->mConnections.mIncoming[0], session,
124 RpcState::CommandType::ANY);
125 if (status != OK) {
126 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
127 statusToString(status).c_str());
128 }
129
130 return NO_ERROR;
131}
132
133void RpcServerTrusty::handleDisconnect(const tipc_port* port, handle_t chan, void* ctx) {}
134
135void RpcServerTrusty::handleChannelCleanup(void* ctx) {
136 auto* session = reinterpret_cast<RpcSession*>(ctx);
137 auto& connection = session->mConnections.mIncoming.at(0);
138 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
139 "bad state: connection object guaranteed to be in list");
140}
141
142} // namespace android