blob: d877fcc3de2cf4e03baae3d755fa5e581cc64c04 [file] [log] [blame]
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -05001/*
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 Ramachandran5c181bf2014-04-07 14:10:04 -070020#include "Permission.h"
21
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050022#include <list>
23#include <map>
24#include <string>
Sreeram Ramachandran379bd332014-04-10 19:58:06 -070025#include <vector>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050026
27#include <stddef.h>
28#include <stdint.h>
29#include <utils/RWLock.h>
30
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031class PermissionsController;
32class RouteController;
33
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050034/*
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 */
40class NetworkController {
41public:
42 enum {
43 // For use with getNetwork().
44 PID_UNSPECIFIED = 0,
45 };
46
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047 static bool isNetIdValid(unsigned netId);
48
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -070049 NetworkController(PermissionsController* permissionsController,
50 RouteController* routeController);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050051
52 void clearNetworkPreference();
53 unsigned getDefaultNetwork() const;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070054 bool setDefaultNetwork(unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050055 void setNetworkForPid(int pid, unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050056 bool setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, bool forward_dns);
Paul Jensen5b49ab92014-04-03 19:06:00 -040057 bool clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050058
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 Ramachandran5c181bf2014-04-07 14:10:04 -070068 bool createNetwork(unsigned netId, const char* interface, Permission permission);
69 bool destroyNetwork(unsigned netId);
70
Sreeram Ramachandran379bd332014-04-10 19:58:06 -070071 bool setPermissionForUser(Permission permission, const std::vector<unsigned>& uid);
72 bool setPermissionForNetwork(Permission permission, const std::vector<unsigned>& netId);
73
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070074 // 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 bool addRoute(unsigned netId, const char* interface, const char* destination,
77 const char* nexthop);
78 bool removeRoute(unsigned netId, const char* interface, const char* destination,
79 const char* nexthop);
80
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050081private:
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070082 typedef std::multimap<unsigned, std::string>::const_iterator InterfaceIterator;
83 typedef std::pair<InterfaceIterator, InterfaceIterator> InterfaceRange;
84
85 // Returns the netId that |interface| belongs to, or NETID_UNSET if it doesn't belong to any.
86 unsigned netIdForInterface(const char* interface);
87
88 // Returns the interfaces assigned to |netId|. Sets |*status| to false if there are none.
89 InterfaceRange interfacesForNetId(unsigned netId, bool* status);
90
91 bool modifyRoute(unsigned netId, const char* interface, const char* destination,
92 const char* nexthop, bool add);
93
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050094 struct UidEntry {
95 int uid_start;
96 int uid_end;
97 unsigned netId;
98 bool forward_dns;
99 UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns);
100 };
101
102 mutable android::RWLock mRWLock;
103 std::list<UidEntry> mUidMap;
104 std::map<int, unsigned> mPidMap;
105 unsigned mDefaultNetId;
106
107 std::map<std::string, unsigned> mIfaceNetidMap;
108 unsigned mNextFreeNetId;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700109
110 PermissionsController* const mPermissionsController;
111 RouteController* const mRouteController;
112
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700113 // Maps a netId to all its interfaces.
114 //
115 // We need to know interface names to configure incoming packet marking and because routing
116 // tables are associated with interfaces and not with netIds.
117 //
118 // An interface may belong to at most one netId, but a netId may have multiple interfaces.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700119 std::multimap<unsigned, std::string> mNetIdToInterfaces;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500120};
121
122#endif