1 /*
2 * Copyright (C) 2014 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 "FwmarkClient.h"
18
19 #include "FwmarkCommand.h"
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27
28 #include <algorithm> // std::size()
29 #include <iterator>
30
31 namespace {
32
33 // Env flag to control whether FwmarkClient sends sockets to netd for marking.
34 // This can only be disabled in debuggable builds and is meant for kernel testing.
35 inline constexpr char ANDROID_NO_USE_FWMARK_CLIENT[] = "ANDROID_NO_USE_FWMARK_CLIENT";
36
37 const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"};
38
39 #if defined(NETD_CLIENT_DEBUGGABLE_BUILD)
40 constexpr bool isBuildDebuggable = true;
41 #else
42 constexpr bool isBuildDebuggable = false;
43 #endif
44
isOverriddenBy(const char * name)45 bool isOverriddenBy(const char *name) {
46 return isBuildDebuggable && getenv(name);
47 }
48
commandHasFd(int cmdId)49 bool commandHasFd(int cmdId) {
50 return (cmdId != FwmarkCommand::QUERY_USER_ACCESS) &&
51 (cmdId != FwmarkCommand::SET_COUNTERSET) &&
52 (cmdId != FwmarkCommand::DELETE_TAGDATA);
53 }
54
55 } // namespace
56
shouldSetFwmark(int family)57 bool FwmarkClient::shouldSetFwmark(int family) {
58 if (isOverriddenBy(ANDROID_NO_USE_FWMARK_CLIENT)) return false;
59 return FwmarkCommand::isSupportedFamily(family);
60 }
61
shouldReportConnectComplete(int family)62 bool FwmarkClient::shouldReportConnectComplete(int family) {
63 if (isOverriddenBy(ANDROID_NO_USE_FWMARK_CLIENT)) return false;
64 return shouldSetFwmark(family);
65 }
66
FwmarkClient()67 FwmarkClient::FwmarkClient() : mChannel(-1) {
68 }
69
~FwmarkClient()70 FwmarkClient::~FwmarkClient() {
71 if (mChannel >= 0) {
72 close(mChannel);
73 }
74 }
75
send(FwmarkCommand * data,int fd,FwmarkConnectInfo * connectInfo)76 int FwmarkClient::send(FwmarkCommand* data, int fd, FwmarkConnectInfo* connectInfo) {
77 mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
78 if (mChannel == -1) {
79 return -errno;
80 }
81
82 if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH),
83 sizeof(FWMARK_SERVER_PATH))) == -1) {
84 // If we are unable to connect to the fwmark server, assume there's no error. This protects
85 // against future changes if the fwmark server goes away.
86 // TODO: This means that fd will very likely be misrouted. See if we can delete this in a
87 // separate CL.
88 return 0;
89 }
90
91 iovec iov[2] = {
92 { data, sizeof(*data) },
93 { connectInfo, (connectInfo ? sizeof(*connectInfo) : 0) },
94 };
95 msghdr message;
96 memset(&message, 0, sizeof(message));
97 message.msg_iov = iov;
98 message.msg_iovlen = std::size(iov);
99
100 union {
101 cmsghdr cmh;
102 char cmsg[CMSG_SPACE(sizeof(fd))];
103 } cmsgu;
104
105 if (commandHasFd(data->cmdId)) {
106 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
107 message.msg_control = cmsgu.cmsg;
108 message.msg_controllen = sizeof(cmsgu.cmsg);
109
110 cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
111 cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
112 cmsgh->cmsg_level = SOL_SOCKET;
113 cmsgh->cmsg_type = SCM_RIGHTS;
114 memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd));
115 }
116
117 if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
118 return -errno;
119 }
120
121 int error = 0;
122
123 if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
124 return -errno;
125 }
126
127 return error;
128 }
129