blob: f1c8fda53e66dd7b783f82ff83571a95a066834a [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:
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070042 static bool isNetIdValid(unsigned netId);
43
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -070044 NetworkController(PermissionsController* permissionsController,
45 RouteController* routeController);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050046
47 void clearNetworkPreference();
48 unsigned getDefaultNetwork() const;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070049 bool setDefaultNetwork(unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050050 bool setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, bool forward_dns);
Paul Jensen5b49ab92014-04-03 19:06:00 -040051 bool clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050052
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 Jakubczaka0efaec2014-02-14 17:09:43 -050055 // 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 Jensen35c77e32014-04-10 14:57:54 -040057 unsigned getNetwork(int uid, unsigned requested_netId, bool for_dns) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050058
59 unsigned getNetworkId(const char* interface);
60
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070061 bool createNetwork(unsigned netId, const char* interface, Permission permission);
62 bool destroyNetwork(unsigned netId);
63
Sreeram Ramachandran379bd332014-04-10 19:58:06 -070064 bool setPermissionForUser(Permission permission, const std::vector<unsigned>& uid);
65 bool setPermissionForNetwork(Permission permission, const std::vector<unsigned>& netId);
66
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070067 // 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 Jakubczaka0efaec2014-02-14 17:09:43 -050074private:
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070075 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 Jakubczaka0efaec2014-02-14 17:09:43 -050087 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 Jakubczaka0efaec2014-02-14 17:09:43 -050097 unsigned mDefaultNetId;
98
99 std::map<std::string, unsigned> mIfaceNetidMap;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700100
101 PermissionsController* const mPermissionsController;
102 RouteController* const mRouteController;
103
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700104 // 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 Ramachandran5c181bf2014-04-07 14:10:04 -0700110 std::multimap<unsigned, std::string> mNetIdToInterfaces;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500111};
112
113#endif