blob: cc4893dbefdc2942761d927a6438d2d3071f456d [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
Bernie Innocenticd257642018-12-20 15:56:40 +090028#include <algorithm> // std::size()
Bernie Innocenti45238a12018-12-04 14:57:48 +090029#include <iterator>
Michal Karpinski7d374532016-10-06 19:33:55 +010030
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070031namespace {
32
Bernie Innocenti1dc863b2019-04-10 19:24:11 +090033// Env flag to control whether FwmarkClient sends sockets to netd for marking.
34// This can only be disabled in debuggable builds and is meant for kernel testing.
35inline constexpr char ANDROID_NO_USE_FWMARK_CLIENT[] = "ANDROID_NO_USE_FWMARK_CLIENT";
36
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070037const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"};
38
Bernie Innocenti1dc863b2019-04-10 19:24:11 +090039#if defined(NETD_CLIENT_DEBUGGABLE_BUILD)
40constexpr bool isBuildDebuggable = true;
41#else
42constexpr bool isBuildDebuggable = false;
43#endif
44
45bool isOverriddenBy(const char *name) {
46 return isBuildDebuggable && getenv(name);
47}
48
Chenbo Feng9944ba82017-10-10 17:33:20 -070049bool commandHasFd(int cmdId) {
50 return (cmdId != FwmarkCommand::QUERY_USER_ACCESS) &&
51 (cmdId != FwmarkCommand::SET_COUNTERSET) &&
52 (cmdId != FwmarkCommand::DELETE_TAGDATA);
53}
54
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070055} // namespace
56
Sreeram Ramachandran5fc27572014-05-21 13:08:34 -070057bool FwmarkClient::shouldSetFwmark(int family) {
Bernie Innocenti1dc863b2019-04-10 19:24:11 +090058 if (isOverriddenBy(ANDROID_NO_USE_FWMARK_CLIENT)) return false;
Remi NGUYEN VANeabc5da2018-04-24 18:54:58 +090059 return FwmarkCommand::isSupportedFamily(family);
Robin Lee2cf56172016-09-13 18:55:42 +090060}
61
62bool FwmarkClient::shouldReportConnectComplete(int family) {
Bernie Innocenti1dc863b2019-04-10 19:24:11 +090063 if (isOverriddenBy(ANDROID_NO_USE_FWMARK_CLIENT)) return false;
Michal Karpinski6028b392017-01-24 13:34:32 +000064 return shouldSetFwmark(family);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070065}
66
67FwmarkClient::FwmarkClient() : mChannel(-1) {
68}
69
70FwmarkClient::~FwmarkClient() {
71 if (mChannel >= 0) {
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070072 close(mChannel);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070073 }
74}
75
Michal Karpinski7d374532016-10-06 19:33:55 +010076int FwmarkClient::send(FwmarkCommand* data, int fd, FwmarkConnectInfo* connectInfo) {
Nick Kralevich53ea9ca2015-01-31 13:54:00 -080077 mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070078 if (mChannel == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070079 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070080 }
81
82 if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH),
83 sizeof(FWMARK_SERVER_PATH))) == -1) {
84 // If we are unable to connect to the fwmark server, assume there's no error. This protects
85 // against future changes if the fwmark server goes away.
Chenbo Feng9944ba82017-10-10 17:33:20 -070086 // TODO: This means that fd will very likely be misrouted. See if we can delete this in a
87 // separate CL.
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070088 return 0;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070089 }
90
Michal Karpinski7d374532016-10-06 19:33:55 +010091 iovec iov[2] = {
92 { data, sizeof(*data) },
93 { connectInfo, (connectInfo ? sizeof(*connectInfo) : 0) },
94 };
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070095 msghdr message;
96 memset(&message, 0, sizeof(message));
Michal Karpinski7d374532016-10-06 19:33:55 +010097 message.msg_iov = iov;
Bernie Innocenti45238a12018-12-04 14:57:48 +090098 message.msg_iovlen = std::size(iov);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070099
Stephen Hinesf86df552015-08-17 17:16:59 -0700100 union {
101 cmsghdr cmh;
102 char cmsg[CMSG_SPACE(sizeof(fd))];
103 } cmsgu;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700104
Chenbo Feng9944ba82017-10-10 17:33:20 -0700105 if (commandHasFd(data->cmdId)) {
Paul Jensend1df5972015-05-06 07:29:56 -0400106 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
107 message.msg_control = cmsgu.cmsg;
108 message.msg_controllen = sizeof(cmsgu.cmsg);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700109
Paul Jensend1df5972015-05-06 07:29:56 -0400110 cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
111 cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
112 cmsgh->cmsg_level = SOL_SOCKET;
113 cmsgh->cmsg_type = SCM_RIGHTS;
114 memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd));
115 }
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700116
117 if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700118 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700119 }
120
121 int error = 0;
Sreeram Ramachandran31f42102014-06-20 11:51:48 -0700122
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700123 if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -0700124 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700125 }
126
Sreeram Ramachandran31f42102014-06-20 11:51:48 -0700127 return error;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700128}