blob: 62343c4b93110fcb751dcc6b47b663d02ed31a09 [file] [log] [blame]
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001/*
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
24namespace {
25
26WARN_UNUSED_RESULT int addToDefault(unsigned netId, const std::string& interface,
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070027 Permission permission, PhysicalNetwork::Delegate* delegate) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070028 if (int ret = RouteController::addInterfaceToDefaultNetwork(interface.c_str(), permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070029 ALOGE("failed to add interface %s to default netId %u", interface.c_str(), netId);
30 return ret;
31 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070032 if (int ret = delegate->addFallthrough(interface, permission)) {
33 return ret;
34 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070035 return 0;
36}
37
38WARN_UNUSED_RESULT int removeFromDefault(unsigned netId, const std::string& interface,
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070039 Permission permission,
40 PhysicalNetwork::Delegate* delegate) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070041 if (int ret = RouteController::removeInterfaceFromDefaultNetwork(interface.c_str(),
42 permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070043 ALOGE("failed to remove interface %s from default netId %u", interface.c_str(), netId);
44 return ret;
45 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070046 if (int ret = delegate->removeFallthrough(interface, permission)) {
47 return ret;
48 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070049 return 0;
50}
51
52} // namespace
53
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070054PhysicalNetwork::Delegate::~Delegate() {
55}
56
57PhysicalNetwork::PhysicalNetwork(unsigned netId, PhysicalNetwork::Delegate* delegate) :
58 Network(netId), mDelegate(delegate), mPermission(PERMISSION_NONE), mIsDefault(false) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070059}
60
61PhysicalNetwork::~PhysicalNetwork() {
62}
63
64Permission PhysicalNetwork::getPermission() const {
65 return mPermission;
66}
67
68int PhysicalNetwork::setPermission(Permission permission) {
69 if (permission == mPermission) {
70 return 0;
71 }
72 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070073 if (int ret = RouteController::modifyPhysicalNetworkPermission(mNetId, interface.c_str(),
74 mPermission, permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070075 ALOGE("failed to change permission on interface %s of netId %u from %x to %x",
76 interface.c_str(), mNetId, mPermission, permission);
77 return ret;
78 }
79 }
80 if (mIsDefault) {
81 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070082 if (int ret = addToDefault(mNetId, interface, permission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070083 return ret;
84 }
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070085 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070086 return ret;
87 }
88 }
89 }
90 mPermission = permission;
91 return 0;
92}
93
94int PhysicalNetwork::addAsDefault() {
95 if (mIsDefault) {
96 return 0;
97 }
98 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070099 if (int ret = addToDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700100 return ret;
101 }
102 }
103 mIsDefault = true;
104 return 0;
105}
106
107int PhysicalNetwork::removeAsDefault() {
108 if (!mIsDefault) {
109 return 0;
110 }
111 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700112 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700113 return ret;
114 }
115 }
116 mIsDefault = false;
117 return 0;
118}
119
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -0700120Network::Type PhysicalNetwork::getType() const {
121 return PHYSICAL;
122}
123
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700124int PhysicalNetwork::addInterface(const std::string& interface) {
125 if (hasInterface(interface)) {
126 return 0;
127 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700128 if (int ret = RouteController::addInterfaceToPhysicalNetwork(mNetId, interface.c_str(),
129 mPermission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700130 ALOGE("failed to add interface %s to netId %u", interface.c_str(), mNetId);
131 return ret;
132 }
133 if (mIsDefault) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700134 if (int ret = addToDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700135 return ret;
136 }
137 }
138 mInterfaces.insert(interface);
139 return 0;
140}
141
142int PhysicalNetwork::removeInterface(const std::string& interface) {
143 if (!hasInterface(interface)) {
144 return 0;
145 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700146 if (int ret = RouteController::removeInterfaceFromPhysicalNetwork(mNetId, interface.c_str(),
147 mPermission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700148 ALOGE("failed to remove interface %s from netId %u", interface.c_str(), mNetId);
149 return ret;
150 }
151 if (mIsDefault) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700152 if (int ret = removeFromDefault(mNetId, interface, mPermission, mDelegate)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700153 return ret;
154 }
155 }
156 mInterfaces.erase(interface);
157 return 0;
158}