blob: 943552846ba87fcd132e060b07772151260b86b2 [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>
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070022#include <stdlib.h>
Elliott Hughesbb881e22015-01-28 11:22:06 -080023#include <string.h>
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070024#include <sys/socket.h>
25#include <sys/un.h>
26#include <unistd.h>
27
Michal Karpinski7d374532016-10-06 19:33:55 +010028#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
29
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070030namespace {
31
32const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"};
33
dimitrye54b2122017-09-18 16:52:45 +020034#if defined(NETD_CLIENT_DEBUGGABLE_BUILD)
35constexpr bool isBuildDebuggable = true;
36#else
37constexpr bool isBuildDebuggable = false;
38#endif
Michal Karpinski6028b392017-01-24 13:34:32 +000039
40bool isOverriddenBy(const char *name) {
41 return isBuildDebuggable && getenv(name);
42}
43
Chenbo Feng9944ba82017-10-10 17:33:20 -070044bool commandHasFd(int cmdId) {
45 return (cmdId != FwmarkCommand::QUERY_USER_ACCESS) &&
46 (cmdId != FwmarkCommand::SET_COUNTERSET) &&
47 (cmdId != FwmarkCommand::DELETE_TAGDATA);
48}
49
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070050} // namespace
51
Sreeram Ramachandran5fc27572014-05-21 13:08:34 -070052bool FwmarkClient::shouldSetFwmark(int family) {
Michal Karpinski6028b392017-01-24 13:34:32 +000053 if (isOverriddenBy(ANDROID_NO_USE_FWMARK_CLIENT)) {
54 return false;
55 }
Remi NGUYEN VANeabc5da2018-04-24 18:54:58 +090056 return FwmarkCommand::isSupportedFamily(family);
Robin Lee2cf56172016-09-13 18:55:42 +090057}
58
59bool FwmarkClient::shouldReportConnectComplete(int family) {
Michal Karpinski6028b392017-01-24 13:34:32 +000060 if (isOverriddenBy(ANDROID_FWMARK_METRICS_ONLY)) {
61 return false;
62 }
63 return shouldSetFwmark(family);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070064}
65
66FwmarkClient::FwmarkClient() : mChannel(-1) {
67}
68
69FwmarkClient::~FwmarkClient() {
70 if (mChannel >= 0) {
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070071 close(mChannel);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070072 }
73}
74
Michal Karpinski7d374532016-10-06 19:33:55 +010075int FwmarkClient::send(FwmarkCommand* data, int fd, FwmarkConnectInfo* connectInfo) {
Nick Kralevich53ea9ca2015-01-31 13:54:00 -080076 mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070077 if (mChannel == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070078 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070079 }
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.
Chenbo Feng9944ba82017-10-10 17:33:20 -070085 // TODO: This means that fd will very likely be misrouted. See if we can delete this in a
86 // separate CL.
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070087 return 0;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070088 }
89
Michal Karpinski7d374532016-10-06 19:33:55 +010090 iovec iov[2] = {
91 { data, sizeof(*data) },
92 { connectInfo, (connectInfo ? sizeof(*connectInfo) : 0) },
93 };
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070094 msghdr message;
95 memset(&message, 0, sizeof(message));
Michal Karpinski7d374532016-10-06 19:33:55 +010096 message.msg_iov = iov;
97 message.msg_iovlen = ARRAY_SIZE(iov);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070098
Stephen Hinesf86df552015-08-17 17:16:59 -070099 union {
100 cmsghdr cmh;
101 char cmsg[CMSG_SPACE(sizeof(fd))];
102 } cmsgu;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700103
Chenbo Feng9944ba82017-10-10 17:33:20 -0700104 if (commandHasFd(data->cmdId)) {
Paul Jensend1df5972015-05-06 07:29:56 -0400105 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
106 message.msg_control = cmsgu.cmsg;
107 message.msg_controllen = sizeof(cmsgu.cmsg);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700108
Paul Jensend1df5972015-05-06 07:29:56 -0400109 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 }
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700115
116 if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700117 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700118 }
119
120 int error = 0;
Sreeram Ramachandran31f42102014-06-20 11:51:48 -0700121
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700122 if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700123 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700124 }
125
Sreeram Ramachandran31f42102014-06-20 11:51:48 -0700126 return error;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700127}