2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "NetdClient.h"
20 #include <sys/socket.h>
26 #include "FwmarkClient.h"
27 #include "FwmarkCommand.h"
28 #include "resolv_netid.h"
32 std::atomic_uint netIdForProcess(NETID_UNSET);
33 std::atomic_uint netIdForResolv(NETID_UNSET);
35 typedef int (*Accept4FunctionType)(int, sockaddr*, socklen_t*, int);
36 typedef int (*ConnectFunctionType)(int, const sockaddr*, socklen_t);
37 typedef int (*SocketFunctionType)(int, int, int);
38 typedef unsigned (*NetIdForResolvFunctionType)(unsigned);
40 // These variables are only modified at startup (when libc.so is loaded) and never afterwards, so
41 // it's okay that they are read later at runtime without a lock.
42 Accept4FunctionType libcAccept4 = 0;
43 ConnectFunctionType libcConnect = 0;
44 SocketFunctionType libcSocket = 0;
46 int closeFdAndSetErrno(int fd, int error) {
52 int netdClientAccept4(int sockfd, sockaddr* addr, socklen_t* addrlen, int flags) {
53 int acceptedSocket = libcAccept4(sockfd, addr, addrlen, flags);
54 if (acceptedSocket == -1) {
59 family = addr->sa_family;
61 socklen_t familyLen = sizeof(family);
62 if (getsockopt(acceptedSocket, SOL_SOCKET, SO_DOMAIN, &family, &familyLen) == -1) {
63 return closeFdAndSetErrno(acceptedSocket, -errno);
66 if (FwmarkClient::shouldSetFwmark(family)) {
67 FwmarkCommand command = {FwmarkCommand::ON_ACCEPT, 0, 0};
68 if (int error = FwmarkClient().send(&command, sizeof(command), acceptedSocket)) {
69 return closeFdAndSetErrno(acceptedSocket, error);
72 return acceptedSocket;
75 int netdClientConnect(int sockfd, const sockaddr* addr, socklen_t addrlen) {
76 if (sockfd >= 0 && addr && FwmarkClient::shouldSetFwmark(addr->sa_family)) {
77 FwmarkCommand command = {FwmarkCommand::ON_CONNECT, 0, 0};
78 if (int error = FwmarkClient().send(&command, sizeof(command), sockfd)) {
83 return libcConnect(sockfd, addr, addrlen);
86 int netdClientSocket(int domain, int type, int protocol) {
87 int socketFd = libcSocket(domain, type, protocol);
91 unsigned netId = netIdForProcess;
92 if (netId != NETID_UNSET && FwmarkClient::shouldSetFwmark(domain)) {
93 if (int error = setNetworkForSocket(netId, socketFd)) {
94 return closeFdAndSetErrno(socketFd, error);
100 unsigned getNetworkForResolv(unsigned netId) {
101 if (netId != NETID_UNSET) {
104 netId = netIdForProcess;
105 if (netId != NETID_UNSET) {
108 return netIdForResolv;
111 int setNetworkForTarget(unsigned netId, std::atomic_uint* target) {
112 if (netId == NETID_UNSET) {
116 // Verify that we are allowed to use |netId|, by creating a socket and trying to have it marked
117 // with the netId. Call libcSocket() directly; else the socket creation (via netdClientSocket())
118 // might itself cause another check with the fwmark server, which would be wasteful.
121 socketFd = libcSocket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
123 socketFd = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
128 int error = setNetworkForSocket(netId, socketFd);
138 // accept() just calls accept4(..., 0), so there's no need to handle accept() separately.
139 extern "C" void netdClientInitAccept4(Accept4FunctionType* function) {
140 if (function && *function) {
141 libcAccept4 = *function;
142 *function = netdClientAccept4;
146 extern "C" void netdClientInitConnect(ConnectFunctionType* function) {
147 if (function && *function) {
148 libcConnect = *function;
149 *function = netdClientConnect;
153 extern "C" void netdClientInitSocket(SocketFunctionType* function) {
154 if (function && *function) {
155 libcSocket = *function;
156 *function = netdClientSocket;
160 extern "C" void netdClientInitNetIdForResolv(NetIdForResolvFunctionType* function) {
162 *function = getNetworkForResolv;
166 extern "C" int getNetworkForSocket(unsigned* netId, int socketFd) {
167 if (!netId || socketFd < 0) {
171 socklen_t fwmarkLen = sizeof(fwmark.intValue);
172 if (getsockopt(socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) {
175 *netId = fwmark.netId;
179 extern "C" unsigned getNetworkForProcess() {
180 return netIdForProcess;
183 extern "C" int setNetworkForSocket(unsigned netId, int socketFd) {
187 FwmarkCommand command = {FwmarkCommand::SELECT_NETWORK, netId, 0};
188 return FwmarkClient().send(&command, sizeof(command), socketFd);
191 extern "C" int setNetworkForProcess(unsigned netId) {
192 return setNetworkForTarget(netId, &netIdForProcess);
195 extern "C" int setNetworkForResolv(unsigned netId) {
196 return setNetworkForTarget(netId, &netIdForResolv);
199 extern "C" int protectFromVpn(int socketFd) {
203 FwmarkCommand command = {FwmarkCommand::PROTECT_FROM_VPN, 0, 0};
204 return FwmarkClient().send(&command, sizeof(command), socketFd);
207 extern "C" int setNetworkForUser(uid_t uid, int socketFd) {
211 FwmarkCommand command = {FwmarkCommand::SELECT_FOR_USER, 0, uid};
212 return FwmarkClient().send(&command, sizeof(command), socketFd);