blob: c51fc3dae0980edcfdc83034396695f9f16a1134 [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>
25
26#include <stddef.h>
27#include <stdint.h>
28#include <utils/RWLock.h>
29
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070030class PermissionsController;
31class RouteController;
32
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050033/*
34 * Keeps track of default, per-pid, and per-uid-range network selection, as
35 * well as the mark associated with each network. Networks are identified
36 * by netid. In all set* commands netid == 0 means "unspecified" and is
37 * equivalent to clearing the mapping.
38 */
39class NetworkController {
40public:
41 enum {
42 // For use with getNetwork().
43 PID_UNSPECIFIED = 0,
44 };
45
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070046 static bool isNetIdValid(unsigned netId);
47
48 NetworkController(PermissionsController* permCtrl,
49 RouteController* routeCtrl);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050050
51 void clearNetworkPreference();
52 unsigned getDefaultNetwork() const;
53 void setDefaultNetwork(unsigned netId);
54 void setNetworkForPid(int pid, unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050055 bool setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, bool forward_dns);
Paul Jensen5b49ab92014-04-03 19:06:00 -040056 bool clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050057
58 // Order of preference: UID-specific, requested_netId, PID-specific, default.
59 // Specify NETID_UNSET for requested_netId if the default network is preferred.
60 // Specify PID_UNSPECIFIED for pid to ignore PID-specific overrides.
61 // for_dns indicates if we're querrying the netId for a DNS request. This avoids sending DNS
62 // requests to VPNs without DNS servers.
63 unsigned getNetwork(int uid, unsigned requested_netId, int pid, bool for_dns) const;
64
65 unsigned getNetworkId(const char* interface);
66
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070067 bool createNetwork(unsigned netId, const char* interface, Permission permission);
68 bool destroyNetwork(unsigned netId);
69
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050070private:
71 struct UidEntry {
72 int uid_start;
73 int uid_end;
74 unsigned netId;
75 bool forward_dns;
76 UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns);
77 };
78
79 mutable android::RWLock mRWLock;
80 std::list<UidEntry> mUidMap;
81 std::map<int, unsigned> mPidMap;
82 unsigned mDefaultNetId;
83
84 std::map<std::string, unsigned> mIfaceNetidMap;
85 unsigned mNextFreeNetId;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070086
87 PermissionsController* const mPermissionsController;
88 RouteController* const mRouteController;
89
90 std::multimap<unsigned, std::string> mNetIdToInterfaces;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050091};
92
93#endif