blob: dad011dce16683b919974125a9f9677f6e4694e5 [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
20#include <list>
21#include <map>
22#include <string>
23
24#include <stddef.h>
25#include <stdint.h>
26#include <utils/RWLock.h>
27
28/*
29 * Keeps track of default, per-pid, and per-uid-range network selection, as
30 * well as the mark associated with each network. Networks are identified
31 * by netid. In all set* commands netid == 0 means "unspecified" and is
32 * equivalent to clearing the mapping.
33 */
34class NetworkController {
35public:
36 enum {
37 // For use with getNetwork().
38 PID_UNSPECIFIED = 0,
39 };
40
41 NetworkController();
42
43 void clearNetworkPreference();
44 unsigned getDefaultNetwork() const;
45 void setDefaultNetwork(unsigned netId);
46 void setNetworkForPid(int pid, unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050047 bool setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, bool forward_dns);
Paul Jensen5b49ab92014-04-03 19:06:00 -040048 bool clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050049
50 // Order of preference: UID-specific, requested_netId, PID-specific, default.
51 // Specify NETID_UNSET for requested_netId if the default network is preferred.
52 // Specify PID_UNSPECIFIED for pid to ignore PID-specific overrides.
53 // for_dns indicates if we're querrying the netId for a DNS request. This avoids sending DNS
54 // requests to VPNs without DNS servers.
55 unsigned getNetwork(int uid, unsigned requested_netId, int pid, bool for_dns) const;
56
57 unsigned getNetworkId(const char* interface);
58
59private:
60 struct UidEntry {
61 int uid_start;
62 int uid_end;
63 unsigned netId;
64 bool forward_dns;
65 UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns);
66 };
67
68 mutable android::RWLock mRWLock;
69 std::list<UidEntry> mUidMap;
70 std::map<int, unsigned> mPidMap;
71 unsigned mDefaultNetId;
72
73 std::map<std::string, unsigned> mIfaceNetidMap;
74 unsigned mNextFreeNetId;
75};
76
77#endif