blob: 52ab7f430f61598bcc2562929fcc93b958bbed11 [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);
47 // Returns false if a partially overlapping range exists.
48 // Specify NETID_UNSET for netId to clear a mapping.
49 bool setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, bool forward_dns);
50
51 // Order of preference: UID-specific, requested_netId, PID-specific, default.
52 // Specify NETID_UNSET for requested_netId if the default network is preferred.
53 // Specify PID_UNSPECIFIED for pid to ignore PID-specific overrides.
54 // for_dns indicates if we're querrying the netId for a DNS request. This avoids sending DNS
55 // requests to VPNs without DNS servers.
56 unsigned getNetwork(int uid, unsigned requested_netId, int pid, bool for_dns) const;
57
58 unsigned getNetworkId(const char* interface);
59
60private:
61 struct UidEntry {
62 int uid_start;
63 int uid_end;
64 unsigned netId;
65 bool forward_dns;
66 UidEntry(int uid_start, int uid_end, unsigned netId, bool forward_dns);
67 };
68
69 mutable android::RWLock mRWLock;
70 std::list<UidEntry> mUidMap;
71 std::map<int, unsigned> mPidMap;
72 unsigned mDefaultNetId;
73
74 std::map<std::string, unsigned> mIfaceNetidMap;
75 unsigned mNextFreeNetId;
76};
77
78#endif