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