blob: a76f49a064159ce17b7beb9531d6aa4f2e08c841 [file] [log] [blame]
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -07001/*
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
Paul Jensend1df5972015-05-06 07:29:56 -040019#include "FwmarkCommand.h"
20
Elliott Hughes27aacc02014-12-29 11:48:03 -080021#include <errno.h>
Michal Karpinski6028b392017-01-24 13:34:32 +000022#include <cutils/properties.h>
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070023#include <stdlib.h>
Elliott Hughesbb881e22015-01-28 11:22:06 -080024#include <string.h>
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070025#include <sys/socket.h>
26#include <sys/un.h>
27#include <unistd.h>
28
Michal Karpinski7d374532016-10-06 19:33:55 +010029#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
30
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070031namespace {
32
33const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"};
34
Michal Karpinski6028b392017-01-24 13:34:32 +000035const bool isBuildDebuggable = property_get_bool("ro.debuggable", 0) == 1;
36
37bool isOverriddenBy(const char *name) {
38 return isBuildDebuggable && getenv(name);
39}
40
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070041} // namespace
42
Sreeram Ramachandran5fc27572014-05-21 13:08:34 -070043bool FwmarkClient::shouldSetFwmark(int family) {
Michal Karpinski6028b392017-01-24 13:34:32 +000044 if (isOverriddenBy(ANDROID_NO_USE_FWMARK_CLIENT)) {
45 return false;
46 }
47 return family == AF_INET || family == AF_INET6;
Robin Lee2cf56172016-09-13 18:55:42 +090048}
49
50bool FwmarkClient::shouldReportConnectComplete(int family) {
Michal Karpinski6028b392017-01-24 13:34:32 +000051 if (isOverriddenBy(ANDROID_FWMARK_METRICS_ONLY)) {
52 return false;
53 }
54 return shouldSetFwmark(family);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070055}
56
57FwmarkClient::FwmarkClient() : mChannel(-1) {
58}
59
60FwmarkClient::~FwmarkClient() {
61 if (mChannel >= 0) {
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070062 close(mChannel);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070063 }
64}
65
Michal Karpinski7d374532016-10-06 19:33:55 +010066int FwmarkClient::send(FwmarkCommand* data, int fd, FwmarkConnectInfo* connectInfo) {
Nick Kralevich53ea9ca2015-01-31 13:54:00 -080067 mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070068 if (mChannel == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070069 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070070 }
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.
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070076 return 0;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070077 }
78
Michal Karpinski7d374532016-10-06 19:33:55 +010079 iovec iov[2] = {
80 { data, sizeof(*data) },
81 { connectInfo, (connectInfo ? sizeof(*connectInfo) : 0) },
82 };
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070083 msghdr message;
84 memset(&message, 0, sizeof(message));
Michal Karpinski7d374532016-10-06 19:33:55 +010085 message.msg_iov = iov;
86 message.msg_iovlen = ARRAY_SIZE(iov);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070087
Stephen Hinesf86df552015-08-17 17:16:59 -070088 union {
89 cmsghdr cmh;
90 char cmsg[CMSG_SPACE(sizeof(fd))];
91 } cmsgu;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070092
Stephen Hinesf86df552015-08-17 17:16:59 -070093 if (data->cmdId != FwmarkCommand::QUERY_USER_ACCESS) {
Paul Jensend1df5972015-05-06 07:29:56 -040094 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
95 message.msg_control = cmsgu.cmsg;
96 message.msg_controllen = sizeof(cmsgu.cmsg);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070097
Paul Jensend1df5972015-05-06 07:29:56 -040098 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 }
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700104
105 if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700106 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700107 }
108
109 int error = 0;
Sreeram Ramachandran31f42102014-06-20 11:51:48 -0700110
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700111 if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700112 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700113 }
114
Sreeram Ramachandran31f42102014-06-20 11:51:48 -0700115 return error;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700116}