NetworkController.h revision 87475a1471373b72ffc9f81f17dfd7884723fa86
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#ifndef NETD_SERVER_NETWORK_CONTROLLER_H 18#define NETD_SERVER_NETWORK_CONTROLLER_H 19 20#include "NetdConstants.h" 21#include "Permission.h" 22 23#include "utils/RWLock.h" 24 25#include <list> 26#include <map> 27#include <set> 28#include <sys/types.h> 29#include <vector> 30 31class Network; 32class UidRanges; 33class VirtualNetwork; 34 35/* 36 * Keeps track of default, per-pid, and per-uid-range network selection, as 37 * well as the mark associated with each network. Networks are identified 38 * by netid. In all set* commands netid == 0 means "unspecified" and is 39 * equivalent to clearing the mapping. 40 */ 41class NetworkController { 42public: 43 static const int LOCAL_NET_ID = 9; 44 45 NetworkController(); 46 47 unsigned getDefaultNetwork() const; 48 int setDefaultNetwork(unsigned netId) WARN_UNUSED_RESULT; 49 50 // Order of preference: UID-specific, requestedNetId, default. 51 // Specify NETID_UNSET for requestedNetId if the default network is preferred. 52 // forDns indicates if we're querying the netId for a DNS request. This avoids sending DNS 53 // requests to VPNs without DNS servers. 54 unsigned getNetworkForUser(uid_t uid, unsigned requestedNetId, bool forDns) const; 55 unsigned getNetworkForInterface(const char* interface) const; 56 bool isVirtualNetwork(unsigned netId) const; 57 58 int createPhysicalNetwork(unsigned netId, Permission permission) WARN_UNUSED_RESULT; 59 int createVirtualNetwork(unsigned netId, bool hasDns) WARN_UNUSED_RESULT; 60 int destroyNetwork(unsigned netId) WARN_UNUSED_RESULT; 61 62 int addInterfaceToNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT; 63 int removeInterfaceFromNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT; 64 65 Permission getPermissionForUser(uid_t uid) const; 66 void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids); 67 bool canUserSelectNetwork(uid_t uid, unsigned netId) const; 68 int setPermissionForNetworks(Permission permission, 69 const std::vector<unsigned>& netIds) WARN_UNUSED_RESULT; 70 71 int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT; 72 int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT; 73 74 // Routes are added to tables determined by the interface, so only |interface| is actually used. 75 // |netId| is given only to sanity check that the interface has the correct netId. 76 int addRoute(unsigned netId, const char* interface, const char* destination, 77 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT; 78 int removeRoute(unsigned netId, const char* interface, const char* destination, 79 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT; 80 81 bool canProtect(uid_t uid) const; 82 void allowProtect(const std::vector<uid_t>& uids); 83 void denyProtect(const std::vector<uid_t>& uids); 84 85private: 86 bool isValidNetwork(unsigned netId) const; 87 Network* getNetworkLocked(unsigned netId) const; 88 VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const; 89 Permission getPermissionForUserLocked(uid_t uid) const; 90 91 int modifyRoute(unsigned netId, const char* interface, const char* destination, 92 const char* nexthop, bool add, bool legacy, uid_t uid) WARN_UNUSED_RESULT; 93 94 // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers and mProtectableUsers. 95 mutable android::RWLock mRWLock; 96 unsigned mDefaultNetId; 97 std::map<unsigned, Network*> mNetworks; // Map keys are NetIds. 98 std::map<uid_t, Permission> mUsers; 99 std::set<uid_t> mProtectableUsers; 100}; 101 102#endif // NETD_SERVER_NETWORK_CONTROLLER_H 103