xref: /aosp_15_r20/system/netd/server/Network.h (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #pragma once
18*8542734aSAndroid Build Coastguard Worker 
19*8542734aSAndroid Build Coastguard Worker #include "NetdConstants.h"
20*8542734aSAndroid Build Coastguard Worker #include "Permission.h"
21*8542734aSAndroid Build Coastguard Worker #include "UidRanges.h"
22*8542734aSAndroid Build Coastguard Worker 
23*8542734aSAndroid Build Coastguard Worker #include <set>
24*8542734aSAndroid Build Coastguard Worker #include <string>
25*8542734aSAndroid Build Coastguard Worker 
26*8542734aSAndroid Build Coastguard Worker namespace android::net {
27*8542734aSAndroid Build Coastguard Worker 
28*8542734aSAndroid Build Coastguard Worker typedef std::map<int32_t, UidRanges> UidRangeMap;
29*8542734aSAndroid Build Coastguard Worker 
30*8542734aSAndroid Build Coastguard Worker // A Network represents a collection of interfaces participating as a single administrative unit.
31*8542734aSAndroid Build Coastguard Worker class Network {
32*8542734aSAndroid Build Coastguard Worker public:
33*8542734aSAndroid Build Coastguard Worker     // You MUST ensure that no interfaces are still assigned to this network, say by calling
34*8542734aSAndroid Build Coastguard Worker     // clearInterfaces(), before deleting it. This is because interface removal may fail. If we
35*8542734aSAndroid Build Coastguard Worker     // automatically removed interfaces in the destructor, you wouldn't know if it failed.
36*8542734aSAndroid Build Coastguard Worker     virtual ~Network();
37*8542734aSAndroid Build Coastguard Worker 
38*8542734aSAndroid Build Coastguard Worker     virtual std::string getTypeString() const = 0;
39*8542734aSAndroid Build Coastguard Worker     unsigned getNetId() const;
40*8542734aSAndroid Build Coastguard Worker 
41*8542734aSAndroid Build Coastguard Worker     bool hasInterface(const std::string& interface) const;
42*8542734aSAndroid Build Coastguard Worker     const std::set<std::string>& getInterfaces() const;
43*8542734aSAndroid Build Coastguard Worker 
44*8542734aSAndroid Build Coastguard Worker     // These return 0 on success or negative errno on failure.
addInterface(const std::string &)45*8542734aSAndroid Build Coastguard Worker     [[nodiscard]] virtual int addInterface(const std::string&) { return -EINVAL; }
removeInterface(const std::string &)46*8542734aSAndroid Build Coastguard Worker     [[nodiscard]] virtual int removeInterface(const std::string&) { return -EINVAL; }
47*8542734aSAndroid Build Coastguard Worker     [[nodiscard]] int clearInterfaces();
48*8542734aSAndroid Build Coastguard Worker 
49*8542734aSAndroid Build Coastguard Worker     std::string toString() const;
50*8542734aSAndroid Build Coastguard Worker     std::string uidRangesToString() const;
51*8542734aSAndroid Build Coastguard Worker     std::string allowedUidsToString() const;
52*8542734aSAndroid Build Coastguard Worker     bool appliesToUser(uid_t uid, int32_t* subPriority) const;
53*8542734aSAndroid Build Coastguard Worker     virtual Permission getPermission() const = 0;
addUsers(const UidRanges &,int32_t)54*8542734aSAndroid Build Coastguard Worker     [[nodiscard]] virtual int addUsers(const UidRanges&, int32_t /*subPriority*/) {
55*8542734aSAndroid Build Coastguard Worker         return -EINVAL;
56*8542734aSAndroid Build Coastguard Worker     };
removeUsers(const UidRanges &,int32_t)57*8542734aSAndroid Build Coastguard Worker     [[nodiscard]] virtual int removeUsers(const UidRanges&, int32_t /*subPriority*/) {
58*8542734aSAndroid Build Coastguard Worker         return -EINVAL;
59*8542734aSAndroid Build Coastguard Worker     };
60*8542734aSAndroid Build Coastguard Worker     bool isSecure() const;
isPhysical()61*8542734aSAndroid Build Coastguard Worker     virtual bool isPhysical() { return false; }
isUnreachable()62*8542734aSAndroid Build Coastguard Worker     virtual bool isUnreachable() { return false; }
isVirtual()63*8542734aSAndroid Build Coastguard Worker     virtual bool isVirtual() { return false; }
canAddUsers()64*8542734aSAndroid Build Coastguard Worker     virtual bool canAddUsers() { return false; }
isValidSubPriority(int32_t)65*8542734aSAndroid Build Coastguard Worker     virtual bool isValidSubPriority(int32_t /*priority*/) { return false; }
66*8542734aSAndroid Build Coastguard Worker     virtual void addToUidRangeMap(const UidRanges& uidRanges, int32_t subPriority);
67*8542734aSAndroid Build Coastguard Worker     virtual void removeFromUidRangeMap(const UidRanges& uidRanges, int32_t subPriority);
68*8542734aSAndroid Build Coastguard Worker     void clearAllowedUids();
69*8542734aSAndroid Build Coastguard Worker     void setAllowedUids(const UidRanges& uidRanges);
70*8542734aSAndroid Build Coastguard Worker     bool isUidAllowed(uid_t uid);
71*8542734aSAndroid Build Coastguard Worker 
72*8542734aSAndroid Build Coastguard Worker   protected:
73*8542734aSAndroid Build Coastguard Worker     explicit Network(unsigned netId, bool secure = false);
74*8542734aSAndroid Build Coastguard Worker     bool canAddUidRanges(const UidRanges& uidRanges) const;
75*8542734aSAndroid Build Coastguard Worker 
76*8542734aSAndroid Build Coastguard Worker     const unsigned mNetId;
77*8542734aSAndroid Build Coastguard Worker     std::set<std::string> mInterfaces;
78*8542734aSAndroid Build Coastguard Worker     // Each subsidiary priority maps to a set of UID ranges of a feature.
79*8542734aSAndroid Build Coastguard Worker     std::map<int32_t, UidRanges> mUidRangeMap;
80*8542734aSAndroid Build Coastguard Worker     const bool mSecure;
81*8542734aSAndroid Build Coastguard Worker     // UIDs that can explicitly select this network. It means no restriction for all UIDs if the
82*8542734aSAndroid Build Coastguard Worker     // optional variable has no value.
83*8542734aSAndroid Build Coastguard Worker     std::optional<UidRanges> mAllowedUids;
84*8542734aSAndroid Build Coastguard Worker 
85*8542734aSAndroid Build Coastguard Worker   private:
86*8542734aSAndroid Build Coastguard Worker     enum Action {
87*8542734aSAndroid Build Coastguard Worker         REMOVE,
88*8542734aSAndroid Build Coastguard Worker         ADD,
89*8542734aSAndroid Build Coastguard Worker     };
90*8542734aSAndroid Build Coastguard Worker };
91*8542734aSAndroid Build Coastguard Worker 
92*8542734aSAndroid Build Coastguard Worker }  // namespace android::net
93