Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 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 | |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 19 | #include "FwmarkCommand.h" |
| 20 | |
Elliott Hughes | 27aacc0 | 2014-12-29 11:48:03 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Elliott Hughes | bb881e2 | 2015-01-28 11:22:06 -0800 | [diff] [blame] | 23 | #include <string.h> |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <sys/un.h> |
| 26 | #include <unistd.h> |
| 27 | |
Bernie Innocenti | cd25764 | 2018-12-20 15:56:40 +0900 | [diff] [blame] | 28 | #include <algorithm> // std::size() |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 29 | #include <iterator> |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 30 | |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 31 | namespace { |
| 32 | |
Bernie Innocenti | 1dc863b | 2019-04-10 19:24:11 +0900 | [diff] [blame] | 33 | // 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. |
| 35 | inline constexpr char ANDROID_NO_USE_FWMARK_CLIENT[] = "ANDROID_NO_USE_FWMARK_CLIENT"; |
| 36 | |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 37 | const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"}; |
| 38 | |
Bernie Innocenti | 1dc863b | 2019-04-10 19:24:11 +0900 | [diff] [blame] | 39 | #if defined(NETD_CLIENT_DEBUGGABLE_BUILD) |
| 40 | constexpr bool isBuildDebuggable = true; |
| 41 | #else |
| 42 | constexpr bool isBuildDebuggable = false; |
| 43 | #endif |
| 44 | |
| 45 | bool isOverriddenBy(const char *name) { |
| 46 | return isBuildDebuggable && getenv(name); |
| 47 | } |
| 48 | |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 49 | bool commandHasFd(int cmdId) { |
| 50 | return (cmdId != FwmarkCommand::QUERY_USER_ACCESS) && |
| 51 | (cmdId != FwmarkCommand::SET_COUNTERSET) && |
| 52 | (cmdId != FwmarkCommand::DELETE_TAGDATA); |
| 53 | } |
| 54 | |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 55 | } // namespace |
| 56 | |
Sreeram Ramachandran | 5fc2757 | 2014-05-21 13:08:34 -0700 | [diff] [blame] | 57 | bool FwmarkClient::shouldSetFwmark(int family) { |
Bernie Innocenti | 1dc863b | 2019-04-10 19:24:11 +0900 | [diff] [blame] | 58 | if (isOverriddenBy(ANDROID_NO_USE_FWMARK_CLIENT)) return false; |
Remi NGUYEN VAN | eabc5da | 2018-04-24 18:54:58 +0900 | [diff] [blame] | 59 | return FwmarkCommand::isSupportedFamily(family); |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool FwmarkClient::shouldReportConnectComplete(int family) { |
Bernie Innocenti | 1dc863b | 2019-04-10 19:24:11 +0900 | [diff] [blame] | 63 | if (isOverriddenBy(ANDROID_NO_USE_FWMARK_CLIENT)) return false; |
Michal Karpinski | 6028b39 | 2017-01-24 13:34:32 +0000 | [diff] [blame] | 64 | return shouldSetFwmark(family); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | FwmarkClient::FwmarkClient() : mChannel(-1) { |
| 68 | } |
| 69 | |
| 70 | FwmarkClient::~FwmarkClient() { |
| 71 | if (mChannel >= 0) { |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 72 | close(mChannel); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 76 | int FwmarkClient::send(FwmarkCommand* data, int fd, FwmarkConnectInfo* connectInfo) { |
Nick Kralevich | 53ea9ca | 2015-01-31 13:54:00 -0800 | [diff] [blame] | 77 | mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 78 | if (mChannel == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 79 | return -errno; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 80 | } |
| 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 Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 86 | // TODO: This means that fd will very likely be misrouted. See if we can delete this in a |
| 87 | // separate CL. |
Sreeram Ramachandran | 31f4210 | 2014-06-20 11:51:48 -0700 | [diff] [blame] | 88 | return 0; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 91 | iovec iov[2] = { |
| 92 | { data, sizeof(*data) }, |
| 93 | { connectInfo, (connectInfo ? sizeof(*connectInfo) : 0) }, |
| 94 | }; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 95 | msghdr message; |
| 96 | memset(&message, 0, sizeof(message)); |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 97 | message.msg_iov = iov; |
Bernie Innocenti | 45238a1 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 98 | message.msg_iovlen = std::size(iov); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 99 | |
Stephen Hines | f86df55 | 2015-08-17 17:16:59 -0700 | [diff] [blame] | 100 | union { |
| 101 | cmsghdr cmh; |
| 102 | char cmsg[CMSG_SPACE(sizeof(fd))]; |
| 103 | } cmsgu; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 104 | |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 105 | if (commandHasFd(data->cmdId)) { |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 106 | memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg)); |
| 107 | message.msg_control = cmsgu.cmsg; |
| 108 | message.msg_controllen = sizeof(cmsgu.cmsg); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 109 | |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 110 | 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 Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 116 | |
| 117 | if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 118 | return -errno; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int error = 0; |
Sreeram Ramachandran | 31f4210 | 2014-06-20 11:51:48 -0700 | [diff] [blame] | 122 | |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 123 | if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 124 | return -errno; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Sreeram Ramachandran | 31f4210 | 2014-06-20 11:51:48 -0700 | [diff] [blame] | 127 | return error; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 128 | } |