blob: 398adb03165cff5d854190e566854eae0e8e94ab [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#include <resolv_netid.h>
18
19#define LOG_TAG "NetworkController"
20#include <cutils/log.h>
21
22#include "NetworkController.h"
23
24// Mark 1 is reserved for SecondaryTableController::PROTECT_MARK.
25NetworkController::NetworkController() : mNextFreeNetId(10) {}
26
27void NetworkController::clearNetworkPreference() {
28 android::RWLock::AutoWLock lock(mRWLock);
29 mUidMap.clear();
30 mPidMap.clear();
31}
32
33unsigned NetworkController::getDefaultNetwork() const {
34 return mDefaultNetId;
35}
36
37void NetworkController::setDefaultNetwork(unsigned netId) {
38 android::RWLock::AutoWLock lock(mRWLock);
39 mDefaultNetId = netId;
40}
41
42void NetworkController::setNetworkForPid(int pid, unsigned netId) {
43 android::RWLock::AutoWLock lock(mRWLock);
44 if (netId == 0) {
45 mPidMap.erase(pid);
46 } else {
47 mPidMap[pid] = netId;
48 }
49}
50
51bool NetworkController::setNetworkForUidRange(int uid_start, int uid_end, unsigned netId,
52 bool forward_dns) {
53 android::RWLock::AutoWLock lock(mRWLock);
Paul Jensen5b49ab92014-04-03 19:06:00 -040054 if (uid_start > uid_end || netId == NETID_UNSET)
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050055 return false;
56
57 for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
Paul Jensen5b49ab92014-04-03 19:06:00 -040058 if (it->uid_start != uid_start || it->uid_end != uid_end || it->netId != netId)
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050059 continue;
Paul Jensen5b49ab92014-04-03 19:06:00 -040060 it->forward_dns = forward_dns;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050061 return true;
62 }
63
Paul Jensen5b49ab92014-04-03 19:06:00 -040064 mUidMap.push_front(UidEntry(uid_start, uid_end, netId, forward_dns));
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050065 return true;
66}
67
Paul Jensen5b49ab92014-04-03 19:06:00 -040068bool NetworkController::clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId) {
69 android::RWLock::AutoWLock lock(mRWLock);
70 if (uid_start > uid_end || netId == NETID_UNSET)
71 return false;
72
73 for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
74 if (it->uid_start != uid_start || it->uid_end != uid_end || it->netId != netId)
75 continue;
76 mUidMap.erase(it);
77 return true;
78 }
79 return false;
80}
81
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050082unsigned NetworkController::getNetwork(int uid, unsigned requested_netId, int pid,
83 bool for_dns) const {
84 android::RWLock::AutoRLock lock(mRWLock);
85 for (std::list<UidEntry>::const_iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
86 if (uid < it->uid_start || it->uid_end < uid)
87 continue;
88 if (for_dns && !it->forward_dns)
89 break;
90 return it->netId;
91 }
92 if (requested_netId != NETID_UNSET)
93 return requested_netId;
94 if (pid != PID_UNSPECIFIED) {
95 std::map<int, unsigned>::const_iterator it = mPidMap.find(pid);
96 if (it != mPidMap.end())
97 return it->second;
98 }
99 return mDefaultNetId;
100}
101
102unsigned NetworkController::getNetworkId(const char* interface) {
103 std::map<std::string, unsigned>::const_iterator it = mIfaceNetidMap.find(interface);
104 if (it != mIfaceNetidMap.end())
105 return it->second;
106
107 unsigned netId = mNextFreeNetId++;
108 mIfaceNetidMap[interface] = netId;
109 return netId;
110}
111
112NetworkController::UidEntry::UidEntry(
113 int start, int end, unsigned netId, bool forward_dns)
114 : uid_start(start),
115 uid_end(end),
116 netId(netId),
117 forward_dns(forward_dns) {
118}