blob: 479af5eb41499df0099c5b6c02e34755c9ed4d27 [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 Ramachandranf4f6c8d2014-06-23 09:54:06 -070020#include "NetdConstants.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070021#include "Permission.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070022
Sreeram Ramachandran72604072014-05-21 13:19:43 -070023#include "utils/RWLock.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070024
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050025#include <list>
26#include <map>
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070027#include <set>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070028#include <sys/types.h>
Sreeram Ramachandran72604072014-05-21 13:19:43 -070029#include <vector>
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050030
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070031class Network;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070032class UidRanges;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070033class VirtualNetwork;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070034
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 Ramachandranf4f6c8d2014-06-23 09:54:06 -070043 NetworkController();
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050044
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050045 unsigned getDefaultNetwork() const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070046 int setDefaultNetwork(unsigned netId) WARN_UNUSED_RESULT;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050047
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070048 // Order of preference: UID-specific, requestedNetId, default.
49 // Specify NETID_UNSET for requestedNetId if the default network is preferred.
50 // forDns indicates if we're querying the netId for a DNS request. This avoids sending DNS
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050051 // requests to VPNs without DNS servers.
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070052 unsigned getNetworkForUser(uid_t uid, unsigned requestedNetId, bool forDns) const;
53 unsigned getNetworkForInterface(const char* interface) const;
Sreeram Ramachandran070b2d22014-07-11 17:06:12 -070054 bool isVirtualNetwork(unsigned netId) const;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050055
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070056 // TODO: Remove this hack.
57 unsigned getNetIdForLocalNetwork() const;
58
59 int createLocalNetwork(unsigned netId) WARN_UNUSED_RESULT;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070060 int createPhysicalNetwork(unsigned netId, Permission permission) WARN_UNUSED_RESULT;
61 int createVirtualNetwork(unsigned netId, bool hasDns) WARN_UNUSED_RESULT;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070062 int destroyNetwork(unsigned netId) WARN_UNUSED_RESULT;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070063
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070064 int addInterfaceToNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
65 int removeInterfaceFromNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
66
67 Permission getPermissionForUser(uid_t uid) const;
68 void setPermissionForUsers(Permission permission, const std::vector<uid_t>& uids);
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070069 bool canUserSelectNetwork(uid_t uid, unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070070 int setPermissionForNetworks(Permission permission,
71 const std::vector<unsigned>& netIds) WARN_UNUSED_RESULT;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -070072
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070073 int addUsersToNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
74 int removeUsersFromNetwork(unsigned netId, const UidRanges& uidRanges) WARN_UNUSED_RESULT;
75
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070076 // Routes are added to tables determined by the interface, so only |interface| is actually used.
77 // |netId| is given only to sanity check that the interface has the correct netId.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090078 int addRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070079 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090080 int removeRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070081 const char* nexthop, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran72604072014-05-21 13:19:43 -070082
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070083 bool canProtect(uid_t uid) const;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070084 void allowProtect(const std::vector<uid_t>& uids);
85 void denyProtect(const std::vector<uid_t>& uids);
86
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050087private:
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070088 bool isValidNetwork(unsigned netId) const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070089 Network* getNetworkLocked(unsigned netId) const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070090 VirtualNetwork* getVirtualNetworkForUserLocked(uid_t uid) const;
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -070091 Permission getPermissionForUserLocked(uid_t uid) const;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070092
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090093 int modifyRoute(unsigned netId, const char* interface, const char* destination,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070094 const char* nexthop, bool add, bool legacy, uid_t uid) WARN_UNUSED_RESULT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070095
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070096 // mRWLock guards all accesses to mDefaultNetId, mNetworks, mUsers and mProtectableUsers.
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050097 mutable android::RWLock mRWLock;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050098 unsigned mDefaultNetId;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070099 std::map<unsigned, Network*> mNetworks; // Map keys are NetIds.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700100 std::map<uid_t, Permission> mUsers;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -0700101 std::set<uid_t> mProtectableUsers;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -0500102};
103
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700104#endif // NETD_SERVER_NETWORK_CONTROLLER_H