11bbe3d1eSWilliam A. Kennington III #pragma once 21bbe3d1eSWilliam A. Kennington III 31bbe3d1eSWilliam A. Kennington III #include "config.h" 41bbe3d1eSWilliam A. Kennington III 51bbe3d1eSWilliam A. Kennington III #include "ethernet_interface.hpp" 61bbe3d1eSWilliam A. Kennington III #include "types.hpp" 71bbe3d1eSWilliam A. Kennington III 81bbe3d1eSWilliam A. Kennington III #include <netinet/ether.h> 91bbe3d1eSWilliam A. Kennington III #include <unistd.h> 101bbe3d1eSWilliam A. Kennington III 111bbe3d1eSWilliam A. Kennington III #include <cstring> 121bbe3d1eSWilliam A. Kennington III #include <optional> 131bbe3d1eSWilliam A. Kennington III #include <sdbusplus/bus.hpp> 141bbe3d1eSWilliam A. Kennington III #include <string> 151bbe3d1eSWilliam A. Kennington III #include <string_view> 161bbe3d1eSWilliam A. Kennington III #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> 171bbe3d1eSWilliam A. Kennington III 181bbe3d1eSWilliam A. Kennington III namespace phosphor 191bbe3d1eSWilliam A. Kennington III { 201bbe3d1eSWilliam A. Kennington III namespace network 211bbe3d1eSWilliam A. Kennington III { 221bbe3d1eSWilliam A. Kennington III 231bbe3d1eSWilliam A. Kennington III using EthernetInterfaceIntf = 241bbe3d1eSWilliam A. Kennington III sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; 251bbe3d1eSWilliam A. Kennington III 261bbe3d1eSWilliam A. Kennington III constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; 271bbe3d1eSWilliam A. Kennington III constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; 281bbe3d1eSWilliam A. Kennington III constexpr auto IPV6_MAX_PREFIX_LENGTH = 128; 291bbe3d1eSWilliam A. Kennington III constexpr auto IPV4_PREFIX = "169.254"; 301bbe3d1eSWilliam A. Kennington III constexpr auto IPV6_PREFIX = "fe80"; 311bbe3d1eSWilliam A. Kennington III 321bbe3d1eSWilliam A. Kennington III namespace mac_address 331bbe3d1eSWilliam A. Kennington III { 341bbe3d1eSWilliam A. Kennington III 351bbe3d1eSWilliam A. Kennington III /** @brief gets the MAC address from the Inventory. 361bbe3d1eSWilliam A. Kennington III * @param[in] bus - DBUS Bus Object. 371bbe3d1eSWilliam A. Kennington III * @param[in] intfName - Interface name 381bbe3d1eSWilliam A. Kennington III */ 39*c38b0710SPatrick Williams ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName); 401bbe3d1eSWilliam A. Kennington III 411bbe3d1eSWilliam A. Kennington III /** @brief Converts the given mac address into byte form 421bbe3d1eSWilliam A. Kennington III * @param[in] str - The mac address in human readable form 431bbe3d1eSWilliam A. Kennington III * @returns A mac address in network byte order 441bbe3d1eSWilliam A. Kennington III * @throws std::runtime_error for bad mac 451bbe3d1eSWilliam A. Kennington III */ 461c776022SWilliam A. Kennington III ether_addr fromString(const char* str); 471c776022SWilliam A. Kennington III inline ether_addr fromString(const std::string& str) 481c776022SWilliam A. Kennington III { 491c776022SWilliam A. Kennington III return fromString(str.c_str()); 501c776022SWilliam A. Kennington III } 511bbe3d1eSWilliam A. Kennington III 521bbe3d1eSWilliam A. Kennington III /** @brief Converts the given mac address bytes into a string 531bbe3d1eSWilliam A. Kennington III * @param[in] mac - The mac address 541bbe3d1eSWilliam A. Kennington III * @returns A valid mac address string 551bbe3d1eSWilliam A. Kennington III */ 561bbe3d1eSWilliam A. Kennington III std::string toString(const ether_addr& mac); 571bbe3d1eSWilliam A. Kennington III 581bbe3d1eSWilliam A. Kennington III /** @brief Determines if the mac address is empty 591bbe3d1eSWilliam A. Kennington III * @param[in] mac - The mac address 601bbe3d1eSWilliam A. Kennington III * @return True if 00:00:00:00:00:00 611bbe3d1eSWilliam A. Kennington III */ 621bbe3d1eSWilliam A. Kennington III bool isEmpty(const ether_addr& mac); 631bbe3d1eSWilliam A. Kennington III 641bbe3d1eSWilliam A. Kennington III /** @brief Determines if the mac address is a multicast address 651bbe3d1eSWilliam A. Kennington III * @param[in] mac - The mac address 661bbe3d1eSWilliam A. Kennington III * @return True if multicast bit is set 671bbe3d1eSWilliam A. Kennington III */ 681bbe3d1eSWilliam A. Kennington III bool isMulticast(const ether_addr& mac); 691bbe3d1eSWilliam A. Kennington III 701bbe3d1eSWilliam A. Kennington III /** @brief Determines if the mac address is a unicast address 711bbe3d1eSWilliam A. Kennington III * @param[in] mac - The mac address 721bbe3d1eSWilliam A. Kennington III * @return True if not multicast or empty 731bbe3d1eSWilliam A. Kennington III */ 741bbe3d1eSWilliam A. Kennington III bool isUnicast(const ether_addr& mac); 751bbe3d1eSWilliam A. Kennington III 761bbe3d1eSWilliam A. Kennington III } // namespace mac_address 771bbe3d1eSWilliam A. Kennington III 781bbe3d1eSWilliam A. Kennington III constexpr auto networkdService = "systemd-networkd.service"; 791bbe3d1eSWilliam A. Kennington III constexpr auto timeSynchdService = "systemd-timesyncd.service"; 801bbe3d1eSWilliam A. Kennington III 811bbe3d1eSWilliam A. Kennington III /* @brief converts the given subnet into prefix notation. 821bbe3d1eSWilliam A. Kennington III * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 831bbe3d1eSWilliam A. Kennington III * @param[in] mask - Subnet Mask. 841bbe3d1eSWilliam A. Kennington III * @returns prefix. 851bbe3d1eSWilliam A. Kennington III */ 861bbe3d1eSWilliam A. Kennington III uint8_t toCidr(int addressFamily, const std::string& mask); 871bbe3d1eSWilliam A. Kennington III 881bbe3d1eSWilliam A. Kennington III /* @brief converts a sockaddr for the specified address family into 891bbe3d1eSWilliam A. Kennington III * a type_safe InAddrAny. 901bbe3d1eSWilliam A. Kennington III * @param[in] addressFamily - The address family of the buf 911bbe3d1eSWilliam A. Kennington III * @param[in] buf - The network byte order address 921bbe3d1eSWilliam A. Kennington III */ 931bbe3d1eSWilliam A. Kennington III InAddrAny addrFromBuf(int addressFamily, std::string_view buf); 941bbe3d1eSWilliam A. Kennington III 951bbe3d1eSWilliam A. Kennington III /* @brief converts the ip bytes into a string representation 961bbe3d1eSWilliam A. Kennington III * @param[in] addr - input ip address to convert. 971bbe3d1eSWilliam A. Kennington III * @returns String representation of the ip. 981bbe3d1eSWilliam A. Kennington III */ 991bbe3d1eSWilliam A. Kennington III std::string toString(const InAddrAny& addr); 1001bbe3d1eSWilliam A. Kennington III std::string toString(const struct in_addr& addr); 1011bbe3d1eSWilliam A. Kennington III std::string toString(const struct in6_addr& addr); 1021bbe3d1eSWilliam A. Kennington III 1031bbe3d1eSWilliam A. Kennington III /* @brief converts the prefix into subnetmask. 1041bbe3d1eSWilliam A. Kennington III * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 1051bbe3d1eSWilliam A. Kennington III * @param[in] prefix - prefix length. 1061bbe3d1eSWilliam A. Kennington III * @returns subnet mask. 1071bbe3d1eSWilliam A. Kennington III */ 1081bbe3d1eSWilliam A. Kennington III std::string toMask(int addressFamily, uint8_t prefix); 1091bbe3d1eSWilliam A. Kennington III 1101bbe3d1eSWilliam A. Kennington III /* @brief checks that the given ip address is link local or not. 1111bbe3d1eSWilliam A. Kennington III * @param[in] address - IP address. 1121bbe3d1eSWilliam A. Kennington III * @returns true if it is linklocal otherwise false. 1131bbe3d1eSWilliam A. Kennington III */ 1141bbe3d1eSWilliam A. Kennington III bool isLinkLocalIP(const std::string& address); 1151bbe3d1eSWilliam A. Kennington III 1161bbe3d1eSWilliam A. Kennington III /* @brief checks that the given ip address valid or not. 1171bbe3d1eSWilliam A. Kennington III * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 1181bbe3d1eSWilliam A. Kennington III * @param[in] address - IP address. 1191bbe3d1eSWilliam A. Kennington III * @returns true if it is valid otherwise false. 1201bbe3d1eSWilliam A. Kennington III */ 1211bbe3d1eSWilliam A. Kennington III bool isValidIP(int addressFamily, const std::string& address); 1221bbe3d1eSWilliam A. Kennington III 1231bbe3d1eSWilliam A. Kennington III /* @brief checks that the given prefix is valid or not. 1241bbe3d1eSWilliam A. Kennington III * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 1251bbe3d1eSWilliam A. Kennington III * @param[in] prefix - prefix length. 1261bbe3d1eSWilliam A. Kennington III * @returns true if it is valid otherwise false. 1271bbe3d1eSWilliam A. Kennington III */ 1281bbe3d1eSWilliam A. Kennington III bool isValidPrefix(int addressFamily, uint8_t prefixLength); 1291bbe3d1eSWilliam A. Kennington III 1301bbe3d1eSWilliam A. Kennington III /** @brief Gets the map of interface and the associated 1311bbe3d1eSWilliam A. Kennington III * address. 1321bbe3d1eSWilliam A. Kennington III * @returns map of interface and the address. 1331bbe3d1eSWilliam A. Kennington III */ 1341bbe3d1eSWilliam A. Kennington III IntfAddrMap getInterfaceAddrs(); 1351bbe3d1eSWilliam A. Kennington III 1361bbe3d1eSWilliam A. Kennington III /** @brief Get all the interfaces from the system. 1371bbe3d1eSWilliam A. Kennington III * @returns list of interface names. 1381bbe3d1eSWilliam A. Kennington III */ 1391bbe3d1eSWilliam A. Kennington III InterfaceList getInterfaces(); 1401bbe3d1eSWilliam A. Kennington III 1411bbe3d1eSWilliam A. Kennington III /** @brief Delete the given interface. 1421bbe3d1eSWilliam A. Kennington III * @param[in] intf - interface name. 1431bbe3d1eSWilliam A. Kennington III */ 1441bbe3d1eSWilliam A. Kennington III void deleteInterface(const std::string& intf); 1451bbe3d1eSWilliam A. Kennington III 1461bbe3d1eSWilliam A. Kennington III /** @brief Converts the interface name into a u-boot environment 1471bbe3d1eSWilliam A. Kennington III * variable that would hold its ethernet address. 1481bbe3d1eSWilliam A. Kennington III * 1491bbe3d1eSWilliam A. Kennington III * @param[in] intf - interface name 1501bbe3d1eSWilliam A. Kennington III * @return The name of th environment key 1511bbe3d1eSWilliam A. Kennington III */ 1521bbe3d1eSWilliam A. Kennington III std::optional<std::string> interfaceToUbootEthAddr(const char* intf); 1531bbe3d1eSWilliam A. Kennington III 1541bbe3d1eSWilliam A. Kennington III /** @brief read the DHCP value from the configuration file 1551bbe3d1eSWilliam A. Kennington III * @param[in] confDir - Network configuration directory. 1561bbe3d1eSWilliam A. Kennington III * @param[in] intf - Interface name. 1571bbe3d1eSWilliam A. Kennington III */ 1581bbe3d1eSWilliam A. Kennington III EthernetInterfaceIntf::DHCPConf getDHCPValue(const std::string& confDir, 1591bbe3d1eSWilliam A. Kennington III const std::string& intf); 1601bbe3d1eSWilliam A. Kennington III 1611bbe3d1eSWilliam A. Kennington III namespace internal 1621bbe3d1eSWilliam A. Kennington III { 1631bbe3d1eSWilliam A. Kennington III 1641bbe3d1eSWilliam A. Kennington III /* @brief runs the given command in child process. 1651bbe3d1eSWilliam A. Kennington III * @param[in] path - path of the binary file which needs to be execeuted. 1661bbe3d1eSWilliam A. Kennington III * @param[in] args - arguments of the command. 1671bbe3d1eSWilliam A. Kennington III */ 1681bbe3d1eSWilliam A. Kennington III void executeCommandinChildProcess(const char* path, char** args); 1691bbe3d1eSWilliam A. Kennington III 1701bbe3d1eSWilliam A. Kennington III /** @brief Get ignored interfaces from environment */ 1711bbe3d1eSWilliam A. Kennington III std::string_view getIgnoredInterfacesEnv(); 1721bbe3d1eSWilliam A. Kennington III 1731bbe3d1eSWilliam A. Kennington III /** @brief Parse the comma separated interface names */ 1741bbe3d1eSWilliam A. Kennington III std::set<std::string_view> parseInterfaces(std::string_view interfaces); 1751bbe3d1eSWilliam A. Kennington III 1761bbe3d1eSWilliam A. Kennington III /** @brief Get the ignored interfaces */ 1771bbe3d1eSWilliam A. Kennington III const std::set<std::string_view>& getIgnoredInterfaces(); 1781bbe3d1eSWilliam A. Kennington III 1791bbe3d1eSWilliam A. Kennington III } // namespace internal 1801bbe3d1eSWilliam A. Kennington III 1811bbe3d1eSWilliam A. Kennington III /* @brief runs the given command in child process. 1821bbe3d1eSWilliam A. Kennington III * @param[in] path -path of the binary file which needs to be execeuted. 1831bbe3d1eSWilliam A. Kennington III * @param[in] tArgs - arguments of the command. 1841bbe3d1eSWilliam A. Kennington III */ 1851bbe3d1eSWilliam A. Kennington III template <typename... ArgTypes> 1861bbe3d1eSWilliam A. Kennington III void execute(const char* path, ArgTypes&&... tArgs) 1871bbe3d1eSWilliam A. Kennington III { 1881bbe3d1eSWilliam A. Kennington III using expandType = char*[]; 1891bbe3d1eSWilliam A. Kennington III 1901bbe3d1eSWilliam A. Kennington III expandType args = {const_cast<char*>(tArgs)..., nullptr}; 1911bbe3d1eSWilliam A. Kennington III 1921bbe3d1eSWilliam A. Kennington III internal::executeCommandinChildProcess(path, args); 1931bbe3d1eSWilliam A. Kennington III } 1941bbe3d1eSWilliam A. Kennington III 1951bbe3d1eSWilliam A. Kennington III } // namespace network 1961bbe3d1eSWilliam A. Kennington III 1971bbe3d1eSWilliam A. Kennington III class Descriptor 1981bbe3d1eSWilliam A. Kennington III { 1991bbe3d1eSWilliam A. Kennington III private: 2001bbe3d1eSWilliam A. Kennington III /** default value */ 2011bbe3d1eSWilliam A. Kennington III int fd = -1; 2021bbe3d1eSWilliam A. Kennington III 2031bbe3d1eSWilliam A. Kennington III public: 2041bbe3d1eSWilliam A. Kennington III Descriptor() = default; 2051bbe3d1eSWilliam A. Kennington III Descriptor(const Descriptor&) = delete; 2061bbe3d1eSWilliam A. Kennington III Descriptor& operator=(const Descriptor&) = delete; 2071bbe3d1eSWilliam A. Kennington III Descriptor(Descriptor&&) = delete; 2081bbe3d1eSWilliam A. Kennington III Descriptor& operator=(Descriptor&&) = delete; 2091bbe3d1eSWilliam A. Kennington III 2101bbe3d1eSWilliam A. Kennington III explicit Descriptor(int fd) : fd(fd) 2111bbe3d1eSWilliam A. Kennington III { 2121bbe3d1eSWilliam A. Kennington III } 2131bbe3d1eSWilliam A. Kennington III 2141bbe3d1eSWilliam A. Kennington III /* @brief sets the internal file descriptor with the given descriptor 2151bbe3d1eSWilliam A. Kennington III * and closes the old descriptor. 2161bbe3d1eSWilliam A. Kennington III * @param[in] descriptor - File/Socket descriptor. 2171bbe3d1eSWilliam A. Kennington III */ 2181bbe3d1eSWilliam A. Kennington III void set(int descriptor) 2191bbe3d1eSWilliam A. Kennington III { 2201bbe3d1eSWilliam A. Kennington III // same descriptor given 2211bbe3d1eSWilliam A. Kennington III if (fd == descriptor) 2221bbe3d1eSWilliam A. Kennington III { 2231bbe3d1eSWilliam A. Kennington III return; 2241bbe3d1eSWilliam A. Kennington III } 2251bbe3d1eSWilliam A. Kennington III 2261bbe3d1eSWilliam A. Kennington III // close the old descriptor 2271bbe3d1eSWilliam A. Kennington III if (fd >= 0) 2281bbe3d1eSWilliam A. Kennington III { 2291bbe3d1eSWilliam A. Kennington III close(fd); 2301bbe3d1eSWilliam A. Kennington III } 2311bbe3d1eSWilliam A. Kennington III 2321bbe3d1eSWilliam A. Kennington III fd = descriptor; 2331bbe3d1eSWilliam A. Kennington III } 2341bbe3d1eSWilliam A. Kennington III 2351bbe3d1eSWilliam A. Kennington III ~Descriptor() 2361bbe3d1eSWilliam A. Kennington III { 2371bbe3d1eSWilliam A. Kennington III if (fd >= 0) 2381bbe3d1eSWilliam A. Kennington III { 2391bbe3d1eSWilliam A. Kennington III close(fd); 2401bbe3d1eSWilliam A. Kennington III } 2411bbe3d1eSWilliam A. Kennington III } 2421bbe3d1eSWilliam A. Kennington III 2431bbe3d1eSWilliam A. Kennington III int operator()() const 2441bbe3d1eSWilliam A. Kennington III { 2451bbe3d1eSWilliam A. Kennington III return fd; 2461bbe3d1eSWilliam A. Kennington III } 2471bbe3d1eSWilliam A. Kennington III }; 2481bbe3d1eSWilliam A. Kennington III 2491bbe3d1eSWilliam A. Kennington III } // namespace phosphor 250