blob: c789614c0bef462b7de44103d1b3abd5d7e0b1ed [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
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000107 /* Save the session and connection for the other callbacks */
108 auto* channelContext = new (std::nothrow) ChannelContext;
109 if (channelContext == nullptr) {
110 rc = ERR_NO_MEMORY;
111 return;
112 }
113
114 channelContext->session = std::move(session);
115 channelContext->connection = std::move(result.connection);
116
117 *ctx_p = channelContext;
Andrei Homescu74a54452021-12-10 05:30:21 +0000118 };
119
120 base::unique_fd clientFd(chan);
121 std::array<uint8_t, RpcServer::kRpcAddressSize> addr;
122 constexpr size_t addrLen = sizeof(*peer);
123 memcpy(addr.data(), peer, addrLen);
124 RpcServer::establishConnection(sp(server->mRpcServer), std::move(clientFd), addr, addrLen,
125 joinFn);
126
127 return rc;
128}
129
130int RpcServerTrusty::handleMessage(const tipc_port* port, handle_t chan, void* ctx) {
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000131 auto* channelContext = reinterpret_cast<ChannelContext*>(ctx);
132 LOG_ALWAYS_FATAL_IF(channelContext == nullptr,
133 "bad state: message received on uninitialized channel");
134
135 auto& session = channelContext->session;
136 auto& connection = channelContext->connection;
137 status_t status =
138 session->state()->drainCommands(connection, session, RpcState::CommandType::ANY);
Andrei Homescu74a54452021-12-10 05:30:21 +0000139 if (status != OK) {
140 LOG_RPC_DETAIL("Binder connection thread closing w/ status %s",
141 statusToString(status).c_str());
142 }
143
144 return NO_ERROR;
145}
146
147void RpcServerTrusty::handleDisconnect(const tipc_port* port, handle_t chan, void* ctx) {}
148
149void RpcServerTrusty::handleChannelCleanup(void* ctx) {
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000150 auto* channelContext = reinterpret_cast<ChannelContext*>(ctx);
151 if (channelContext == nullptr) {
152 return;
153 }
154
155 auto& session = channelContext->session;
156 auto& connection = channelContext->connection;
Andrei Homescu74a54452021-12-10 05:30:21 +0000157 LOG_ALWAYS_FATAL_IF(!session->removeIncomingConnection(connection),
158 "bad state: connection object guaranteed to be in list");
Andrei Homescu5e74d2d2022-08-19 22:46:38 +0000159
160 delete channelContext;
Andrei Homescu74a54452021-12-10 05:30:21 +0000161}
162
163} // namespace android