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 | */ |
| 16 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 17 | #pragma once |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 18 | |
| 19 | #include "NetdConstants.h" |
Ken Chen | 1a028a7 | 2022-10-27 17:54:38 +0800 | [diff] [blame] | 20 | #include "Permission.h" |
Ken Chen | d15bcfc | 2020-12-04 00:08:54 +0800 | [diff] [blame] | 21 | #include "UidRanges.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 22 | |
| 23 | #include <set> |
| 24 | #include <string> |
| 25 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 26 | namespace android::net { |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 27 | |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 28 | typedef std::map<int32_t, UidRanges> UidRangeMap; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 29 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 30 | // A Network represents a collection of interfaces participating as a single administrative unit. |
| 31 | class Network { |
| 32 | public: |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 33 | // You MUST ensure that no interfaces are still assigned to this network, say by calling |
| 34 | // clearInterfaces(), before deleting it. This is because interface removal may fail. If we |
| 35 | // automatically removed interfaces in the destructor, you wouldn't know if it failed. |
| 36 | virtual ~Network(); |
| 37 | |
Ken Chen | 2f66152 | 2021-03-30 19:41:49 +0800 | [diff] [blame] | 38 | virtual std::string getTypeString() const = 0; |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 39 | unsigned getNetId() const; |
Sreeram Ramachandran | 36ed53e | 2014-07-01 19:01:56 -0700 | [diff] [blame] | 40 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 41 | bool hasInterface(const std::string& interface) const; |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 42 | const std::set<std::string>& getInterfaces() const; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 43 | |
| 44 | // These return 0 on success or negative errno on failure. |
Ken Chen | b573648 | 2021-03-24 18:12:01 +0800 | [diff] [blame] | 45 | [[nodiscard]] virtual int addInterface(const std::string&) { return -EINVAL; } |
| 46 | [[nodiscard]] virtual int removeInterface(const std::string&) { return -EINVAL; } |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 47 | [[nodiscard]] int clearInterfaces(); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 48 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 49 | std::string toString() const; |
Ken Chen | 8e0ba5a | 2021-06-11 03:29:45 +0800 | [diff] [blame] | 50 | std::string uidRangesToString() const; |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 51 | std::string allowedUidsToString() const; |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 52 | bool appliesToUser(uid_t uid, int32_t* subPriority) const; |
Ken Chen | 1a028a7 | 2022-10-27 17:54:38 +0800 | [diff] [blame] | 53 | virtual Permission getPermission() const = 0; |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 54 | [[nodiscard]] virtual int addUsers(const UidRanges&, int32_t /*subPriority*/) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 55 | return -EINVAL; |
| 56 | }; |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 57 | [[nodiscard]] virtual int removeUsers(const UidRanges&, int32_t /*subPriority*/) { |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 58 | return -EINVAL; |
| 59 | }; |
Ken Chen | 1a3a327 | 2020-12-04 04:03:08 +0800 | [diff] [blame] | 60 | bool isSecure() const; |
Ken Chen | 6559f1a | 2021-03-30 16:29:50 +0800 | [diff] [blame] | 61 | virtual bool isPhysical() { return false; } |
| 62 | virtual bool isUnreachable() { return false; } |
| 63 | virtual bool isVirtual() { return false; } |
| 64 | virtual bool canAddUsers() { return false; } |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 65 | virtual bool isValidSubPriority(int32_t /*priority*/) { return false; } |
| 66 | virtual void addToUidRangeMap(const UidRanges& uidRanges, int32_t subPriority); |
| 67 | virtual void removeFromUidRangeMap(const UidRanges& uidRanges, int32_t subPriority); |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 68 | void clearAllowedUids(); |
| 69 | void setAllowedUids(const UidRanges& uidRanges); |
| 70 | bool isUidAllowed(uid_t uid); |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 71 | |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 72 | protected: |
Ken Chen | 1101a65 | 2022-01-14 13:27:44 +0000 | [diff] [blame] | 73 | explicit Network(unsigned netId, bool secure = false); |
chiachangwang | 65bc4ea | 2022-09-07 08:10:30 +0000 | [diff] [blame] | 74 | bool canAddUidRanges(const UidRanges& uidRanges) const; |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 75 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 76 | const unsigned mNetId; |
| 77 | std::set<std::string> mInterfaces; |
Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 78 | // Each subsidiary priority maps to a set of UID ranges of a feature. |
Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 79 | std::map<int32_t, UidRanges> mUidRangeMap; |
Ken Chen | 1a3a327 | 2020-12-04 04:03:08 +0800 | [diff] [blame] | 80 | const bool mSecure; |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 81 | // UIDs that can explicitly select this network. It means no restriction for all UIDs if the |
| 82 | // optional variable has no value. |
Ken Chen | 04ee609 | 2022-12-26 17:35:33 +0800 | [diff] [blame] | 83 | std::optional<UidRanges> mAllowedUids; |
Ken Chen | 1a3a327 | 2020-12-04 04:03:08 +0800 | [diff] [blame] | 84 | |
Ken Chen | 0c209f8 | 2022-12-22 15:11:39 +0800 | [diff] [blame] | 85 | private: |
Ken Chen | 1a3a327 | 2020-12-04 04:03:08 +0800 | [diff] [blame] | 86 | enum Action { |
| 87 | REMOVE, |
| 88 | ADD, |
| 89 | }; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 92 | } // namespace android::net |