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