blob: 6b68defc5d59ae2208fc2a65a8e47b1a3ca31698 [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 Chen1a028a72022-10-27 17:54:38 +080020#include "Permission.h"
Ken Chend15bcfc2020-12-04 00:08:54 +080021#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070022
23#include <set>
24#include <string>
25
Bernie Innocenti762dcf42019-06-14 19:52:49 +090026namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090027
Ken Chen53360bf2021-12-10 02:41:05 +080028typedef std::map<int32_t, UidRanges> UidRangeMap;
Ken Chen4ea88462021-05-23 14:56:43 +080029
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070030// A Network represents a collection of interfaces participating as a single administrative unit.
31class Network {
32public:
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070033 // 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 Chen2f661522021-03-30 19:41:49 +080038 virtual std::string getTypeString() const = 0;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070039 unsigned getNetId() const;
Sreeram Ramachandran36ed53e2014-07-01 19:01:56 -070040
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070041 bool hasInterface(const std::string& interface) const;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070042 const std::set<std::string>& getInterfaces() const;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070043
44 // These return 0 on success or negative errno on failure.
Ken Chenb5736482021-03-24 18:12:01 +080045 [[nodiscard]] virtual int addInterface(const std::string&) { return -EINVAL; }
46 [[nodiscard]] virtual int removeInterface(const std::string&) { return -EINVAL; }
Bernie Innocenti762dcf42019-06-14 19:52:49 +090047 [[nodiscard]] int clearInterfaces();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070048
Erik Kline2d3a1632016-03-15 16:33:48 +090049 std::string toString() const;
Ken Chen8e0ba5a2021-06-11 03:29:45 +080050 std::string uidRangesToString() const;
Ken Chen0c209f82022-12-22 15:11:39 +080051 std::string allowedUidsToString() const;
Ken Chen53360bf2021-12-10 02:41:05 +080052 bool appliesToUser(uid_t uid, int32_t* subPriority) const;
Ken Chen1a028a72022-10-27 17:54:38 +080053 virtual Permission getPermission() const = 0;
Ken Chen53360bf2021-12-10 02:41:05 +080054 [[nodiscard]] virtual int addUsers(const UidRanges&, int32_t /*subPriority*/) {
Ken Chen4ea88462021-05-23 14:56:43 +080055 return -EINVAL;
56 };
Ken Chen53360bf2021-12-10 02:41:05 +080057 [[nodiscard]] virtual int removeUsers(const UidRanges&, int32_t /*subPriority*/) {
Ken Chen4ea88462021-05-23 14:56:43 +080058 return -EINVAL;
59 };
Ken Chen1a3a3272020-12-04 04:03:08 +080060 bool isSecure() const;
Ken Chen6559f1a2021-03-30 16:29:50 +080061 virtual bool isPhysical() { return false; }
62 virtual bool isUnreachable() { return false; }
63 virtual bool isVirtual() { return false; }
64 virtual bool canAddUsers() { return false; }
Ken Chen53360bf2021-12-10 02:41:05 +080065 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 Chen0c209f82022-12-22 15:11:39 +080068 void clearAllowedUids();
69 void setAllowedUids(const UidRanges& uidRanges);
70 bool isUidAllowed(uid_t uid);
Erik Kline2d3a1632016-03-15 16:33:48 +090071
Ken Chen0c209f82022-12-22 15:11:39 +080072 protected:
Ken Chen1101a652022-01-14 13:27:44 +000073 explicit Network(unsigned netId, bool secure = false);
chiachangwang65bc4ea2022-09-07 08:10:30 +000074 bool canAddUidRanges(const UidRanges& uidRanges) const;
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070075
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070076 const unsigned mNetId;
77 std::set<std::string> mInterfaces;
Ken Chen4ea88462021-05-23 14:56:43 +080078 // Each subsidiary priority maps to a set of UID ranges of a feature.
Ken Chen53360bf2021-12-10 02:41:05 +080079 std::map<int32_t, UidRanges> mUidRangeMap;
Ken Chen1a3a3272020-12-04 04:03:08 +080080 const bool mSecure;
Ken Chen0c209f82022-12-22 15:11:39 +080081 // UIDs that can explicitly select this network. It means no restriction for all UIDs if the
82 // optional variable has no value.
Ken Chen04ee6092022-12-26 17:35:33 +080083 std::optional<UidRanges> mAllowedUids;
Ken Chen1a3a3272020-12-04 04:03:08 +080084
Ken Chen0c209f82022-12-22 15:11:39 +080085 private:
Ken Chen1a3a3272020-12-04 04:03:08 +080086 enum Action {
87 REMOVE,
88 ADD,
89 };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070090};
91
Bernie Innocenti762dcf42019-06-14 19:52:49 +090092} // namespace android::net