Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_DEVICE_H_ |
| 6 | #define PATCHPANEL_DEVICE_H_ |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 7 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 8 | #include <linux/in6.h> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 9 | #include <netinet/in.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <unistd.h> |
| 12 | |
Taoyu Li | ce7caa6 | 2019-10-01 15:43:33 +0900 | [diff] [blame] | 13 | #include <map> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <string> |
| 16 | |
Garrick Evans | 428e476 | 2018-12-11 15:18:42 +0900 | [diff] [blame] | 17 | #include <base/bind.h> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 18 | #include <base/memory/weak_ptr.h> |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 19 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 20 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 21 | #include "patchpanel/ipc.pb.h" |
| 22 | #include "patchpanel/mac_address_generator.h" |
| 23 | #include "patchpanel/subnet.h" |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 24 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 25 | namespace patchpanel { |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 26 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 27 | // Encapsulates a physical (e.g. eth0) or proxy (e.g. arc) network device and |
| 28 | // its configuration spec (interfaces, addresses) on the host and in the |
| 29 | // container. It manages additional services such as router detection, address |
| 30 | // assignment, and MDNS and SSDP forwarding. This class is the authoritative |
| 31 | // source for configuration events. |
| 32 | class Device { |
| 33 | public: |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 34 | class Config { |
| 35 | public: |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 36 | Config(const MacAddress& mac_addr, |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 37 | std::unique_ptr<Subnet> ipv4_subnet, |
| 38 | std::unique_ptr<SubnetAddress> host_ipv4_addr, |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 39 | std::unique_ptr<SubnetAddress> guest_ipv4_addr, |
| 40 | std::unique_ptr<Subnet> lxd_ipv4_subnet = nullptr); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 41 | Config(const Config&) = delete; |
| 42 | Config& operator=(const Config&) = delete; |
| 43 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 44 | ~Config() = default; |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 45 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 46 | MacAddress mac_addr() const { return mac_addr_; } |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 47 | uint32_t host_ipv4_addr() const { return host_ipv4_addr_->Address(); } |
| 48 | uint32_t guest_ipv4_addr() const { return guest_ipv4_addr_->Address(); } |
| 49 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 50 | const SubnetAddress* const host_ipv4_subnet_addr() const { |
| 51 | return host_ipv4_addr_.get(); |
| 52 | } |
| 53 | const SubnetAddress* const guest_ipv4_subnet_addr() const { |
| 54 | return guest_ipv4_addr_.get(); |
| 55 | } |
| 56 | |
| 57 | const Subnet* const ipv4_subnet() const { return ipv4_subnet_.get(); } |
| 58 | |
| 59 | const Subnet* const lxd_ipv4_subnet() const { |
| 60 | return lxd_ipv4_subnet_.get(); |
| 61 | } |
| 62 | |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 63 | void set_tap_ifname(const std::string& tap); |
| 64 | const std::string& tap_ifname() const; |
| 65 | |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 66 | friend std::ostream& operator<<(std::ostream& stream, const Device& device); |
| 67 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 68 | private: |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 69 | // A random MAC address assigned to the device. |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 70 | MacAddress mac_addr_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 71 | // The IPV4 subnet allocated for this device. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 72 | std::unique_ptr<Subnet> ipv4_subnet_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 73 | // The address allocated from |ipv4_subnet| for use by the CrOS-side |
| 74 | // interface associated with this device. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 75 | std::unique_ptr<SubnetAddress> host_ipv4_addr_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 76 | // The address allocated from |ipv4_subnet| for use by the guest-side |
| 77 | // interface associated with this device, if applicable. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 78 | std::unique_ptr<SubnetAddress> guest_ipv4_addr_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 79 | // If applicable, an additional subnet allocated for this device for guests |
| 80 | // like Crostini to use for assigning addresses to containers running within |
| 81 | // the VM. |
| 82 | std::unique_ptr<Subnet> lxd_ipv4_subnet_; |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 83 | // TAP devices currently associated with the configuration. |
| 84 | std::string tap_; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 85 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | struct Options { |
| 89 | bool fwd_multicast; |
Taoyu Li | ce7caa6 | 2019-10-01 15:43:33 +0900 | [diff] [blame] | 90 | bool ipv6_enabled; |
Jason Jeremy Iman | 3081d0e | 2020-03-04 15:52:06 +0900 | [diff] [blame] | 91 | bool adb_allowed; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 92 | }; |
| 93 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 94 | // |phys_ifname| corresponds either to the physical interface provided by |
| 95 | // shill or a placeholder for a guest-specific control interface (e.g. arc0). |
| 96 | // |host_ifname| identifies the name of the virtual (bridge) interface. |
| 97 | // |guest_ifname|, if specified, identifies the name of the interface used |
| 98 | // inside the guest. |
| 99 | Device(const std::string& phys_ifname, |
| 100 | const std::string& host_ifname, |
| 101 | const std::string& guest_ifname, |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 102 | std::unique_ptr<Config> config, |
Garrick Evans | bcce09e | 2020-03-10 15:08:04 +0900 | [diff] [blame] | 103 | const Options& options); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 104 | Device(const Device&) = delete; |
| 105 | Device& operator=(const Device&) = delete; |
| 106 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 107 | ~Device() = default; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 108 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 109 | const std::string& phys_ifname() const { return phys_ifname_; } |
| 110 | const std::string& host_ifname() const { return host_ifname_; } |
| 111 | const std::string& guest_ifname() const { return guest_ifname_; } |
Garrick Evans | 894abc2 | 2019-06-07 10:49:02 +0900 | [diff] [blame] | 112 | Config& config() const; |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 113 | std::unique_ptr<Config> release_config(); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 114 | const Options& options() const; |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 115 | |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 116 | friend std::ostream& operator<<(std::ostream& stream, const Device& device); |
| 117 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 118 | private: |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 119 | std::string phys_ifname_; |
| 120 | std::string host_ifname_; |
| 121 | std::string guest_ifname_; |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 122 | std::unique_ptr<Config> config_; |
| 123 | const Options options_; |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 124 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 125 | FRIEND_TEST(DeviceTest, DisableLegacyAndroidDeviceSendsTwoMessages); |
Garrick Evans | bcce09e | 2020-03-10 15:08:04 +0900 | [diff] [blame] | 126 | |
| 127 | base::WeakPtrFactory<Device> weak_factory_{this}; |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 128 | }; |
| 129 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 130 | } // namespace patchpanel |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 131 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 132 | #endif // PATCHPANEL_DEVICE_H_ |