| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | #pragma once |
| 3 | |
| 4 | #include <string> |
| 5 | #include <net/if.h> |
| 6 | #include <vector> |
| 7 | #include <optional> |
| 8 | #include <linux/netlink.h> |
| 9 | #include "InterfaceConfiguration.h" |
| 10 | #include "NetlinkResponse.h" |
| 11 | #include "Operation.h" |
| 12 | |
| 13 | class Interface |
| 14 | { |
| 15 | public: |
| 16 | Interface(); |
| 17 | Interface(const Interface& source); |
| 18 | Interface(int index, const std::string& name); |
| 19 | |
| 20 | operator bool() const; |
| 21 | |
| 22 | InterfaceConfiguration Ipv4Configuration(); |
| 23 | InterfaceConfiguration Ipv6Configuration(); |
| 24 | |
| 25 | void CreateVirtualWifiAdapter(const std::string& wifiName); |
| 26 | void CreateProxyWifiAdapter(const std::string& wifiName); |
| 27 | static void CreateBondAdapter(const std::string& bondName); |
| 28 | static void CreateTunAdapter(const std::string& name); |
| 29 | static void CreateTapAdapter(const std::string& name); |
| 30 | void DeleteInterface(); |
| 31 | |
| 32 | void SetIpv4Configuration(const InterfaceConfiguration& configuration); |
| 33 | void SetIpv6Configuration(const InterfaceConfiguration& configuration); |
| 34 | |
| 35 | void ModifyIpAddress(const Address& address, Operation operation); |
| 36 | |
| 37 | void SetMacAddress(const MacAddress& address); |
| 38 | MacAddress GetMacAddress(); |
| 39 | |
| 40 | void SetName(const std::string& newName); |
| 41 | |
| 42 | void SetNamespace(int namespaceFd); |
| 43 | |
| 44 | void SetWiphyNamespace(int namespaceFd); |
| 45 | |
| 46 | void AddFlags(int flags); |
| 47 | |
| 48 | void RemoveFlags(int flags); |
| 49 | |
| 50 | void SetUp(); |
| 51 | |
| 52 | void SetDown(); |
| 53 | |
| 54 | int Index() const; |
| 55 | |
| 56 | void SetMtu(int mtu); |
| 57 | |
| 58 | void SetMetric(int metric); |
| 59 | |
| 60 | void RemoveFromBond(const Interface& child_interface); |
| 61 | |
| 62 | void AddToBond(const Interface& child_interface); |
| 63 | |
| 64 | void SetActiveChild(const Interface& child_interface); |
| 65 | |
| 66 | void DisableNetworkSetting(const char* settingName, int addressFamily); |
| 67 | void EnableNetworkSetting(const char* settingName, int addressFamily); |
| 68 | |
| 69 | void ResetIpv6State(); |
| 70 | |
| 71 | const std::string& Name() const; |
| 72 | |
| 73 | static Interface Open(const std::string& name); |
| 74 | |
| 75 | void ModifyTcClassifier(bool Add); |
| 76 | void BpfAttachTcClassifier(int ProgramFd, bool Ingress); |
| 77 | |
| 78 | private: |
| 79 | template <size_t wifiTypeArraySize> |
| 80 | void CreateWifiAdapter(const std::string& wifiName, const char (&wifiType)[wifiTypeArraySize]); |
| 81 | |
| 82 | template <typename TAddr> |
| 83 | void ChangeAddress(const Address& address, const std::optional<Address>& broadcastAddress, Operation operation); |
| 84 | |
| 85 | template <typename TAddr, typename TMessage> |
| 86 | void ChangeAddressImpl(const Address& address, const std::optional<Address>& broadcastAddress, Operation operation); |
| 87 | |
| 88 | template <typename TAddr> |
| 89 | void SetConfiguration(const InterfaceConfiguration& current, const InterfaceConfiguration& configuration); |
| 90 | |
| 91 | static void CreateTunTapAdapter(const std::string& name, bool TunAdapter); |
| 92 | |
| 93 | InterfaceConfiguration ListAddressesImpl(int af); |
| 94 | |
| 95 | int m_index = -1; |
| 96 | std::string m_name; |
| 97 | }; |