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 | */ |
Ken Chen | d15bcfc | 2020-12-04 00:08:54 +0800 | [diff] [blame] | 16 | #define LOG_TAG "Netd" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 17 | |
| 18 | #include "Network.h" |
| 19 | |
Ken Chen | 1a3a327 | 2020-12-04 04:03:08 +0800 | [diff] [blame] | 20 | #include "RouteController.h" |
| 21 | #include "SockDiag.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 22 | #include "log/log.h" |
| 23 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 24 | #include <android-base/strings.h> |
| 25 | #include <sstream> |
| 26 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace net { |
| 29 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 30 | Network::~Network() { |
| 31 | if (!mInterfaces.empty()) { |
| 32 | ALOGE("deleting network with netId %u without clearing its interfaces", mNetId); |
| 33 | } |
| 34 | } |
| 35 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 36 | unsigned Network::getNetId() const { |
| 37 | return mNetId; |
| 38 | } |
| 39 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 40 | bool Network::hasInterface(const std::string& interface) const { |
| 41 | return mInterfaces.find(interface) != mInterfaces.end(); |
| 42 | } |
| 43 | |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 44 | const std::set<std::string>& Network::getInterfaces() const { |
| 45 | return mInterfaces; |
| 46 | } |
| 47 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 48 | int Network::clearInterfaces() { |
| 49 | while (!mInterfaces.empty()) { |
| 50 | // Make a copy of the string, so removeInterface() doesn't lose its parameter when it |
| 51 | // removes the string from the set. |
| 52 | std::string interface = *mInterfaces.begin(); |
| 53 | if (int ret = removeInterface(interface)) { |
| 54 | return ret; |
| 55 | } |
| 56 | } |
| 57 | return 0; |
| 58 | } |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 59 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 60 | std::string Network::toString() const { |
| 61 | const char kSeparator[] = " "; |
| 62 | std::stringstream repr; |
| 63 | |
Ken Chen | 2f66152 | 2021-03-30 19:41:49 +0800 | [diff] [blame] | 64 | repr << mNetId << kSeparator << getTypeString(); |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 65 | |
| 66 | if (mInterfaces.size() > 0) { |
| 67 | repr << kSeparator << android::base::Join(mInterfaces, ","); |
| 68 | } |
| 69 | |
| 70 | return repr.str(); |
| 71 | } |
| 72 | |
Ken Chen | 8e0ba5a | 2021-06-11 03:29:45 +0800 | [diff] [blame] | 73 | std::string Network::uidRangesToString() const { |
| 74 | if (mUidRangeMap.empty()) { |
| 75 | return ""; |
| 76 | } |
| 77 | |
| 78 | std::ostringstream result; |
| 79 | for (auto it = mUidRangeMap.begin(); it != mUidRangeMap.end(); ++it) { |
| 80 | result << "prio " << it->first << " " << it->second.toString(); |
| 81 | if (std::next(it) != mUidRangeMap.end()) result << "; "; |
| 82 | } |
| 83 | return result.str(); |
| 84 | } |
| 85 | |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 86 | std::string Network::allowedUidsToString() const { |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 87 | if (!mAllowedUids) { |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 88 | return "unrestricted"; |
| 89 | } |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 90 | return mAllowedUids->toString(); |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 91 | } |
| 92 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 93 | // Check if the user has been added to this network. If yes, the highest priority of matching |
| 94 | // setting is returned by subPriority. Thus caller can make choice among several matching |
| 95 | // networks. |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 96 | bool Network::appliesToUser(uid_t uid, int32_t* subPriority) const { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 97 | for (const auto& [priority, uidRanges] : mUidRangeMap) { |
| 98 | if (uidRanges.hasUid(uid)) { |
| 99 | *subPriority = priority; |
| 100 | return true; |
| 101 | } |
Ken Chen | 868ae63 | 2021-02-24 17:50:08 +0800 | [diff] [blame] | 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 106 | void Network::addToUidRangeMap(const UidRanges& uidRanges, int32_t subPriority) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 107 | auto iter = mUidRangeMap.find(subPriority); |
| 108 | if (iter != mUidRangeMap.end()) { |
| 109 | iter->second.add(uidRanges); |
| 110 | } else { |
| 111 | mUidRangeMap[subPriority] = uidRanges; |
| 112 | } |
| 113 | } |
| 114 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 115 | void Network::removeFromUidRangeMap(const UidRanges& uidRanges, int32_t subPriority) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 116 | auto iter = mUidRangeMap.find(subPriority); |
| 117 | if (iter != mUidRangeMap.end()) { |
| 118 | iter->second.remove(uidRanges); |
| 119 | if (iter->second.empty()) { |
| 120 | mUidRangeMap.erase(subPriority); |
| 121 | } |
| 122 | } else { |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 123 | ALOGW("uidRanges with priority %d not found", subPriority); |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 127 | void Network::clearAllowedUids() { |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 128 | mAllowedUids.reset(); |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void Network::setAllowedUids(const UidRanges& uidRanges) { |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 132 | mAllowedUids = uidRanges; |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | bool Network::isUidAllowed(uid_t uid) { |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 136 | return !mAllowedUids || mAllowedUids->hasUid(uid); |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 137 | } |
| 138 | |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 139 | bool Network::canAddUidRanges(const UidRanges& uidRanges) const { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 140 | if (uidRanges.overlapsSelf()) { |
| 141 | ALOGE("uid range %s overlaps self", uidRanges.toString().c_str()); |
| 142 | return false; |
| 143 | } |
| 144 | |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
Ken Chen | 1a3a327 | 2020-12-04 04:03:08 +0800 | [diff] [blame] | 148 | bool Network::isSecure() const { |
| 149 | return mSecure; |
| 150 | } |
| 151 | |
| 152 | Network::Network(unsigned netId, bool secure) : mNetId(netId), mSecure(secure) {} |
| 153 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 154 | } // namespace net |
| 155 | } // namespace android |