Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [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 "PhysicalNetwork.h" |
| 18 | |
| 19 | #include "RouteController.h" |
| 20 | |
| 21 | #define LOG_TAG "Netd" |
| 22 | #include "log/log.h" |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | WARN_UNUSED_RESULT int addToDefault(unsigned netId, const std::string& interface, |
| 27 | Permission permission) { |
| 28 | if (int ret = RouteController::addToDefaultNetwork(interface.c_str(), permission)) { |
| 29 | ALOGE("failed to add interface %s to default netId %u", interface.c_str(), netId); |
| 30 | return ret; |
| 31 | } |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | WARN_UNUSED_RESULT int removeFromDefault(unsigned netId, const std::string& interface, |
| 36 | Permission permission) { |
| 37 | if (int ret = RouteController::removeFromDefaultNetwork(interface.c_str(), permission)) { |
| 38 | ALOGE("failed to remove interface %s from default netId %u", interface.c_str(), netId); |
| 39 | return ret; |
| 40 | } |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | PhysicalNetwork::PhysicalNetwork(unsigned netId) : |
| 47 | Network(netId), mPermission(PERMISSION_NONE), mIsDefault(false) { |
| 48 | } |
| 49 | |
| 50 | PhysicalNetwork::~PhysicalNetwork() { |
| 51 | } |
| 52 | |
| 53 | Permission PhysicalNetwork::getPermission() const { |
| 54 | return mPermission; |
| 55 | } |
| 56 | |
| 57 | int PhysicalNetwork::setPermission(Permission permission) { |
| 58 | if (permission == mPermission) { |
| 59 | return 0; |
| 60 | } |
| 61 | for (const std::string& interface : mInterfaces) { |
| 62 | if (int ret = RouteController::modifyNetworkPermission(mNetId, interface.c_str(), |
| 63 | mPermission, permission)) { |
| 64 | ALOGE("failed to change permission on interface %s of netId %u from %x to %x", |
| 65 | interface.c_str(), mNetId, mPermission, permission); |
| 66 | return ret; |
| 67 | } |
| 68 | } |
| 69 | if (mIsDefault) { |
| 70 | for (const std::string& interface : mInterfaces) { |
| 71 | if (int ret = addToDefault(mNetId, interface, permission)) { |
| 72 | return ret; |
| 73 | } |
| 74 | if (int ret = removeFromDefault(mNetId, interface, mPermission)) { |
| 75 | return ret; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | mPermission = permission; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int PhysicalNetwork::addAsDefault() { |
| 84 | if (mIsDefault) { |
| 85 | return 0; |
| 86 | } |
| 87 | for (const std::string& interface : mInterfaces) { |
| 88 | if (int ret = addToDefault(mNetId, interface, mPermission)) { |
| 89 | return ret; |
| 90 | } |
| 91 | } |
| 92 | mIsDefault = true; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int PhysicalNetwork::removeAsDefault() { |
| 97 | if (!mIsDefault) { |
| 98 | return 0; |
| 99 | } |
| 100 | for (const std::string& interface : mInterfaces) { |
| 101 | if (int ret = removeFromDefault(mNetId, interface, mPermission)) { |
| 102 | return ret; |
| 103 | } |
| 104 | } |
| 105 | mIsDefault = false; |
| 106 | return 0; |
| 107 | } |
| 108 | |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame^] | 109 | Network::Type PhysicalNetwork::getType() const { |
| 110 | return PHYSICAL; |
| 111 | } |
| 112 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 113 | int PhysicalNetwork::addInterface(const std::string& interface) { |
| 114 | if (hasInterface(interface)) { |
| 115 | return 0; |
| 116 | } |
| 117 | if (int ret = RouteController::addInterfaceToNetwork(mNetId, interface.c_str(), mPermission)) { |
| 118 | ALOGE("failed to add interface %s to netId %u", interface.c_str(), mNetId); |
| 119 | return ret; |
| 120 | } |
| 121 | if (mIsDefault) { |
| 122 | if (int ret = addToDefault(mNetId, interface, mPermission)) { |
| 123 | return ret; |
| 124 | } |
| 125 | } |
| 126 | mInterfaces.insert(interface); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int PhysicalNetwork::removeInterface(const std::string& interface) { |
| 131 | if (!hasInterface(interface)) { |
| 132 | return 0; |
| 133 | } |
| 134 | if (int ret = RouteController::removeInterfaceFromNetwork(mNetId, interface.c_str(), |
| 135 | mPermission)) { |
| 136 | ALOGE("failed to remove interface %s from netId %u", interface.c_str(), mNetId); |
| 137 | return ret; |
| 138 | } |
| 139 | if (mIsDefault) { |
| 140 | if (int ret = removeFromDefault(mNetId, interface, mPermission)) { |
| 141 | return ret; |
| 142 | } |
| 143 | } |
| 144 | mInterfaces.erase(interface); |
| 145 | return 0; |
| 146 | } |