Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [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 | |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 17 | #ifndef NETD_SERVER_NETWORK_CONTROLLER_H |
| 18 | #define NETD_SERVER_NETWORK_CONTROLLER_H |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 19 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 20 | #include "NetdConstants.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 21 | #include "Permission.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 22 | |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 23 | #include "utils/RWLock.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 24 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 25 | #include <list> |
| 26 | #include <map> |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 28 | #include <vector> |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 29 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 30 | class Network; |
| 31 | class PhysicalNetwork; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame^] | 32 | class VirtualNetwork; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 33 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 34 | /* |
| 35 | * Keeps track of default, per-pid, and per-uid-range network selection, as |
| 36 | * well as the mark associated with each network. Networks are identified |
| 37 | * by netid. In all set* commands netid == 0 means "unspecified" and is |
| 38 | * equivalent to clearing the mapping. |
| 39 | */ |
| 40 | class NetworkController { |
| 41 | public: |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 42 | NetworkController(); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 43 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 44 | unsigned getDefaultNetwork() const; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 45 | int setDefaultNetwork(unsigned netId) WARN_UNUSED_RESULT; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 46 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 47 | bool setNetworkForUidRange(uid_t uidStart, uid_t uidEnd, unsigned netId, bool forwardDns); |
| 48 | bool clearNetworkForUidRange(uid_t uidStart, uid_t uidEnd, unsigned netId); |
| 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 |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 53 | // requests to VPNs without DNS servers. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 54 | unsigned getNetwork(uid_t uid, unsigned requestedNetId, bool forDns) const; |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 55 | unsigned getNetworkId(const char* interface) const; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 56 | bool isValidNetwork(unsigned netId) const; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 57 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 58 | int createNetwork(unsigned netId, Permission permission) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame^] | 59 | int createVpn(unsigned netId, uid_t ownerUid) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 60 | int destroyNetwork(unsigned netId) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 61 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 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 isUserPermittedOnNetwork(uid_t uid, unsigned netId) const; |
| 68 | int setPermissionForNetworks(Permission permission, |
| 69 | const std::vector<unsigned>& netIds) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 70 | |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 71 | // Routes are added to tables determined by the interface, so only |interface| is actually used. |
| 72 | // |netId| is given only to sanity check that the interface has the correct netId. |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 73 | int addRoute(unsigned netId, const char* interface, const char* destination, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 74 | const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT; |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 75 | int removeRoute(unsigned netId, const char* interface, const char* destination, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 76 | const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 77 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 78 | private: |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 79 | Network* getNetworkLocked(unsigned netId) const; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 80 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 81 | int modifyRoute(unsigned netId, const char* interface, const char* destination, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 82 | const char* nexthop, bool add, bool legacy, uid_t uid) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 83 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 84 | struct UidEntry { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 85 | const uid_t uidStart; |
| 86 | const uid_t uidEnd; |
| 87 | const unsigned netId; |
| 88 | bool forwardDns; |
| 89 | |
| 90 | UidEntry(uid_t uidStart, uid_t uidEnd, unsigned netId, bool forwardDns); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 91 | }; |
| 92 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 93 | // mRWLock guards all accesses to mUidMap, mDefaultNetId, mPhysicalNetworks and mUsers. |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 94 | mutable android::RWLock mRWLock; |
| 95 | std::list<UidEntry> mUidMap; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 96 | unsigned mDefaultNetId; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 97 | std::map<unsigned, PhysicalNetwork*> mPhysicalNetworks; // Map keys are NetIds. |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame^] | 98 | std::map<unsigned, VirtualNetwork*> mVirtualNetworks; // Map keys are NetIds. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 99 | std::map<uid_t, Permission> mUsers; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 100 | }; |
| 101 | |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 102 | #endif // NETD_SERVER_NETWORK_CONTROLLER_H |