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. |
| 25 | NetworkController::NetworkController() : mNextFreeNetId(10) {} |
| 26 | |
| 27 | void NetworkController::clearNetworkPreference() { |
| 28 | android::RWLock::AutoWLock lock(mRWLock); |
| 29 | mUidMap.clear(); |
| 30 | mPidMap.clear(); |
| 31 | } |
| 32 | |
| 33 | unsigned NetworkController::getDefaultNetwork() const { |
| 34 | return mDefaultNetId; |
| 35 | } |
| 36 | |
| 37 | void NetworkController::setDefaultNetwork(unsigned netId) { |
| 38 | android::RWLock::AutoWLock lock(mRWLock); |
| 39 | mDefaultNetId = netId; |
| 40 | } |
| 41 | |
| 42 | void 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 | |
| 51 | bool NetworkController::setNetworkForUidRange(int uid_start, int uid_end, unsigned netId, |
| 52 | bool forward_dns) { |
| 53 | android::RWLock::AutoWLock lock(mRWLock); |
| 54 | if (uid_start > uid_end) |
| 55 | return false; |
| 56 | |
| 57 | for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) { |
| 58 | if (it->uid_start > uid_end || uid_start > it->uid_end) |
| 59 | continue; |
| 60 | /* Overlapping or identical range. */ |
| 61 | if (it->uid_start != uid_start || it->uid_end != uid_end) { |
| 62 | ALOGE("Overlapping but not identical uid range detected."); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if (netId == NETID_UNSET) { |
| 67 | mUidMap.erase(it); |
| 68 | } else { |
| 69 | it->netId = netId; |
| 70 | it->forward_dns = forward_dns; |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | mUidMap.push_back(UidEntry(uid_start, uid_end, netId, forward_dns)); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | unsigned NetworkController::getNetwork(int uid, unsigned requested_netId, int pid, |
| 80 | bool for_dns) const { |
| 81 | android::RWLock::AutoRLock lock(mRWLock); |
| 82 | for (std::list<UidEntry>::const_iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) { |
| 83 | if (uid < it->uid_start || it->uid_end < uid) |
| 84 | continue; |
| 85 | if (for_dns && !it->forward_dns) |
| 86 | break; |
| 87 | return it->netId; |
| 88 | } |
| 89 | if (requested_netId != NETID_UNSET) |
| 90 | return requested_netId; |
| 91 | if (pid != PID_UNSPECIFIED) { |
| 92 | std::map<int, unsigned>::const_iterator it = mPidMap.find(pid); |
| 93 | if (it != mPidMap.end()) |
| 94 | return it->second; |
| 95 | } |
| 96 | return mDefaultNetId; |
| 97 | } |
| 98 | |
| 99 | unsigned NetworkController::getNetworkId(const char* interface) { |
| 100 | std::map<std::string, unsigned>::const_iterator it = mIfaceNetidMap.find(interface); |
| 101 | if (it != mIfaceNetidMap.end()) |
| 102 | return it->second; |
| 103 | |
| 104 | unsigned netId = mNextFreeNetId++; |
| 105 | mIfaceNetidMap[interface] = netId; |
| 106 | return netId; |
| 107 | } |
| 108 | |
| 109 | NetworkController::UidEntry::UidEntry( |
| 110 | int start, int end, unsigned netId, bool forward_dns) |
| 111 | : uid_start(start), |
| 112 | uid_end(end), |
| 113 | netId(netId), |
| 114 | forward_dns(forward_dns) { |
| 115 | } |