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" |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 21 | #include "NetdConstants.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 22 | #include "NetworkController.h" |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 23 | #include "TrafficController.h" |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 24 | #include "resolv_netid.h" |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 25 | |
Hugo Benichi | 456c906 | 2017-01-04 12:19:16 +0900 | [diff] [blame] | 26 | #include <netinet/in.h> |
Lorenzo Colitti | 09175be | 2017-11-17 14:01:21 +0900 | [diff] [blame] | 27 | #include <selinux/selinux.h> |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 28 | #include <sys/socket.h> |
| 29 | #include <unistd.h> |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 30 | #include <utils/String16.h> |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 31 | |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 32 | #include <binder/IServiceManager.h> |
| 33 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 34 | using android::String16; |
| 35 | using android::net::metrics::INetdEventListener; |
| 36 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace net { |
| 39 | |
Lorenzo Colitti | 09175be | 2017-11-17 14:01:21 +0900 | [diff] [blame] | 40 | constexpr const char *UPDATE_DEVICE_STATS = "android.permission.UPDATE_DEVICE_STATS"; |
| 41 | constexpr const char *SYSTEM_SERVER_CONTEXT = "u:r:system_server:s0"; |
| 42 | |
| 43 | bool isSystemServer(SocketClient* client) { |
| 44 | if (client->getUid() != AID_SYSTEM) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | char *context; |
| 49 | if (getpeercon(client->getSocket(), &context)) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | // We can't use context_new and context_type_get as they're private to libselinux. So just do |
| 54 | // a string match instead. |
| 55 | bool ret = !strcmp(context, SYSTEM_SERVER_CONTEXT); |
| 56 | freecon(context); |
| 57 | |
| 58 | return ret; |
| 59 | } |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 60 | |
| 61 | bool hasUpdateDeviceStatsPermission(SocketClient* client) { |
Lorenzo Colitti | 09175be | 2017-11-17 14:01:21 +0900 | [diff] [blame] | 62 | // If the caller is the system server, allow without any further checks. |
| 63 | // Otherwise, if the system server's binder thread pool is full, and all the threads are |
| 64 | // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492 |
| 65 | return isSystemServer(client) || |
| 66 | checkPermission(String16(UPDATE_DEVICE_STATS), client->getPid(), client->getUid()); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 69 | FwmarkServer::FwmarkServer(NetworkController* networkController, EventReporter* eventReporter, |
| 70 | TrafficController* trafficCtrl) |
| 71 | : SocketListener(SOCKET_NAME, true), |
| 72 | mNetworkController(networkController), |
| 73 | mEventReporter(eventReporter), |
| 74 | mTrafficCtrl(trafficCtrl) {} |
| 75 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 76 | bool FwmarkServer::onDataAvailable(SocketClient* client) { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 77 | int socketFd = -1; |
| 78 | int error = processClient(client, &socketFd); |
| 79 | if (socketFd >= 0) { |
| 80 | close(socketFd); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Always send a response even if there were connection errors or read errors, so that we don't |
| 84 | // inadvertently cause the client to hang (which always waits for a response). |
| 85 | client->sendData(&error, sizeof(error)); |
| 86 | |
| 87 | // Always close the client connection (by returning false). This prevents a DoS attack where |
| 88 | // the client issues multiple commands on the same connection, never reading the responses, |
| 89 | // causing its receive buffer to fill up, and thus causing our client->sendData() to block. |
| 90 | return false; |
| 91 | } |
| 92 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 93 | int FwmarkServer::processClient(SocketClient* client, int* socketFd) { |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 94 | FwmarkCommand command; |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 95 | FwmarkConnectInfo connectInfo; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 96 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 97 | iovec iov[2] = { |
| 98 | { &command, sizeof(command) }, |
| 99 | { &connectInfo, sizeof(connectInfo) }, |
| 100 | }; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 101 | msghdr message; |
| 102 | memset(&message, 0, sizeof(message)); |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 103 | message.msg_iov = iov; |
| 104 | message.msg_iovlen = ARRAY_SIZE(iov); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 105 | |
| 106 | union { |
| 107 | cmsghdr cmh; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 108 | char cmsg[CMSG_SPACE(sizeof(*socketFd))]; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 109 | } cmsgu; |
| 110 | |
| 111 | memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg)); |
| 112 | message.msg_control = cmsgu.cmsg; |
| 113 | message.msg_controllen = sizeof(cmsgu.cmsg); |
| 114 | |
Nick Kralevich | 9384f23 | 2016-12-20 06:51:32 -0800 | [diff] [blame] | 115 | int messageLength = TEMP_FAILURE_RETRY(recvmsg(client->getSocket(), &message, MSG_CMSG_CLOEXEC)); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 116 | if (messageLength <= 0) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 117 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 120 | if (!((command.cmdId != FwmarkCommand::ON_CONNECT_COMPLETE && messageLength == sizeof(command)) |
| 121 | || (command.cmdId == FwmarkCommand::ON_CONNECT_COMPLETE |
| 122 | && messageLength == sizeof(command) + sizeof(connectInfo)))) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 123 | return -EBADMSG; |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 126 | Permission permission = mNetworkController->getPermissionForUser(client->getUid()); |
| 127 | |
| 128 | if (command.cmdId == FwmarkCommand::QUERY_USER_ACCESS) { |
| 129 | if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) { |
| 130 | return -EPERM; |
| 131 | } |
| 132 | return mNetworkController->checkUserNetworkAccess(command.uid, command.netId); |
| 133 | } |
| 134 | |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 135 | if (command.cmdId == FwmarkCommand::SET_COUNTERSET) { |
| 136 | if (!hasUpdateDeviceStatsPermission(client)) { |
| 137 | return -EPERM; |
| 138 | } |
| 139 | return mTrafficCtrl->setCounterSet(command.trafficCtrlInfo, command.uid); |
| 140 | } |
| 141 | |
| 142 | if (command.cmdId == FwmarkCommand::DELETE_TAGDATA) { |
| 143 | if (!hasUpdateDeviceStatsPermission(client)) { |
| 144 | return -EPERM; |
| 145 | } |
| 146 | return mTrafficCtrl->deleteTagData(command.trafficCtrlInfo, command.uid); |
| 147 | } |
| 148 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 149 | cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message); |
| 150 | if (cmsgh && cmsgh->cmsg_level == SOL_SOCKET && cmsgh->cmsg_type == SCM_RIGHTS && |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 151 | cmsgh->cmsg_len == CMSG_LEN(sizeof(*socketFd))) { |
| 152 | memcpy(socketFd, CMSG_DATA(cmsgh), sizeof(*socketFd)); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 155 | if (*socketFd < 0) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 156 | return -EBADF; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Remi NGUYEN VAN | eabc5da | 2018-04-24 18:54:58 +0900 | [diff] [blame] | 159 | int family; |
| 160 | socklen_t familyLen = sizeof(family); |
| 161 | if (getsockopt(*socketFd, SOL_SOCKET, SO_DOMAIN, &family, &familyLen) == -1) { |
| 162 | return -errno; |
| 163 | } |
| 164 | if (!FwmarkCommand::isSupportedFamily(family)) { |
| 165 | return -EAFNOSUPPORT; |
| 166 | } |
| 167 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 168 | Fwmark fwmark; |
Sreeram Ramachandran | 5ff58d4 | 2014-05-14 09:57:31 -0700 | [diff] [blame] | 169 | socklen_t fwmarkLen = sizeof(fwmark.intValue); |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 170 | if (getsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 171 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 174 | switch (command.cmdId) { |
| 175 | case FwmarkCommand::ON_ACCEPT: { |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 176 | // Called after a socket accept(). The kernel would've marked the NetId and necessary |
Sreeram Ramachandran | a675b00 | 2014-07-05 11:00:55 -0700 | [diff] [blame] | 177 | // permissions bits, so we just add the rest of the user's permissions here. |
| 178 | permission = static_cast<Permission>(permission | fwmark.permission); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 182 | case FwmarkCommand::ON_CONNECT: { |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 183 | // Called before a socket connect() happens. Set an appropriate NetId into the fwmark so |
| 184 | // that the socket routes consistently over that network. Do this even if the socket |
| 185 | // already has a NetId, so that calling connect() multiple times still works. |
Sreeram Ramachandran | 070b2d2 | 2014-07-11 17:06:12 -0700 | [diff] [blame] | 186 | // |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 187 | // But if the explicit bit was set, the existing NetId was explicitly preferred (and not |
| 188 | // a case of connect() being called multiple times). Don't reset the NetId in that case. |
| 189 | // |
| 190 | // An "appropriate" NetId is the NetId of a bypassable VPN that applies to the user, or |
| 191 | // failing that, the default network. We'll never set the NetId of a secure VPN here. |
| 192 | // See the comments in the implementation of getNetworkForConnect() for more details. |
| 193 | // |
| 194 | // If the protect bit is set, this could be either a system proxy (e.g.: the dns proxy |
| 195 | // or the download manager) acting on behalf of another user, or a VPN provider. If it's |
| 196 | // a proxy, we shouldn't reset the NetId. If it's a VPN provider, we should set the |
| 197 | // default network's NetId. |
| 198 | // |
| 199 | // There's no easy way to tell the difference between a proxy and a VPN app. We can't |
| 200 | // use PERMISSION_SYSTEM to identify the proxy because a VPN app may also have those |
| 201 | // permissions. So we use the following heuristic: |
| 202 | // |
| 203 | // If it's a proxy, but the existing NetId is not a VPN, that means the user (that the |
| 204 | // proxy is acting on behalf of) is not subject to a VPN, so the proxy must have picked |
| 205 | // the default network's NetId. So, it's okay to replace that with the current default |
| 206 | // network's NetId (which in all likelihood is the same). |
| 207 | // |
| 208 | // Conversely, if it's a VPN provider, the existing NetId cannot be a VPN. The only time |
| 209 | // we set a VPN's NetId into a socket without setting the explicit bit is here, in |
| 210 | // ON_CONNECT, but we won't do that if the socket has the protect bit set. If the VPN |
| 211 | // provider connect()ed (and got the VPN NetId set) and then called protect(), we |
| 212 | // would've unset the NetId in PROTECT_FROM_VPN below. |
| 213 | // |
| 214 | // So, overall (when the explicit bit is not set but the protect bit is set), if the |
| 215 | // existing NetId is a VPN, don't reset it. Else, set the default network's NetId. |
| 216 | if (!fwmark.explicitlySelected) { |
| 217 | if (!fwmark.protectedFromVpn) { |
| 218 | fwmark.netId = mNetworkController->getNetworkForConnect(client->getUid()); |
| 219 | } else if (!mNetworkController->isVirtualNetwork(fwmark.netId)) { |
| 220 | fwmark.netId = mNetworkController->getDefaultNetwork(); |
| 221 | } |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 222 | } |
| 223 | break; |
| 224 | } |
| 225 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 226 | case FwmarkCommand::ON_CONNECT_COMPLETE: { |
| 227 | // Called after a socket connect() completes. |
| 228 | // This reports connect event including netId, destination IP address, destination port, |
Hugo Benichi | 7dfaa78 | 2016-10-31 15:07:23 +0900 | [diff] [blame] | 229 | // uid, connect latency, and connect errno if any. |
Hugo Benichi | 456c906 | 2017-01-04 12:19:16 +0900 | [diff] [blame] | 230 | |
| 231 | // Skip reporting if connect() happened on a UDP socket. |
| 232 | int socketProto; |
| 233 | socklen_t intSize = sizeof(socketProto); |
| 234 | const int ret = getsockopt(*socketFd, SOL_SOCKET, SO_PROTOCOL, &socketProto, &intSize); |
| 235 | if ((ret != 0) || (socketProto == IPPROTO_UDP)) { |
| 236 | break; |
| 237 | } |
| 238 | |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 239 | android::sp<android::net::metrics::INetdEventListener> netdEventListener = |
| 240 | mEventReporter->getNetdEventListener(); |
| 241 | |
| 242 | if (netdEventListener != nullptr) { |
| 243 | char addrstr[INET6_ADDRSTRLEN]; |
| 244 | char portstr[sizeof("65536")]; |
| 245 | const int ret = getnameinfo((sockaddr*) &connectInfo.addr, sizeof(connectInfo.addr), |
| 246 | addrstr, sizeof(addrstr), portstr, sizeof(portstr), |
| 247 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 248 | |
Hugo Benichi | 7dfaa78 | 2016-10-31 15:07:23 +0900 | [diff] [blame] | 249 | netdEventListener->onConnectEvent(fwmark.netId, connectInfo.error, |
| 250 | connectInfo.latencyMs, |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 251 | (ret == 0) ? String16(addrstr) : String16(""), |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 252 | (ret == 0) ? strtoul(portstr, nullptr, 10) : 0, client->getUid()); |
Michal Karpinski | 7d37453 | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 253 | } |
| 254 | break; |
| 255 | } |
| 256 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 257 | case FwmarkCommand::SELECT_NETWORK: { |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 258 | fwmark.netId = command.netId; |
| 259 | if (command.netId == NETID_UNSET) { |
| 260 | fwmark.explicitlySelected = false; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 261 | fwmark.protectedFromVpn = false; |
| 262 | permission = PERMISSION_NONE; |
Lorenzo Colitti | a1067c8 | 2014-10-02 22:47:41 +0900 | [diff] [blame] | 263 | } else { |
| 264 | if (int ret = mNetworkController->checkUserNetworkAccess(client->getUid(), |
| 265 | command.netId)) { |
| 266 | return ret; |
| 267 | } |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 268 | fwmark.explicitlySelected = true; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 269 | fwmark.protectedFromVpn = mNetworkController->canProtect(client->getUid()); |
Sreeram Ramachandran | ec00884 | 2014-05-21 14:01:16 -0700 | [diff] [blame] | 270 | } |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 274 | case FwmarkCommand::PROTECT_FROM_VPN: { |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 275 | if (!mNetworkController->canProtect(client->getUid())) { |
| 276 | return -EPERM; |
| 277 | } |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 278 | // If a bypassable VPN's provider app calls connect() and then protect(), it will end up |
| 279 | // with a socket that looks like that of a system proxy but is not (see comments for |
| 280 | // ON_CONNECT above). So, reset the NetId. |
| 281 | // |
| 282 | // In any case, it's appropriate that if the socket has an implicit VPN NetId mark, the |
| 283 | // PROTECT_FROM_VPN command should unset it. |
| 284 | if (!fwmark.explicitlySelected && mNetworkController->isVirtualNetwork(fwmark.netId)) { |
| 285 | fwmark.netId = mNetworkController->getDefaultNetwork(); |
| 286 | } |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 287 | fwmark.protectedFromVpn = true; |
| 288 | permission = static_cast<Permission>(permission | fwmark.permission); |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 289 | break; |
| 290 | } |
| 291 | |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 292 | case FwmarkCommand::SELECT_FOR_USER: { |
| 293 | if ((permission & PERMISSION_SYSTEM) != PERMISSION_SYSTEM) { |
| 294 | return -EPERM; |
| 295 | } |
Sreeram Ramachandran | 1011b49 | 2014-07-24 19:04:32 -0700 | [diff] [blame] | 296 | fwmark.netId = mNetworkController->getNetworkForUser(command.uid); |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 297 | fwmark.protectedFromVpn = true; |
| 298 | break; |
| 299 | } |
| 300 | |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 301 | case FwmarkCommand::TAG_SOCKET: { |
| 302 | // If the UID is -1, tag as the caller's UID: |
| 303 | // - TrafficStats and NetworkManagementSocketTagger use -1 to indicate "use the |
| 304 | // caller's UID". |
| 305 | // - xt_qtaguid will see -1 on the command line, fail to parse it as a uint32_t, and |
| 306 | // fall back to current_fsuid(). |
| 307 | if (static_cast<int>(command.uid) == -1) { |
| 308 | command.uid = client->getUid(); |
| 309 | } |
| 310 | if (command.uid != client->getUid() && !hasUpdateDeviceStatsPermission(client)) { |
| 311 | return -EPERM; |
| 312 | } |
| 313 | return mTrafficCtrl->tagSocket(*socketFd, command.trafficCtrlInfo, command.uid); |
| 314 | } |
| 315 | |
| 316 | case FwmarkCommand::UNTAG_SOCKET: { |
| 317 | // Any process can untag a socket it has an fd for. |
| 318 | return mTrafficCtrl->untagSocket(*socketFd); |
| 319 | } |
| 320 | |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 321 | default: { |
| 322 | // unknown command |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 323 | return -EPROTO; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Sreeram Ramachandran | a675b00 | 2014-07-05 11:00:55 -0700 | [diff] [blame] | 327 | fwmark.permission = permission; |
| 328 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 329 | if (setsockopt(*socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, |
| 330 | sizeof(fwmark.intValue)) == -1) { |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 331 | return -errno; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Sreeram Ramachandran | 3a069e6 | 2014-06-22 11:02:57 -0700 | [diff] [blame] | 334 | return 0; |
Sreeram Ramachandran | 030b36e | 2014-05-11 21:04:03 -0700 | [diff] [blame] | 335 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 336 | |
| 337 | } // namespace net |
| 338 | } // namespace android |