blob: 20c8dee372f28f7b51ed2881ace4a70fe6ed7d97 [file] [log] [blame]
Sreeram Ramachandran4043f012014-06-23 12:41:37 -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 Ramachandran4043f012014-06-23 12:41:37 -070018
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090019#include <set>
20
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070021#include "Network.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070022#include "UidRanges.h"
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070023
Bernie Innocenti762dcf42019-06-14 19:52:49 +090024namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090025
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070026// A VirtualNetwork may be "secure" or not.
27//
28// A secure VPN is the usual type of VPN that grabs the default route (and thus all user traffic).
29// Only a few privileged UIDs may skip the VPN and go directly to the underlying physical network.
30//
31// A non-secure VPN ("bypassable" VPN) also grabs all user traffic by default. But all apps are
32// permitted to skip it and pick any other network for their connections.
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070033class VirtualNetwork : public Network {
34public:
cken67cd14c2018-12-05 17:26:59 +090035 VirtualNetwork(unsigned netId, bool secure);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070036 virtual ~VirtualNetwork();
37
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070038 bool isSecure() const;
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070039 bool appliesToUser(uid_t uid) const;
40
Bernie Innocenti762dcf42019-06-14 19:52:49 +090041 [[nodiscard]] int addUsers(const UidRanges& uidRanges, const std::set<uid_t>& protectableUsers);
42 [[nodiscard]] int removeUsers(const UidRanges& uidRanges,
43 const std::set<uid_t>& protectableUsers);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070044
Bernie Innocenti762dcf42019-06-14 19:52:49 +090045 private:
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070046 Type getType() const override;
Bernie Innocenti762dcf42019-06-14 19:52:49 +090047 [[nodiscard]] int addInterface(const std::string& interface) override;
48 [[nodiscard]] int removeInterface(const std::string& interface) override;
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090049 int maybeCloseSockets(bool add, const UidRanges& uidRanges,
50 const std::set<uid_t>& protectableUsers);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070051
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070052 const bool mSecure;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070053 UidRanges mUidRanges;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070054};
55
Bernie Innocenti762dcf42019-06-14 19:52:49 +090056} // namespace android::net