blob: 5074a7860dbedb601750fc91ce8a1db5d923d0f2 [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
28namespace {
29
30const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"};
31
32} // namespace
33
Sreeram Ramachandran5fc27572014-05-21 13:08:34 -070034bool FwmarkClient::shouldSetFwmark(int family) {
35 return (family == AF_INET || family == AF_INET6) && !getenv("ANDROID_NO_USE_FWMARK_CLIENT");
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070036}
37
38FwmarkClient::FwmarkClient() : mChannel(-1) {
39}
40
41FwmarkClient::~FwmarkClient() {
42 if (mChannel >= 0) {
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070043 close(mChannel);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070044 }
45}
46
Paul Jensend1df5972015-05-06 07:29:56 -040047int FwmarkClient::send(FwmarkCommand* data, int fd) {
Nick Kralevich53ea9ca2015-01-31 13:54:00 -080048 mChannel = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070049 if (mChannel == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070050 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070051 }
52
53 if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH),
54 sizeof(FWMARK_SERVER_PATH))) == -1) {
55 // If we are unable to connect to the fwmark server, assume there's no error. This protects
56 // against future changes if the fwmark server goes away.
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070057 return 0;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070058 }
59
60 iovec iov;
61 iov.iov_base = data;
Paul Jensend1df5972015-05-06 07:29:56 -040062 iov.iov_len = sizeof(*data);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070063
64 msghdr message;
65 memset(&message, 0, sizeof(message));
66 message.msg_iov = &iov;
67 message.msg_iovlen = 1;
68
Paul Jensend1df5972015-05-06 07:29:56 -040069 if (data->cmdId != FwmarkCommand::QUERY_USER_ACCESS) {
70 union {
71 cmsghdr cmh;
72 char cmsg[CMSG_SPACE(sizeof(fd))];
73 } cmsgu;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070074
Paul Jensend1df5972015-05-06 07:29:56 -040075 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
76 message.msg_control = cmsgu.cmsg;
77 message.msg_controllen = sizeof(cmsgu.cmsg);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070078
Paul Jensend1df5972015-05-06 07:29:56 -040079 cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
80 cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
81 cmsgh->cmsg_level = SOL_SOCKET;
82 cmsgh->cmsg_type = SCM_RIGHTS;
83 memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd));
84 }
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070085
86 if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070087 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070088 }
89
90 int error = 0;
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070091
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070092 if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070093 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070094 }
95
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070096 return error;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070097}