• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #pragma once
18 
19 #include "NetdConstants.h"
20 #include "UidRanges.h"
21 
22 #include <set>
23 #include <string>
24 
25 namespace android::net {
26 
27 typedef std::map<uint32_t, UidRanges> UidRangeMap;
28 
29 // A Network represents a collection of interfaces participating as a single administrative unit.
30 class Network {
31 public:
32     // 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 
37     virtual std::string getTypeString() const = 0;
38     unsigned getNetId() const;
39 
40     bool hasInterface(const std::string& interface) const;
41     const std::set<std::string>& getInterfaces() const;
42 
43     // These return 0 on success or negative errno on failure.
addInterface(const std::string &)44     [[nodiscard]] virtual int addInterface(const std::string&) { return -EINVAL; }
removeInterface(const std::string &)45     [[nodiscard]] virtual int removeInterface(const std::string&) { return -EINVAL; }
46     [[nodiscard]] int clearInterfaces();
47 
48     std::string toString() const;
49     std::string uidRangesToString() const;
50     bool appliesToUser(uid_t uid, uint32_t* subPriority) const;
addUsers(const UidRanges &,uint32_t)51     [[nodiscard]] virtual int addUsers(const UidRanges&, uint32_t /*subPriority*/) {
52         return -EINVAL;
53     };
removeUsers(const UidRanges &,uint32_t)54     [[nodiscard]] virtual int removeUsers(const UidRanges&, uint32_t /*subPriority*/) {
55         return -EINVAL;
56     };
57     bool isSecure() const;
isPhysical()58     virtual bool isPhysical() { return false; }
isUnreachable()59     virtual bool isUnreachable() { return false; }
isVirtual()60     virtual bool isVirtual() { return false; }
canAddUsers()61     virtual bool canAddUsers() { return false; }
isValidSubPriority(uint32_t)62     virtual bool isValidSubPriority(uint32_t /*priority*/) { return false; }
63     virtual void addToUidRangeMap(const UidRanges& uidRanges, uint32_t subPriority);
64     virtual void removeFromUidRangeMap(const UidRanges& uidRanges, uint32_t subPriority);
65 
66 protected:
67     explicit Network(unsigned netId, bool mSecure = false);
68     bool canAddUidRanges(const UidRanges& uidRanges, uint32_t subPriority) const;
69 
70     const unsigned mNetId;
71     std::set<std::string> mInterfaces;
72     // Each subsidiary priority maps to a set of UID ranges of a feature.
73     std::map<uint32_t, UidRanges> mUidRangeMap;
74     const bool mSecure;
75 
76 private:
77     enum Action {
78         REMOVE,
79         ADD,
80     };
81 };
82 
83 }  // namespace android::net
84