Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 1 | /* |
| 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 Jensen | be1e7d8 | 2014-04-17 13:35:33 -0400 | [diff] [blame] | 25 | NetworkController::NetworkController() |
| 26 | : mDefaultNetId(NETID_UNSET), |
| 27 | mNextFreeNetId(10) {} |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 28 | |
| 29 | void NetworkController::clearNetworkPreference() { |
| 30 | android::RWLock::AutoWLock lock(mRWLock); |
| 31 | mUidMap.clear(); |
| 32 | mPidMap.clear(); |
| 33 | } |
| 34 | |
| 35 | unsigned NetworkController::getDefaultNetwork() const { |
| 36 | return mDefaultNetId; |
| 37 | } |
| 38 | |
| 39 | void NetworkController::setDefaultNetwork(unsigned netId) { |
| 40 | android::RWLock::AutoWLock lock(mRWLock); |
| 41 | mDefaultNetId = netId; |
| 42 | } |
| 43 | |
| 44 | void 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 | |
| 53 | bool NetworkController::setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, |
| 54 | bool forward_dns) { |
| 55 | android::RWLock::AutoWLock lock(mRWLock); |
Paul Jensen | 5b49ab9 | 2014-04-03 19:06:00 -0400 | [diff] [blame] | 56 | if (uid_start > uid_end || netId == NETID_UNSET) |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 57 | return false; |
| 58 | |
| 59 | for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) { |
Paul Jensen | 5b49ab9 | 2014-04-03 19:06:00 -0400 | [diff] [blame] | 60 | if (it->uid_start != uid_start || it->uid_end != uid_end || it->netId != netId) |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 61 | continue; |
Paul Jensen | 5b49ab9 | 2014-04-03 19:06:00 -0400 | [diff] [blame] | 62 | it->forward_dns = forward_dns; |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | |
Paul Jensen | 5b49ab9 | 2014-04-03 19:06:00 -0400 | [diff] [blame] | 66 | mUidMap.push_front(UidEntry(uid_start, uid_end, netId, forward_dns)); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
Paul Jensen | 5b49ab9 | 2014-04-03 19:06:00 -0400 | [diff] [blame] | 70 | bool 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 Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 84 | unsigned 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 | |
| 104 | unsigned 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 | |
| 114 | NetworkController::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 | } |