blob: 395bea42f1b2ff9f255fc7ad0402e08529326f15 [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,
27 Permission permission) {
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 }
32 return 0;
33}
34
35WARN_UNUSED_RESULT int removeFromDefault(unsigned netId, const std::string& interface,
36 Permission permission) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070037 if (int ret = RouteController::removeInterfaceFromDefaultNetwork(interface.c_str(),
38 permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070039 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
47PhysicalNetwork::PhysicalNetwork(unsigned netId) :
48 Network(netId), mPermission(PERMISSION_NONE), mIsDefault(false) {
49}
50
51PhysicalNetwork::~PhysicalNetwork() {
52}
53
54Permission PhysicalNetwork::getPermission() const {
55 return mPermission;
56}
57
58int PhysicalNetwork::setPermission(Permission permission) {
59 if (permission == mPermission) {
60 return 0;
61 }
62 for (const std::string& interface : mInterfaces) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070063 if (int ret = RouteController::modifyPhysicalNetworkPermission(mNetId, interface.c_str(),
64 mPermission, permission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070065 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
84int 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
97int 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 Ramachandran36ed53e2014-07-01 19:01:56 -0700110Network::Type PhysicalNetwork::getType() const {
111 return PHYSICAL;
112}
113
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700114int PhysicalNetwork::addInterface(const std::string& interface) {
115 if (hasInterface(interface)) {
116 return 0;
117 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700118 if (int ret = RouteController::addInterfaceToPhysicalNetwork(mNetId, interface.c_str(),
119 mPermission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700120 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
132int PhysicalNetwork::removeInterface(const std::string& interface) {
133 if (!hasInterface(interface)) {
134 return 0;
135 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700136 if (int ret = RouteController::removeInterfaceFromPhysicalNetwork(mNetId, interface.c_str(),
137 mPermission)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700138 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}