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 | |
| 28 | namespace { |
| 29 | |
| 30 | const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"}; |
| 31 | |
| 32 | } // namespace |
| 33 | |
Sreeram Ramachandran | 5fc2757 | 2014-05-21 13:08:34 -0700 | [diff] [blame] | 34 | bool FwmarkClient::shouldSetFwmark(int family) { |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame^] | 35 | return (family == AF_INET || family == AF_INET6) && !getenv(ANDROID_NO_USE_FWMARK_CLIENT); |
| 36 | } |
| 37 | |
| 38 | bool FwmarkClient::shouldReportConnectComplete(int family) { |
| 39 | return shouldSetFwmark(family) && !getenv(ANDROID_FWMARK_METRICS_ONLY); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | FwmarkClient::FwmarkClient() : mChannel(-1) { |
| 43 | } |
| 44 | |
| 45 | FwmarkClient::~FwmarkClient() { |
| 46 | if (mChannel >= 0) { |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 47 | close(mChannel); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Stephen Hines | 996f037 | 2015-08-17 17:16:59 -0700 | [diff] [blame] | 51 | int FwmarkClient::send(FwmarkCommand* data, int fd) { |
Nick Kralevich | 53ea9ca | 2015-01-31 13:54:00 -0800 | [diff] [blame] | 52 | mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 53 | if (mChannel == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 54 | return -errno; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH), |
| 58 | sizeof(FWMARK_SERVER_PATH))) == -1) { |
| 59 | // If we are unable to connect to the fwmark server, assume there's no error. This protects |
| 60 | // against future changes if the fwmark server goes away. |
Sreeram Ramachandran | 31f4210 | 2014-06-20 11:51:48 -0700 | [diff] [blame] | 61 | return 0; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | iovec iov; |
| 65 | iov.iov_base = data; |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 66 | iov.iov_len = sizeof(*data); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 67 | |
| 68 | msghdr message; |
| 69 | memset(&message, 0, sizeof(message)); |
| 70 | message.msg_iov = &iov; |
| 71 | message.msg_iovlen = 1; |
| 72 | |
Stephen Hines | f86df55 | 2015-08-17 17:16:59 -0700 | [diff] [blame] | 73 | union { |
| 74 | cmsghdr cmh; |
| 75 | char cmsg[CMSG_SPACE(sizeof(fd))]; |
| 76 | } cmsgu; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 77 | |
Stephen Hines | f86df55 | 2015-08-17 17:16:59 -0700 | [diff] [blame] | 78 | if (data->cmdId != FwmarkCommand::QUERY_USER_ACCESS) { |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 79 | memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg)); |
| 80 | message.msg_control = cmsgu.cmsg; |
| 81 | message.msg_controllen = sizeof(cmsgu.cmsg); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 82 | |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 83 | cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message); |
| 84 | cmsgh->cmsg_len = CMSG_LEN(sizeof(fd)); |
| 85 | cmsgh->cmsg_level = SOL_SOCKET; |
| 86 | cmsgh->cmsg_type = SCM_RIGHTS; |
| 87 | memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd)); |
| 88 | } |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 89 | |
| 90 | if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 91 | return -errno; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int error = 0; |
Sreeram Ramachandran | 31f4210 | 2014-06-20 11:51:48 -0700 | [diff] [blame] | 95 | |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 96 | if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 97 | return -errno; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Sreeram Ramachandran | 31f4210 | 2014-06-20 11:51:48 -0700 | [diff] [blame] | 100 | return error; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 101 | } |