blob: 937347be606f79184d81adf0360659f55cd658aa [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.
Paul Jensenbe1e7d82014-04-17 13:35:33 -040025NetworkController::NetworkController()
26 : mDefaultNetId(NETID_UNSET),
27 mNextFreeNetId(10) {}
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050028
29void NetworkController::clearNetworkPreference() {
30 android::RWLock::AutoWLock lock(mRWLock);
31 mUidMap.clear();
32 mPidMap.clear();
33}
34
35unsigned NetworkController::getDefaultNetwork() const {
36 return mDefaultNetId;
37}
38
39void NetworkController::setDefaultNetwork(unsigned netId) {
40 android::RWLock::AutoWLock lock(mRWLock);
41 mDefaultNetId = netId;
42}
43
44void NetworkController::setNetworkForPid(int pid, unsigned netId) {
45 android::RWLock::AutoWLock lock(mRWLock);
46 if (netId == 0) {
47 mPidMap.erase(pid);
48 } else {
49 mPidMap[pid] = netId;
50 }
51}
52
53bool NetworkController::setNetworkForUidRange(int uid_start, int uid_end, unsigned netId,
54 bool forward_dns) {
55 android::RWLock::AutoWLock lock(mRWLock);
Paul Jensen5b49ab92014-04-03 19:06:00 -040056 if (uid_start > uid_end || netId == NETID_UNSET)
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050057 return false;
58
59 for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
Paul Jensen5b49ab92014-04-03 19:06:00 -040060 if (it->uid_start != uid_start || it->uid_end != uid_end || it->netId != netId)
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050061 continue;
Paul Jensen5b49ab92014-04-03 19:06:00 -040062 it->forward_dns = forward_dns;
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050063 return true;
64 }
65
Paul Jensen5b49ab92014-04-03 19:06:00 -040066 mUidMap.push_front(UidEntry(uid_start, uid_end, netId, forward_dns));
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050067 return true;
68}
69
Paul Jensen5b49ab92014-04-03 19:06:00 -040070bool NetworkController::clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId) {
71 android::RWLock::AutoWLock lock(mRWLock);
72 if (uid_start > uid_end || netId == NETID_UNSET)
73 return false;
74
75 for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
76 if (it->uid_start != uid_start || it->uid_end != uid_end || it->netId != netId)
77 continue;
78 mUidMap.erase(it);
79 return true;
80 }
81 return false;
82}
83
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050084unsigned NetworkController::getNetwork(int uid, unsigned requested_netId, int pid,
85 bool for_dns) const {
86 android::RWLock::AutoRLock lock(mRWLock);
87 for (std::list<UidEntry>::const_iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
88 if (uid < it->uid_start || it->uid_end < uid)
89 continue;
90 if (for_dns && !it->forward_dns)
91 break;
92 return it->netId;
93 }
94 if (requested_netId != NETID_UNSET)
95 return requested_netId;
96 if (pid != PID_UNSPECIFIED) {
97 std::map<int, unsigned>::const_iterator it = mPidMap.find(pid);
98 if (it != mPidMap.end())
99 return it->second;
100 }
101 return mDefaultNetId;
102}
103
104unsigned NetworkController::getNetworkId(const char* interface) {
105 std::map<std::string, unsigned>::const_iterator it = mIfaceNetidMap.find(interface);
106 if (it != mIfaceNetidMap.end())
107 return it->second;
108
109 unsigned netId = mNextFreeNetId++;
110 mIfaceNetidMap[interface] = netId;
111 return netId;
112}
113
114NetworkController::UidEntry::UidEntry(
115 int start, int end, unsigned netId, bool forward_dns)
116 : uid_start(start),
117 uid_end(end),
118 netId(netId),
119 forward_dns(forward_dns) {
120}