blob: 1063f929ff70d70236129cec5e9c045c7bbfa28d [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;
54 void setDefaultNetwork(unsigned netId);
55 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
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050074private:
75 struct UidEntry {
76 int uid_start;
77 int uid_end;
78 unsigned netId;
79 bool forward_dns;
80 UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns);
81 };
82
83 mutable android::RWLock mRWLock;
84 std::list<UidEntry> mUidMap;
85 std::map<int, unsigned> mPidMap;
86 unsigned mDefaultNetId;
87
88 std::map<std::string, unsigned> mIfaceNetidMap;
89 unsigned mNextFreeNetId;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070090
91 PermissionsController* const mPermissionsController;
92 RouteController* const mRouteController;
93
94 std::multimap<unsigned, std::string> mNetIdToInterfaces;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050095};
96
97#endif