blob: 378c9f195be96fee6859edf271d4e8ad467cb203 [file] [log] [blame]
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -05001/*
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
Sreeram Ramachandran72604072014-05-21 13:19:43 -070017#ifndef NETD_SERVER_NETWORK_CONTROLLER_H
18#define NETD_SERVER_NETWORK_CONTROLLER_H
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050019
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070020#include <android/multinetwork.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070021#include "NetdConstants.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070022#include "Permission.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070023
Sreeram Ramachandran72604072014-05-21 13:19:43 -070024#include "utils/RWLock.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070025
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050026#include <list>
27#include <map>
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070028#include <set>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070029#include <sys/types.h>
Sreeram Ramachandran72604072014-05-21 13:19:43 -070030#include <vector>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050031
Lorenzo Colitti7035f222017-02-13 18:29:00 +090032struct android_net_context;
33
34namespace android {
35namespace net {
36
Lorenzo Colittiae8da9a2017-09-05 11:58:27 +090037constexpr uint32_t kHandleMagic = 0xcafed00d;
38
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070039// Utility to convert from netId to net_handle_t. Doing this here as opposed to exporting
40// from net.c as it may have NDK implications. Besides no conversion available in net.c for
41// obtaining handle given netId.
42// TODO: Use getnetidfromhandle() in net.c.
43static inline unsigned netHandleToNetId(net_handle_t fromNetHandle) {
44 const uint32_t k32BitMask = 0xffffffff;
45 // This value MUST be kept in sync with the corresponding value in
46 // the android.net.Network#getNetworkHandle() implementation.
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070047
48 // Check for minimum acceptable version of the API in the low bits.
49 if (fromNetHandle != NETWORK_UNSPECIFIED &&
50 (fromNetHandle & k32BitMask) != kHandleMagic) {
51 return 0;
52 }
53
54 return ((fromNetHandle >> (CHAR_BIT * sizeof(k32BitMask))) & k32BitMask);
55}
56
57// Utility to convert from nethandle to netid, keep in sync with getNetworkHandle
58// in Network.java.
59static inline net_handle_t netIdToNetHandle(unsigned fromNetId) {
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070060 if (!fromNetId) {
61 return NETWORK_UNSPECIFIED;
62 }
Lorenzo Colittiae8da9a2017-09-05 11:58:27 +090063 return (((net_handle_t)fromNetId << 32) | kHandleMagic);
Niranjan Pendharkar4c18bd92017-07-24 09:54:07 -070064}
65
Erik Kline2d3a1632016-03-15 16:33:48 +090066class DumpWriter;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070067class Network;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070068class UidRanges;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070069class VirtualNetwork;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070070
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050071/*
72 * Keeps track of default, per-pid, and per-uid-range network selection, as
73 * well as the mark associated with each network. Networks are identified
74 * by netid. In all set* commands netid == 0 means "unspecified" and is
75 * equivalent to clearing the mapping.
76 */
77class NetworkController {
78public:
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070079 static const unsigned MIN_OEM_ID;
80 static const unsigned MAX_OEM_ID;
81 static const unsigned LOCAL_NET_ID;
Lorenzo Colitti36679362015-02-25 10:26:19 +090082 static const unsigned DUMMY_NET_ID;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070083
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070084 NetworkController();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050085
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050086 unsigned getDefaultNetwork() const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070087 int setDefaultNetwork(unsigned netId) WARN_UNUSED_RESULT;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050088
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070089 // Sets |*netId| to an appropriate NetId to use for DNS for the given user. Call with |*netId|
90 // set to a non-NETID_UNSET value if the user already has indicated a preference. Returns the
91 // fwmark value to set on the socket when performing the DNS request.
92 uint32_t getNetworkForDns(unsigned* netId, uid_t uid) const;
93 unsigned getNetworkForUser(uid_t uid) const;
94 unsigned getNetworkForConnect(uid_t uid) const;
Erik Klinecea2d342015-06-25 18:24:46 +090095 void getNetworkContext(unsigned netId, uid_t uid, struct android_net_context* netcontext) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070096 unsigned getNetworkForInterface(const char* interface) const;
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -070097 bool isVirtualNetwork(unsigned netId) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050098
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070099 int createPhysicalNetwork(unsigned netId, Permission permission) WARN_UNUSED_RESULT;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700100 int createPhysicalOemNetwork(Permission permission, unsigned *netId) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700101 int createVirtualNetwork(unsigned netId, bool hasDns, bool secure) WARN_UNUSED_RESULT;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700102 int destroyNetwork(unsigned netId) WARN_UNUSED_RESULT;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700103
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700104 int addInterfaceToNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
105 int removeInterfaceFromNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
106
107 Permission getPermissionForUser(uid_t uid) const;
108 void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids);
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900109 int checkUserNetworkAccess(uid_t uid, unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700110 int setPermissionForNetworks(Permission permission,
111 const std::vector<unsigned>& netIds) WARN_UNUSED_RESULT;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700112
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700113 int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
114 int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
115
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700116 // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900117 // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700118 //
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700119 // Routes are added to tables determined by the interface, so only |interface| is actually used.
120 // |netId| is given only to sanity check that the interface has the correct netId.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900121 int addRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700122 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900123 int removeRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700124 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700125
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700126 bool canProtect(uid_t uid) const;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700127 void allowProtect(const std::vector<uid_t>& uids);
128 void denyProtect(const std::vector<uid_t>& uids);
129
Erik Kline2d3a1632016-03-15 16:33:48 +0900130 void dump(DumpWriter& dw);
131
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500132private:
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700133 bool isValidNetworkLocked(unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700134 Network* getNetworkLocked(unsigned netId) const;
Lorenzo Colittibbd0aff2016-12-15 22:53:24 +0900135 uint32_t getNetworkForDnsLocked(unsigned* netId, uid_t uid) const;
136 unsigned getNetworkForUserLocked(uid_t uid) const;
137 unsigned getNetworkForConnectLocked(uid_t uid) const;
138 unsigned getNetworkForInterfaceLocked(const char* interface) const;
139 bool canProtectLocked(uid_t uid) const;
140
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700141 VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const;
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700142 Permission getPermissionForUserLocked(uid_t uid) const;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900143 int checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700144 int createPhysicalNetworkLocked(unsigned netId, Permission permission) WARN_UNUSED_RESULT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700145
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900146 int modifyRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700147 const char* nexthop, bool add, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700148 int modifyFallthroughLocked(unsigned vpnNetId, bool add) WARN_UNUSED_RESULT;
149
150 class DelegateImpl;
151 DelegateImpl* const mDelegateImpl;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700152
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700153 // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers and mProtectableUsers.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500154 mutable android::RWLock mRWLock;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500155 unsigned mDefaultNetId;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700156 std::map<unsigned, Network*> mNetworks; // Map keys are NetIds.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700157 std::map<uid_t, Permission> mUsers;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700158 std::set<uid_t> mProtectableUsers;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500159};
160
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900161} // namespace net
162} // namespace android
163
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700164#endif // NETD_SERVER_NETWORK_CONTROLLER_H