blob: 2e9d71d786e4f442c8d2ff33241248366c538061 [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) {
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
35WARN_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
46PhysicalNetwork::PhysicalNetwork(unsigned netId) :
47 Network(netId), mPermission(PERMISSION_NONE), mIsDefault(false) {
48}
49
50PhysicalNetwork::~PhysicalNetwork() {
51}
52
53Permission PhysicalNetwork::getPermission() const {
54 return mPermission;
55}
56
57int 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
83int 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
96int 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
109int PhysicalNetwork::addInterface(const std::string& interface) {
110 if (hasInterface(interface)) {
111 return 0;
112 }
113 if (int ret = RouteController::addInterfaceToNetwork(mNetId, interface.c_str(), mPermission)) {
114 ALOGE("failed to add interface %s to netId %u", interface.c_str(), mNetId);
115 return ret;
116 }
117 if (mIsDefault) {
118 if (int ret = addToDefault(mNetId, interface, mPermission)) {
119 return ret;
120 }
121 }
122 mInterfaces.insert(interface);
123 return 0;
124}
125
126int PhysicalNetwork::removeInterface(const std::string& interface) {
127 if (!hasInterface(interface)) {
128 return 0;
129 }
130 if (int ret = RouteController::removeInterfaceFromNetwork(mNetId, interface.c_str(),
131 mPermission)) {
132 ALOGE("failed to remove interface %s from netId %u", interface.c_str(), mNetId);
133 return ret;
134 }
135 if (mIsDefault) {
136 if (int ret = removeFromDefault(mNetId, interface, mPermission)) {
137 return ret;
138 }
139 }
140 mInterfaces.erase(interface);
141 return 0;
142}