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 | |
| 17 | #ifndef _NETD_NETWORKCONTROLLER_H |
| 18 | #define _NETD_NETWORKCONTROLLER_H |
| 19 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 20 | #include "Permission.h" |
| 21 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 22 | #include <list> |
| 23 | #include <map> |
| 24 | #include <string> |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame^] | 25 | #include <vector> |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 26 | |
| 27 | #include <stddef.h> |
| 28 | #include <stdint.h> |
| 29 | #include <utils/RWLock.h> |
| 30 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 31 | class PermissionsController; |
| 32 | class RouteController; |
| 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: |
| 42 | enum { |
| 43 | // For use with getNetwork(). |
| 44 | PID_UNSPECIFIED = 0, |
| 45 | }; |
| 46 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 47 | static bool isNetIdValid(unsigned netId); |
| 48 | |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 49 | NetworkController(PermissionsController* permissionsController, |
| 50 | RouteController* routeController); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 51 | |
| 52 | void clearNetworkPreference(); |
| 53 | unsigned getDefaultNetwork() const; |
| 54 | void setDefaultNetwork(unsigned netId); |
| 55 | void setNetworkForPid(int pid, unsigned netId); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 56 | bool setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, bool forward_dns); |
Paul Jensen | 5b49ab9 | 2014-04-03 19:06:00 -0400 | [diff] [blame] | 57 | bool clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 58 | |
| 59 | // Order of preference: UID-specific, requested_netId, PID-specific, default. |
| 60 | // Specify NETID_UNSET for requested_netId if the default network is preferred. |
| 61 | // Specify PID_UNSPECIFIED for pid to ignore PID-specific overrides. |
| 62 | // for_dns indicates if we're querrying the netId for a DNS request. This avoids sending DNS |
| 63 | // requests to VPNs without DNS servers. |
| 64 | unsigned getNetwork(int uid, unsigned requested_netId, int pid, bool for_dns) const; |
| 65 | |
| 66 | unsigned getNetworkId(const char* interface); |
| 67 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 68 | bool createNetwork(unsigned netId, const char* interface, Permission permission); |
| 69 | bool destroyNetwork(unsigned netId); |
| 70 | |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame^] | 71 | bool setPermissionForUser(Permission permission, const std::vector<unsigned>& uid); |
| 72 | bool setPermissionForNetwork(Permission permission, const std::vector<unsigned>& netId); |
| 73 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 74 | private: |
| 75 | struct UidEntry { |
| 76 | int uid_start; |
| 77 | int uid_end; |
| 78 | unsigned netId; |
| 79 | bool forward_dns; |
| 80 | UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns); |
| 81 | }; |
| 82 | |
| 83 | mutable android::RWLock mRWLock; |
| 84 | std::list<UidEntry> mUidMap; |
| 85 | std::map<int, unsigned> mPidMap; |
| 86 | unsigned mDefaultNetId; |
| 87 | |
| 88 | std::map<std::string, unsigned> mIfaceNetidMap; |
| 89 | unsigned mNextFreeNetId; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 90 | |
| 91 | PermissionsController* const mPermissionsController; |
| 92 | RouteController* const mRouteController; |
| 93 | |
| 94 | std::multimap<unsigned, std::string> mNetIdToInterfaces; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | #endif |