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> |
| 25 | |
| 26 | #include <stddef.h> |
| 27 | #include <stdint.h> |
| 28 | #include <utils/RWLock.h> |
| 29 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame^] | 30 | class PermissionsController; |
| 31 | class RouteController; |
| 32 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 33 | /* |
| 34 | * Keeps track of default, per-pid, and per-uid-range network selection, as |
| 35 | * well as the mark associated with each network. Networks are identified |
| 36 | * by netid. In all set* commands netid == 0 means "unspecified" and is |
| 37 | * equivalent to clearing the mapping. |
| 38 | */ |
| 39 | class NetworkController { |
| 40 | public: |
| 41 | enum { |
| 42 | // For use with getNetwork(). |
| 43 | PID_UNSPECIFIED = 0, |
| 44 | }; |
| 45 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame^] | 46 | static bool isNetIdValid(unsigned netId); |
| 47 | |
| 48 | NetworkController(PermissionsController* permCtrl, |
| 49 | RouteController* routeCtrl); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 50 | |
| 51 | void clearNetworkPreference(); |
| 52 | unsigned getDefaultNetwork() const; |
| 53 | void setDefaultNetwork(unsigned netId); |
| 54 | void setNetworkForPid(int pid, unsigned netId); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 55 | 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] | 56 | bool clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 57 | |
| 58 | // Order of preference: UID-specific, requested_netId, PID-specific, default. |
| 59 | // Specify NETID_UNSET for requested_netId if the default network is preferred. |
| 60 | // Specify PID_UNSPECIFIED for pid to ignore PID-specific overrides. |
| 61 | // for_dns indicates if we're querrying the netId for a DNS request. This avoids sending DNS |
| 62 | // requests to VPNs without DNS servers. |
| 63 | unsigned getNetwork(int uid, unsigned requested_netId, int pid, bool for_dns) const; |
| 64 | |
| 65 | unsigned getNetworkId(const char* interface); |
| 66 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame^] | 67 | bool createNetwork(unsigned netId, const char* interface, Permission permission); |
| 68 | bool destroyNetwork(unsigned netId); |
| 69 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 70 | private: |
| 71 | struct UidEntry { |
| 72 | int uid_start; |
| 73 | int uid_end; |
| 74 | unsigned netId; |
| 75 | bool forward_dns; |
| 76 | UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns); |
| 77 | }; |
| 78 | |
| 79 | mutable android::RWLock mRWLock; |
| 80 | std::list<UidEntry> mUidMap; |
| 81 | std::map<int, unsigned> mPidMap; |
| 82 | unsigned mDefaultNetId; |
| 83 | |
| 84 | std::map<std::string, unsigned> mIfaceNetidMap; |
| 85 | unsigned mNextFreeNetId; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame^] | 86 | |
| 87 | PermissionsController* const mPermissionsController; |
| 88 | RouteController* const mRouteController; |
| 89 | |
| 90 | std::multimap<unsigned, std::string> mNetIdToInterfaces; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | #endif |