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