• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }  // namespace
35 
shouldSetFwmark(int family)36 bool FwmarkClient::shouldSetFwmark(int family) {
37     return (family == AF_INET || family == AF_INET6) && !getenv(ANDROID_NO_USE_FWMARK_CLIENT);
38 }
39 
shouldReportConnectComplete(int family)40 bool FwmarkClient::shouldReportConnectComplete(int family) {
41     // TODO: put getenv(ANDROID_FWMARK_METRICS_ONLY) behind the ro.debuggable system property
42     // or else an app can evade connect logging just by setting the env variable
43     return shouldSetFwmark(family) && !getenv(ANDROID_FWMARK_METRICS_ONLY);
44 }
45 
FwmarkClient()46 FwmarkClient::FwmarkClient() : mChannel(-1) {
47 }
48 
~FwmarkClient()49 FwmarkClient::~FwmarkClient() {
50     if (mChannel >= 0) {
51         close(mChannel);
52     }
53 }
54 
send(FwmarkCommand * data,int fd,FwmarkConnectInfo * connectInfo)55 int FwmarkClient::send(FwmarkCommand* data, int fd, FwmarkConnectInfo* connectInfo) {
56     mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
57     if (mChannel == -1) {
58         return -errno;
59     }
60 
61     if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH),
62                                    sizeof(FWMARK_SERVER_PATH))) == -1) {
63         // If we are unable to connect to the fwmark server, assume there's no error. This protects
64         // against future changes if the fwmark server goes away.
65         return 0;
66     }
67 
68     iovec iov[2] = {
69         { data, sizeof(*data) },
70         { connectInfo, (connectInfo ? sizeof(*connectInfo) : 0) },
71     };
72     msghdr message;
73     memset(&message, 0, sizeof(message));
74     message.msg_iov = iov;
75     message.msg_iovlen = ARRAY_SIZE(iov);
76 
77     union {
78         cmsghdr cmh;
79         char cmsg[CMSG_SPACE(sizeof(fd))];
80     } cmsgu;
81 
82     if (data->cmdId != FwmarkCommand::QUERY_USER_ACCESS) {
83         memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
84         message.msg_control = cmsgu.cmsg;
85         message.msg_controllen = sizeof(cmsgu.cmsg);
86 
87         cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
88         cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
89         cmsgh->cmsg_level = SOL_SOCKET;
90         cmsgh->cmsg_type = SCM_RIGHTS;
91         memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd));
92     }
93 
94     if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
95         return -errno;
96     }
97 
98     int error = 0;
99 
100     if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
101         return -errno;
102     }
103 
104     return error;
105 }
106