2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "PhysicalNetwork.h"
19 #include "RouteController.h"
21 #define LOG_TAG "Netd"
26 WARN_UNUSED_RESULT int addToDefault(unsigned netId, const std::string& interface,
27 Permission permission, PhysicalNetwork::Delegate* delegate) {
28 if (int ret = RouteController::addInterfaceToDefaultNetwork(interface.c_str(), permission)) {
29 ALOGE("failed to add interface %s to default netId %u", interface.c_str(), netId);
32 if (int ret = delegate->addFallthrough(interface, permission)) {
38 WARN_UNUSED_RESULT int removeFromDefault(unsigned netId, const std::string& interface,
39 Permission permission,
40 PhysicalNetwork::Delegate* delegate) {
41 if (int ret = RouteController::removeInterfaceFromDefaultNetwork(interface.c_str(),
43 ALOGE("failed to remove interface %s from default netId %u", interface.c_str(), netId);
46 if (int ret = delegate->removeFallthrough(interface, permission)) {
54 PhysicalNetwork::Delegate::~Delegate() {
57 PhysicalNetwork::PhysicalNetwork(unsigned netId, PhysicalNetwork::Delegate* delegate) :
58 Network(netId), mDelegate(delegate), mPermission(PERMISSION_NONE), mIsDefault(false) {
61 PhysicalNetwork::~PhysicalNetwork() {
64 Permission PhysicalNetwork::getPermission() const {
68 int PhysicalNetwork::setPermission(Permission permission) {
69 if (permission == mPermission) {
72 for (const std::string& interface : mInterfaces) {
73 if (int ret = RouteController::modifyPhysicalNetworkPermission(mNetId, interface.c_str(),
74 mPermission, permission)) {
75 ALOGE("failed to change permission on interface %s of netId %u from %x to %x",
76 interface.c_str(), mNetId, mPermission, permission);
81 for (const std::string& interface : mInterfaces) {
82 if (int ret = addToDefault(mNetId, interface, permission, mDelegate)) {
85 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
90 mPermission = permission;
94 int PhysicalNetwork::addAsDefault() {
98 for (const std::string& interface : mInterfaces) {
99 if (int ret = addToDefault(mNetId, interface, mPermission, mDelegate)) {
107 int PhysicalNetwork::removeAsDefault() {
111 for (const std::string& interface : mInterfaces) {
112 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
120 Network::Type PhysicalNetwork::getType() const {
124 int PhysicalNetwork::addInterface(const std::string& interface) {
125 if (hasInterface(interface)) {
128 if (int ret = RouteController::addInterfaceToPhysicalNetwork(mNetId, interface.c_str(),
130 ALOGE("failed to add interface %s to netId %u", interface.c_str(), mNetId);
134 if (int ret = addToDefault(mNetId, interface, mPermission, mDelegate)) {
138 mInterfaces.insert(interface);
142 int PhysicalNetwork::removeInterface(const std::string& interface) {
143 if (!hasInterface(interface)) {
147 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
151 // This step will flush the interface index from the cache in RouteController so it must be
152 // done last as further requests to the RouteController regarding this interface will fail
153 // to find the interface index in the cache in cases where the interface is already gone
155 if (int ret = RouteController::removeInterfaceFromPhysicalNetwork(mNetId, interface.c_str(),
157 ALOGE("failed to remove interface %s from netId %u", interface.c_str(), mNetId);
160 mInterfaces.erase(interface);