Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -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 "FwmarkServer.h" |
| 18 | |
| 19 | #include "Fwmark.h" |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 20 | #include "FwmarkCommand.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 21 | #include "NetworkController.h" |
| 22 | #include "PermissionsController.h" |
| 23 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <unistd.h> |
| 26 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 27 | FwmarkServer::FwmarkServer(NetworkController* networkController, |
| 28 | PermissionsController* permissionsController) |
| 29 | : SocketListener("fwmarkd", true), |
| 30 | mNetworkController(networkController), |
| 31 | mPermissionsController(permissionsController) { |
| 32 | } |
| 33 | |
| 34 | bool FwmarkServer::onDataAvailable(SocketClient* client) { |
| 35 | int fd = -1; |
| 36 | processClient(client, &fd); |
| 37 | int error = errno; |
| 38 | if (fd >= 0) { |
| 39 | close(fd); |
| 40 | } |
| 41 | |
| 42 | // Always send a response even if there were connection errors or read errors, so that we don't |
| 43 | // inadvertently cause the client to hang (which always waits for a response). |
| 44 | client->sendData(&error, sizeof(error)); |
| 45 | |
| 46 | // Always close the client connection (by returning false). This prevents a DoS attack where |
| 47 | // the client issues multiple commands on the same connection, never reading the responses, |
| 48 | // causing its receive buffer to fill up, and thus causing our client->sendData() to block. |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | void FwmarkServer::processClient(SocketClient* client, int* fd) { |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame^] | 53 | FwmarkCommand command; |
| 54 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 55 | iovec iov; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame^] | 56 | iov.iov_base = &command; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 57 | iov.iov_len = sizeof(command); |
| 58 | |
| 59 | msghdr message; |
| 60 | memset(&message, 0, sizeof(message)); |
| 61 | message.msg_iov = &iov; |
| 62 | message.msg_iovlen = 1; |
| 63 | |
| 64 | union { |
| 65 | cmsghdr cmh; |
| 66 | char cmsg[CMSG_SPACE(sizeof(*fd))]; |
| 67 | } cmsgu; |
| 68 | |
| 69 | memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg)); |
| 70 | message.msg_control = cmsgu.cmsg; |
| 71 | message.msg_controllen = sizeof(cmsgu.cmsg); |
| 72 | |
| 73 | int messageLength = TEMP_FAILURE_RETRY(recvmsg(client->getSocket(), &message, 0)); |
| 74 | if (messageLength <= 0) { |
| 75 | return; |
| 76 | } |
| 77 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame^] | 78 | if (messageLength != sizeof(command)) { |
| 79 | errno = EINVAL; |
| 80 | return; |
| 81 | } |
| 82 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 83 | cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message); |
| 84 | if (cmsgh && cmsgh->cmsg_level == SOL_SOCKET && cmsgh->cmsg_type == SCM_RIGHTS && |
| 85 | cmsgh->cmsg_len == CMSG_LEN(sizeof(*fd))) { |
| 86 | memcpy(fd, CMSG_DATA(cmsgh), sizeof(*fd)); |
| 87 | } |
| 88 | |
| 89 | if (*fd < 0) { |
| 90 | errno = ENOENT; |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | Fwmark fwmark; |
Sreeram Ramachandran | 5ff58d4 | 2014-05-14 09:57:31 -0700 | [diff] [blame] | 95 | socklen_t fwmarkLen = sizeof(fwmark.intValue); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 96 | if (getsockopt(*fd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) { |
| 97 | return; |
| 98 | } |
| 99 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame^] | 100 | switch (command.cmdId) { |
| 101 | case FwmarkCommand::ON_ACCEPT: { |
Sreeram Ramachandran | 4f53662 | 2014-05-13 13:25:34 -0700 | [diff] [blame] | 102 | // Called after a socket accept(). The kernel would've marked the netId into the socket |
| 103 | // already, so we just need to check permissions here. |
| 104 | if (!mPermissionsController->isUserPermittedOnNetwork(client->getUid(), fwmark.netId)) { |
| 105 | errno = EPERM; |
| 106 | return; |
| 107 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame^] | 111 | case FwmarkCommand::ON_CONNECT: { |
| 112 | // Set the netId (of the default network) into the fwmark, if it has not already been |
| 113 | // set explicitly. Called before a socket connect() happens. |
| 114 | if (!fwmark.explicitlySelected) { |
| 115 | fwmark.netId = mNetworkController->getDefaultNetwork(); |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | case FwmarkCommand::SELECT_NETWORK: { |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 121 | // set socket mark |
| 122 | // TODO |
| 123 | break; |
| 124 | } |
| 125 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame^] | 126 | case FwmarkCommand::PROTECT_FROM_VPN: { |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 127 | // set vpn protect |
| 128 | // TODO |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | default: { |
| 133 | // unknown command |
| 134 | errno = EINVAL; |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | fwmark.permission = mPermissionsController->getPermissionForUser(client->getUid()); |
| 140 | |
| 141 | if (setsockopt(*fd, SOL_SOCKET, SO_MARK, &fwmark.intValue, sizeof(fwmark.intValue)) == -1) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | errno = 0; |
| 146 | } |