1 /*
2 * Copyright (C) 2023 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 #include <errno.h>
18 #include <iostream>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/select.h>
22 #include <sys/socket.h>
23 #include <thread>
24 #include <unistd.h>
25
26 #include <libProxyConfig/libProxyConfig.h>
27
28 #include <linux/vm_sockets.h>
29
30 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
31 #define BUFFER_SIZE 16384
32 #define CLIENT_QUEUE_SIZE 128
33
setupServerSocket(sockaddr_vm & addr)34 int setupServerSocket(sockaddr_vm& addr) {
35 int vsock_socket = socket(AF_VSOCK, SOCK_STREAM, 0);
36
37 if (vsock_socket == -1) {
38 std::cerr << "Failed to create server VSOCK socket, ERROR = "
39 << strerror(errno) << std::endl;
40 return -1;
41 }
42
43 if (bind(vsock_socket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0) {
44 std::cerr << "Failed to bind to server VSOCK socket, ERROR = "
45 << strerror(errno) << std::endl;
46 return -1;
47 }
48
49 if (listen(vsock_socket, CLIENT_QUEUE_SIZE) != 0) {
50 std::cerr << "Failed to listen on server VSOCK socket, ERROR = "
51 << strerror(errno) << std::endl;
52 return -1;
53 }
54
55 return vsock_socket;
56 }
57
closeFileDescriptor(int fd)58 void closeFileDescriptor(int fd) {
59 close(fd);
60 shutdown(fd, SHUT_RDWR);
61 }
62
63 // transfers a max of BUFFER_SIZE bytes between a source file descriptor and a
64 // destination file descriptor. Returns true on success, false otherwise
transferBytes(int src_fd,int dst_fd)65 bool transferBytes(int src_fd, int dst_fd) {
66 char buf[BUFFER_SIZE];
67 int readBytes = read(src_fd, buf, BUFFER_SIZE);
68 if (readBytes <= 0) {
69 return false;
70 }
71 int writtenBytes = write(dst_fd, buf, readBytes);
72 return writtenBytes >= 0;
73 }
74
75 // Handles a client requesting to connect with the forwarding address
handleConnection(int client_sock,int fwd_cid,int fwd_port)76 void* handleConnection(int client_sock, int fwd_cid, int fwd_port) {
77 int server_sock = socket(AF_VSOCK, SOCK_STREAM, 0);
78
79 if (server_sock < 0) {
80 std::cerr << "Failed to create forwarding VSOCK socket, ERROR = "
81 << strerror(errno) << std::endl;
82 closeFileDescriptor(server_sock);
83 closeFileDescriptor(client_sock);
84 return nullptr;
85 }
86
87 sockaddr_vm fwd_addr{};
88 fwd_addr.svm_family = AF_VSOCK;
89 fwd_addr.svm_cid = fwd_cid;
90 fwd_addr.svm_port = fwd_port;
91
92 if (connect(server_sock, reinterpret_cast<sockaddr*>(&fwd_addr),
93 sizeof(fwd_addr)) < 0) {
94 std::cerr << "Failed to connect to forwarding vsock socket, ERROR = "
95 << strerror(errno) << std::endl;
96 closeFileDescriptor(server_sock);
97 closeFileDescriptor(client_sock);
98 return nullptr;
99 }
100
101 bool connected = true;
102 while (connected) {
103 fd_set file_descriptors;
104 FD_ZERO(&file_descriptors);
105 FD_SET(client_sock, &file_descriptors);
106 FD_SET(server_sock, &file_descriptors);
107
108 int rv = select(MAX(client_sock, server_sock) + 1, &file_descriptors, nullptr, nullptr,
109 nullptr);
110 if (rv == -1) {
111 std::cerr << "ERROR in Select!. Error = " << strerror(errno) << std::endl;
112 break;
113 }
114
115 if (FD_ISSET(client_sock, &file_descriptors)) {
116 // transfer bytes from client to forward address
117 connected = transferBytes(client_sock, server_sock);
118 }
119
120 if (FD_ISSET(server_sock, &file_descriptors)) {
121 // transfer bytes from forward address to client
122 connected = transferBytes(server_sock, client_sock);
123 }
124 }
125
126 closeFileDescriptor(client_sock);
127 closeFileDescriptor(server_sock);
128
129 return nullptr;
130 }
131
setupRoute(int cid,const android::automotive::proxyconfig::Service & service)132 void setupRoute(int cid, const android::automotive::proxyconfig::Service& service) {
133
134 sockaddr_vm addr{};
135 addr.svm_family = AF_VSOCK;
136 addr.svm_cid = 2;
137 addr.svm_port = service.port;
138
139 int fwd_cid = cid;
140 int fwd_port = service.port;
141
142 int proxy_socket = setupServerSocket(addr);
143
144 if (proxy_socket == -1) {
145 std::cerr << "Failed to set up proxy server VSOCK socket, ERROR = " <<
146 strerror(errno) << std::endl;
147 return;
148 }
149
150 int client_sock;
151 int len = sizeof(addr);
152
153 while (true) {
154 if ((client_sock = accept(proxy_socket, reinterpret_cast<sockaddr*>(&addr),
155 reinterpret_cast<socklen_t*>(&len))) < 0) {
156 std::cerr << "Failed to accept VSOCK connection, ERROR = " <<
157 strerror(errno) << std::endl;
158 closeFileDescriptor(client_sock);
159 continue;
160 }
161
162 std::thread t(handleConnection, client_sock, fwd_cid, fwd_port);
163 t.detach();
164 }
165
166 closeFileDescriptor(proxy_socket);
167 }
168
169
170 static constexpr const char *kProxyConfigFile =
171 "../etc/automotive/proxy_config.json";
172
main(int argc,char ** argv)173 int main(int argc, char **argv ) {
174 android::automotive::proxyconfig::setProxyConfigFile(
175 (argc >= 2)?argv[1]:kProxyConfigFile);
176
177 auto vmConfigs = android::automotive::proxyconfig::getAllVmProxyConfigs();
178
179 std::vector<std::thread> routeThreads;
180 for (const auto& vmConfig: vmConfigs) {
181 for (const auto& service: vmConfig.services) {
182 routeThreads.push_back(std::thread(setupRoute, vmConfig.cid, service));
183 }
184 }
185
186 for(auto& t: routeThreads) {
187 t.join();
188 }
189
190 return 0;
191 }
192