blob: e5a6e799c363dc2e21ddf965ac8976a875a33427 [file] [log] [blame]
Garrick Evans49879532018-12-03 13:15:36 +09001// 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
5#ifndef ARC_NETWORK_DEVICE_H_
6#define ARC_NETWORK_DEVICE_H_
7
Garrick Evans260ff302019-07-25 11:22:50 +09008#include <linux/in6.h>
Garrick Evans49879532018-12-03 13:15:36 +09009#include <netinet/in.h>
10#include <sys/socket.h>
11#include <unistd.h>
12
Taoyu Lice7caa62019-10-01 15:43:33 +090013#include <map>
Garrick Evans49879532018-12-03 13:15:36 +090014#include <memory>
15#include <string>
16
Garrick Evans428e4762018-12-11 15:18:42 +090017#include <base/bind.h>
Garrick Evans49879532018-12-03 13:15:36 +090018#include <base/memory/weak_ptr.h>
Garrick Evansf4a93292019-03-13 14:19:43 +090019#include <gtest/gtest_prod.h> // for FRIEND_TEST
Garrick Evans49879532018-12-03 13:15:36 +090020
21#include "arc/network/ipc.pb.h"
Garrick Evansf4a93292019-03-13 14:19:43 +090022#include "arc/network/mac_address_generator.h"
Garrick Evansf4a93292019-03-13 14:19:43 +090023#include "arc/network/subnet.h"
Garrick Evans49879532018-12-03 13:15:36 +090024
25namespace arc_networkd {
26
Garrick Evans49879532018-12-03 13:15:36 +090027// 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.
32class Device {
33 public:
Garrick Evansf4a93292019-03-13 14:19:43 +090034 class Config {
35 public:
Garrick Evans6c7dcb82020-03-16 15:21:05 +090036 Config(const MacAddress& mac_addr,
Garrick Evansf4a93292019-03-13 14:19:43 +090037 std::unique_ptr<Subnet> ipv4_subnet,
38 std::unique_ptr<SubnetAddress> host_ipv4_addr,
Garrick Evans47c19272019-11-21 10:58:21 +090039 std::unique_ptr<SubnetAddress> guest_ipv4_addr,
40 std::unique_ptr<Subnet> lxd_ipv4_subnet = nullptr);
Garrick Evansf4a93292019-03-13 14:19:43 +090041 ~Config() = default;
Garrick Evans49879532018-12-03 13:15:36 +090042
Garrick Evans6c7dcb82020-03-16 15:21:05 +090043 MacAddress mac_addr() const { return mac_addr_; }
Garrick Evansf4a93292019-03-13 14:19:43 +090044 uint32_t host_ipv4_addr() const { return host_ipv4_addr_->Address(); }
45 uint32_t guest_ipv4_addr() const { return guest_ipv4_addr_->Address(); }
46
Garrick Evans47c19272019-11-21 10:58:21 +090047 const SubnetAddress* const host_ipv4_subnet_addr() const {
48 return host_ipv4_addr_.get();
49 }
50 const SubnetAddress* const guest_ipv4_subnet_addr() const {
51 return guest_ipv4_addr_.get();
52 }
53
54 const Subnet* const ipv4_subnet() const { return ipv4_subnet_.get(); }
55
56 const Subnet* const lxd_ipv4_subnet() const {
57 return lxd_ipv4_subnet_.get();
58 }
59
Hugo Benichiee787ff2019-05-20 16:42:42 +090060 friend std::ostream& operator<<(std::ostream& stream, const Device& device);
61
Garrick Evansf4a93292019-03-13 14:19:43 +090062 private:
Garrick Evans47c19272019-11-21 10:58:21 +090063 // A random MAC address assigned to the device.
Garrick Evans6c7dcb82020-03-16 15:21:05 +090064 MacAddress mac_addr_;
Garrick Evans47c19272019-11-21 10:58:21 +090065 // The IPV4 subnet allocated for this device.
Garrick Evansf4a93292019-03-13 14:19:43 +090066 std::unique_ptr<Subnet> ipv4_subnet_;
Garrick Evans47c19272019-11-21 10:58:21 +090067 // The address allocated from |ipv4_subnet| for use by the CrOS-side
68 // interface associated with this device.
Garrick Evansf4a93292019-03-13 14:19:43 +090069 std::unique_ptr<SubnetAddress> host_ipv4_addr_;
Garrick Evans47c19272019-11-21 10:58:21 +090070 // The address allocated from |ipv4_subnet| for use by the guest-side
71 // interface associated with this device, if applicable.
Garrick Evansf4a93292019-03-13 14:19:43 +090072 std::unique_ptr<SubnetAddress> guest_ipv4_addr_;
Garrick Evans47c19272019-11-21 10:58:21 +090073 // If applicable, an additional subnet allocated for this device for guests
74 // like Crostini to use for assigning addresses to containers running within
75 // the VM.
76 std::unique_ptr<Subnet> lxd_ipv4_subnet_;
Garrick Evansf4a93292019-03-13 14:19:43 +090077
78 DISALLOW_COPY_AND_ASSIGN(Config);
79 };
80
81 struct Options {
82 bool fwd_multicast;
Taoyu Lice7caa62019-10-01 15:43:33 +090083 bool ipv6_enabled;
Garrick Evans8ff08452019-11-25 09:24:26 +090084
85 // Indicates this device must track shill's default interface.
86 // TODO(garrick): Further qualify if this interface is a physical interface
87 // or an ARC VPN to match the distinction shill is making; specifically, ARC
88 // N should not loop back into itself but for Termina this should flow over
89 // the VPN.
90 bool use_default_interface;
Garrick Evansf4a93292019-03-13 14:19:43 +090091 };
92
Garrick Evans6c7dcb82020-03-16 15:21:05 +090093 // |phys_ifname| corresponds either to the physical interface provided by
94 // shill or a placeholder for a guest-specific control interface (e.g. arc0).
95 // |host_ifname| identifies the name of the virtual (bridge) interface.
96 // |guest_ifname|, if specified, identifies the name of the interface used
97 // inside the guest.
98 Device(const std::string& phys_ifname,
99 const std::string& host_ifname,
100 const std::string& guest_ifname,
Garrick Evansf4a93292019-03-13 14:19:43 +0900101 std::unique_ptr<Config> config,
Garrick Evansbcce09e2020-03-10 15:08:04 +0900102 const Options& options);
Garrick Evans260ff302019-07-25 11:22:50 +0900103 ~Device() = default;
Garrick Evansf4a93292019-03-13 14:19:43 +0900104
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900105 const std::string& phys_ifname() const { return phys_ifname_; }
106 const std::string& host_ifname() const { return host_ifname_; }
107 const std::string& guest_ifname() const { return guest_ifname_; }
Garrick Evans894abc22019-06-07 10:49:02 +0900108 Config& config() const;
Garrick Evans54861622019-07-19 09:05:09 +0900109 const Options& options() const;
Garrick Evans49879532018-12-03 13:15:36 +0900110
Garrick Evansbcce09e2020-03-10 15:08:04 +0900111 void set_tap_ifname(const std::string& tap);
112 const std::string& tap_ifname() const;
Garrick Evans8ff08452019-11-25 09:24:26 +0900113
114 bool UsesDefaultInterface() const;
Garrick Evans54861622019-07-19 09:05:09 +0900115
Hugo Benichiee787ff2019-05-20 16:42:42 +0900116 friend std::ostream& operator<<(std::ostream& stream, const Device& device);
117
Garrick Evans49879532018-12-03 13:15:36 +0900118 private:
Garrick Evans6c7dcb82020-03-16 15:21:05 +0900119 std::string phys_ifname_;
120 std::string host_ifname_;
121 std::string guest_ifname_;
Garrick Evansf4a93292019-03-13 14:19:43 +0900122 std::unique_ptr<Config> config_;
123 const Options options_;
Garrick Evansbcce09e2020-03-10 15:08:04 +0900124 std::string tap_;
Garrick Evans49879532018-12-03 13:15:36 +0900125
Garrick Evansf4a93292019-03-13 14:19:43 +0900126 FRIEND_TEST(DeviceTest, DisableLegacyAndroidDeviceSendsTwoMessages);
Garrick Evansbcce09e2020-03-10 15:08:04 +0900127
128 base::WeakPtrFactory<Device> weak_factory_{this};
Garrick Evans49879532018-12-03 13:15:36 +0900129 DISALLOW_COPY_AND_ASSIGN(Device);
130};
131
132} // namespace arc_networkd
133
134#endif // ARC_NETWORK_DEVICE_H_