blob: 27785d32b5b94bc6b0083e851f2fc5c4bdaaa7c2 [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
Sreeram Ramachandran72604072014-05-21 13:19:43 -070017#ifndef NETD_SERVER_NETWORK_CONTROLLER_H
18#define NETD_SERVER_NETWORK_CONTROLLER_H
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050019
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070020#include "Permission.h"
Sreeram Ramachandran72604072014-05-21 13:19:43 -070021#include "utils/RWLock.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070022
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050023#include <list>
24#include <map>
Sreeram Ramachandran72604072014-05-21 13:19:43 -070025#include <set>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050026#include <stddef.h>
27#include <stdint.h>
Sreeram Ramachandran72604072014-05-21 13:19:43 -070028#include <string>
29#include <utility>
30#include <vector>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050031
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070032class PermissionsController;
33class RouteController;
34
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050035/*
36 * Keeps track of default, per-pid, and per-uid-range network selection, as
37 * well as the mark associated with each network. Networks are identified
38 * by netid. In all set* commands netid == 0 means "unspecified" and is
39 * equivalent to clearing the mapping.
40 */
41class NetworkController {
42public:
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -070043 NetworkController(PermissionsController* permissionsController,
44 RouteController* routeController);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050045
46 void clearNetworkPreference();
47 unsigned getDefaultNetwork() const;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070048 bool setDefaultNetwork(unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050049 bool setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, bool forward_dns);
Paul Jensen5b49ab92014-04-03 19:06:00 -040050 bool clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050051
52 // Order of preference: UID-specific, requested_netId, PID-specific, default.
53 // Specify NETID_UNSET for requested_netId if the default network is preferred.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050054 // for_dns indicates if we're querrying the netId for a DNS request. This avoids sending DNS
55 // requests to VPNs without DNS servers.
Paul Jensen35c77e32014-04-10 14:57:54 -040056 unsigned getNetwork(int uid, unsigned requested_netId, bool for_dns) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050057
Sreeram Ramachandran72604072014-05-21 13:19:43 -070058 unsigned getNetworkId(const char* interface) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050059
Paul Jensenae37e8a2014-04-28 10:35:51 -040060 bool createNetwork(unsigned netId, Permission permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070061 bool destroyNetwork(unsigned netId);
Paul Jensenae37e8a2014-04-28 10:35:51 -040062 bool addInterfaceToNetwork(unsigned netId, const char* interface);
63 bool removeInterfaceFromNetwork(unsigned netId, const char* interface);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070064
Sreeram Ramachandran379bd332014-04-10 19:58:06 -070065 bool setPermissionForUser(Permission permission, const std::vector<unsigned>& uid);
66 bool setPermissionForNetwork(Permission permission, const std::vector<unsigned>& netId);
67
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070068 // Routes are added to tables determined by the interface, so only |interface| is actually used.
69 // |netId| is given only to sanity check that the interface has the correct netId.
70 bool addRoute(unsigned netId, const char* interface, const char* destination,
71 const char* nexthop);
72 bool removeRoute(unsigned netId, const char* interface, const char* destination,
73 const char* nexthop);
74
Sreeram Ramachandran72604072014-05-21 13:19:43 -070075 bool isValidNetwork(unsigned netId) const;
76
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050077private:
Sreeram Ramachandran72604072014-05-21 13:19:43 -070078 typedef std::multimap<unsigned, std::string>::const_iterator InterfaceIteratorConst;
Paul Jensenae37e8a2014-04-28 10:35:51 -040079 typedef std::multimap<unsigned, std::string>::iterator InterfaceIterator;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070080 typedef std::pair<InterfaceIterator, InterfaceIterator> InterfaceRange;
81
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070082 bool modifyRoute(unsigned netId, const char* interface, const char* destination,
83 const char* nexthop, bool add);
84
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050085 struct UidEntry {
86 int uid_start;
87 int uid_end;
88 unsigned netId;
89 bool forward_dns;
90 UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns);
91 };
92
Sreeram Ramachandran72604072014-05-21 13:19:43 -070093 // mRWLock guards all accesses to mUidMap, mDefaultNetId and mValidNetworks.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050094 mutable android::RWLock mRWLock;
95 std::list<UidEntry> mUidMap;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050096 unsigned mDefaultNetId;
Sreeram Ramachandran72604072014-05-21 13:19:43 -070097 std::set<unsigned> mValidNetworks;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070098
99 PermissionsController* const mPermissionsController;
100 RouteController* const mRouteController;
101
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700102 // Maps a netId to all its interfaces.
103 //
104 // We need to know interface names to configure incoming packet marking and because routing
105 // tables are associated with interfaces and not with netIds.
106 //
107 // An interface may belong to at most one netId, but a netId may have multiple interfaces.
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700108 std::multimap<unsigned, std::string> mNetIdToInterfaces;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500109};
110
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700111#endif // NETD_SERVER_NETWORK_CONTROLLER_H