blob: ac044e33b434bae53e286eb14a23d9a6f39bc18b [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
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070020#include "NetdConstants.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070021#include "Permission.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070022
Sreeram Ramachandran72604072014-05-21 13:19:43 -070023#include "utils/RWLock.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070024
Robin Lee2cf56172016-09-13 18:55:42 +090025#include <atomic>
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
Erik Kline2d3a1632016-03-15 16:33:48 +090032class DumpWriter;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070033class Network;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070034class UidRanges;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070035class VirtualNetwork;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070036
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050037/*
38 * Keeps track of default, per-pid, and per-uid-range network selection, as
39 * well as the mark associated with each network. Networks are identified
40 * by netid. In all set* commands netid == 0 means "unspecified" and is
41 * equivalent to clearing the mapping.
42 */
43class NetworkController {
44public:
Sreeram Ramachandranbbdde992014-09-05 16:05:03 -070045 static const unsigned MIN_OEM_ID;
46 static const unsigned MAX_OEM_ID;
47 static const unsigned LOCAL_NET_ID;
Lorenzo Colitti36679362015-02-25 10:26:19 +090048 static const unsigned DUMMY_NET_ID;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070049
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070050 NetworkController();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050051
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050052 unsigned getDefaultNetwork() const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070053 int setDefaultNetwork(unsigned netId) WARN_UNUSED_RESULT;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050054
Sreeram Ramachandran1011b492014-07-24 19:04:32 -070055 // Sets |*netId| to an appropriate NetId to use for DNS for the given user. Call with |*netId|
56 // set to a non-NETID_UNSET value if the user already has indicated a preference. Returns the
57 // fwmark value to set on the socket when performing the DNS request.
58 uint32_t getNetworkForDns(unsigned* netId, uid_t uid) const;
59 unsigned getNetworkForUser(uid_t uid) const;
60 unsigned getNetworkForConnect(uid_t uid) const;
Erik Klinecea2d342015-06-25 18:24:46 +090061 void getNetworkContext(unsigned netId, uid_t uid, struct android_net_context* netcontext) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070062 unsigned getNetworkForInterface(const char* interface) const;
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -070063 bool isVirtualNetwork(unsigned netId) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050064
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070065 int createPhysicalNetwork(unsigned netId, Permission permission) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070066 int createVirtualNetwork(unsigned netId, bool hasDns, bool secure) WARN_UNUSED_RESULT;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070067 int destroyNetwork(unsigned netId) WARN_UNUSED_RESULT;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070068
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070069 int addInterfaceToNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
70 int removeInterfaceFromNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
71
72 Permission getPermissionForUser(uid_t uid) const;
73 void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids);
Lorenzo Colittia1067c82014-10-02 22:47:41 +090074 int checkUserNetworkAccess(uid_t uid, unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070075 int setPermissionForNetworks(Permission permission,
76 const std::vector<unsigned>& netIds) WARN_UNUSED_RESULT;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -070077
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070078 int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
79 int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
80
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -070081 // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
Lorenzo Colitti4c95a122014-09-18 16:01:50 +090082 // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -070083 //
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070084 // Routes are added to tables determined by the interface, so only |interface| is actually used.
85 // |netId| is given only to sanity check that the interface has the correct netId.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090086 int addRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070087 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090088 int removeRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070089 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran72604072014-05-21 13:19:43 -070090
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070091 bool canProtect(uid_t uid) const;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070092 void allowProtect(const std::vector<uid_t>& uids);
93 void denyProtect(const std::vector<uid_t>& uids);
94
Erik Kline2d3a1632016-03-15 16:33:48 +090095 void dump(DumpWriter& dw);
96
Robin Lee2cf56172016-09-13 18:55:42 +090097 int setMetricsReportingLevel(const int level);
98 int getMetricsReportingLevel() const;
99
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500100private:
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700101 bool isValidNetwork(unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700102 Network* getNetworkLocked(unsigned netId) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700103 VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const;
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700104 Permission getPermissionForUserLocked(uid_t uid) const;
Lorenzo Colittia1067c82014-10-02 22:47:41 +0900105 int checkUserNetworkAccessLocked(uid_t uid, unsigned netId) const;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700106
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900107 int modifyRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700108 const char* nexthop, bool add, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700109 int modifyFallthroughLocked(unsigned vpnNetId, bool add) WARN_UNUSED_RESULT;
110
111 class DelegateImpl;
112 DelegateImpl* const mDelegateImpl;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700113
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700114 // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers and mProtectableUsers.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500115 mutable android::RWLock mRWLock;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500116 unsigned mDefaultNetId;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700117 std::map<unsigned, Network*> mNetworks; // Map keys are NetIds.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700118 std::map<uid_t, Permission> mUsers;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700119 std::set<uid_t> mProtectableUsers;
Robin Lee2cf56172016-09-13 18:55:42 +0900120 std::atomic_int mReportingLevel{1};
121
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500122};
123
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700124#endif // NETD_SERVER_NETWORK_CONTROLLER_H