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: |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 42 | static bool isNetIdValid(unsigned netId); |
| 43 | |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 44 | NetworkController(PermissionsController* permissionsController, |
| 45 | RouteController* routeController); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 46 | |
| 47 | void clearNetworkPreference(); |
| 48 | unsigned getDefaultNetwork() const; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 49 | bool setDefaultNetwork(unsigned netId); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 50 | 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] | 51 | bool clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 52 | |
| 53 | // Order of preference: UID-specific, requested_netId, PID-specific, default. |
| 54 | // Specify NETID_UNSET for requested_netId if the default network is preferred. |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 55 | // for_dns indicates if we're querrying the netId for a DNS request. This avoids sending DNS |
| 56 | // requests to VPNs without DNS servers. |
Paul Jensen | 35c77e3 | 2014-04-10 14:57:54 -0400 | [diff] [blame^] | 57 | unsigned getNetwork(int uid, unsigned requested_netId, bool for_dns) const; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 58 | |
| 59 | unsigned getNetworkId(const char* interface); |
| 60 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 61 | bool createNetwork(unsigned netId, const char* interface, Permission permission); |
| 62 | bool destroyNetwork(unsigned netId); |
| 63 | |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 64 | bool setPermissionForUser(Permission permission, const std::vector<unsigned>& uid); |
| 65 | bool setPermissionForNetwork(Permission permission, const std::vector<unsigned>& netId); |
| 66 | |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 67 | // Routes are added to tables determined by the interface, so only |interface| is actually used. |
| 68 | // |netId| is given only to sanity check that the interface has the correct netId. |
| 69 | bool addRoute(unsigned netId, const char* interface, const char* destination, |
| 70 | const char* nexthop); |
| 71 | bool removeRoute(unsigned netId, const char* interface, const char* destination, |
| 72 | const char* nexthop); |
| 73 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 74 | private: |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 75 | typedef std::multimap<unsigned, std::string>::const_iterator InterfaceIterator; |
| 76 | typedef std::pair<InterfaceIterator, InterfaceIterator> InterfaceRange; |
| 77 | |
| 78 | // Returns the netId that |interface| belongs to, or NETID_UNSET if it doesn't belong to any. |
| 79 | unsigned netIdForInterface(const char* interface); |
| 80 | |
| 81 | // Returns the interfaces assigned to |netId|. Sets |*status| to false if there are none. |
| 82 | InterfaceRange interfacesForNetId(unsigned netId, bool* status); |
| 83 | |
| 84 | bool modifyRoute(unsigned netId, const char* interface, const char* destination, |
| 85 | const char* nexthop, bool add); |
| 86 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 87 | struct UidEntry { |
| 88 | int uid_start; |
| 89 | int uid_end; |
| 90 | unsigned netId; |
| 91 | bool forward_dns; |
| 92 | UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns); |
| 93 | }; |
| 94 | |
| 95 | mutable android::RWLock mRWLock; |
| 96 | std::list<UidEntry> mUidMap; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 97 | unsigned mDefaultNetId; |
| 98 | |
| 99 | std::map<std::string, unsigned> mIfaceNetidMap; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 100 | |
| 101 | PermissionsController* const mPermissionsController; |
| 102 | RouteController* const mRouteController; |
| 103 | |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 104 | // Maps a netId to all its interfaces. |
| 105 | // |
| 106 | // We need to know interface names to configure incoming packet marking and because routing |
| 107 | // tables are associated with interfaces and not with netIds. |
| 108 | // |
| 109 | // An interface may belong to at most one netId, but a netId may have multiple interfaces. |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 110 | std::multimap<unsigned, std::string> mNetIdToInterfaces; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | #endif |