blob: e18e1cdbee6d04755faf2bd923c5832c35ef0d9b [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
Bernie Innocenti762dcf42019-06-14 19:52:49 +090017#pragma once
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070018
19#include "NetdConstants.h"
Ken Chend15bcfc2020-12-04 00:08:54 +080020#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070021
22#include <set>
23#include <string>
24
Bernie Innocenti762dcf42019-06-14 19:52:49 +090025namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090026
Ken Chen53360bf2021-12-10 02:41:05 +080027typedef std::map<int32_t, UidRanges> UidRangeMap;
Ken Chen4ea88462021-05-23 14:56:43 +080028
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070029// A Network represents a collection of interfaces participating as a single administrative unit.
30class Network {
31public:
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070032 // You MUST ensure that no interfaces are still assigned to this network, say by calling
33 // clearInterfaces(), before deleting it. This is because interface removal may fail. If we
34 // automatically removed interfaces in the destructor, you wouldn't know if it failed.
35 virtual ~Network();
36
Ken Chen2f661522021-03-30 19:41:49 +080037 virtual std::string getTypeString() const = 0;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070038 unsigned getNetId() const;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070039
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070040 bool hasInterface(const std::string& interface) const;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070041 const std::set<std::string>& getInterfaces() const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070042
43 // These return 0 on success or negative errno on failure.
Ken Chenb5736482021-03-24 18:12:01 +080044 [[nodiscard]] virtual int addInterface(const std::string&) { return -EINVAL; }
45 [[nodiscard]] virtual int removeInterface(const std::string&) { return -EINVAL; }
Bernie Innocenti762dcf42019-06-14 19:52:49 +090046 [[nodiscard]] int clearInterfaces();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070047
Erik Kline2d3a1632016-03-15 16:33:48 +090048 std::string toString() const;
Ken Chen8e0ba5a2021-06-11 03:29:45 +080049 std::string uidRangesToString() const;
Ken Chen53360bf2021-12-10 02:41:05 +080050 bool appliesToUser(uid_t uid, int32_t* subPriority) const;
51 [[nodiscard]] virtual int addUsers(const UidRanges&, int32_t /*subPriority*/) {
Ken Chen4ea88462021-05-23 14:56:43 +080052 return -EINVAL;
53 };
Ken Chen53360bf2021-12-10 02:41:05 +080054 [[nodiscard]] virtual int removeUsers(const UidRanges&, int32_t /*subPriority*/) {
Ken Chen4ea88462021-05-23 14:56:43 +080055 return -EINVAL;
56 };
Ken Chen1a3a3272020-12-04 04:03:08 +080057 bool isSecure() const;
Ken Chen6559f1a2021-03-30 16:29:50 +080058 virtual bool isPhysical() { return false; }
59 virtual bool isUnreachable() { return false; }
60 virtual bool isVirtual() { return false; }
61 virtual bool canAddUsers() { return false; }
Ken Chen53360bf2021-12-10 02:41:05 +080062 virtual bool isValidSubPriority(int32_t /*priority*/) { return false; }
63 virtual void addToUidRangeMap(const UidRanges& uidRanges, int32_t subPriority);
64 virtual void removeFromUidRangeMap(const UidRanges& uidRanges, int32_t subPriority);
Erik Kline2d3a1632016-03-15 16:33:48 +090065
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070066protected:
Ken Chen1101a652022-01-14 13:27:44 +000067 explicit Network(unsigned netId, bool secure = false);
Ken Chen53360bf2021-12-10 02:41:05 +080068 bool canAddUidRanges(const UidRanges& uidRanges, int32_t subPriority) const;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070069
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070070 const unsigned mNetId;
71 std::set<std::string> mInterfaces;
Ken Chen4ea88462021-05-23 14:56:43 +080072 // Each subsidiary priority maps to a set of UID ranges of a feature.
Ken Chen53360bf2021-12-10 02:41:05 +080073 std::map<int32_t, UidRanges> mUidRangeMap;
Ken Chen1a3a3272020-12-04 04:03:08 +080074 const bool mSecure;
75
76private:
77 enum Action {
78 REMOVE,
79 ADD,
80 };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070081};
82
Bernie Innocenti762dcf42019-06-14 19:52:49 +090083} // namespace android::net