11bbe3d1eSWilliam A. Kennington III #pragma once 2*95530ec9SWilliam A. Kennington III #include "config_parser.hpp" 31bbe3d1eSWilliam A. Kennington III #include "types.hpp" 41bbe3d1eSWilliam A. Kennington III 51bbe3d1eSWilliam A. Kennington III #include <netinet/ether.h> 61bbe3d1eSWilliam A. Kennington III #include <unistd.h> 71bbe3d1eSWilliam A. Kennington III 81bbe3d1eSWilliam A. Kennington III #include <cstring> 9a520a39dSWilliam A. Kennington III #include <filesystem> 101bbe3d1eSWilliam A. Kennington III #include <optional> 111bbe3d1eSWilliam A. Kennington III #include <sdbusplus/bus.hpp> 121bbe3d1eSWilliam A. Kennington III #include <string> 131bbe3d1eSWilliam A. Kennington III #include <string_view> 14*95530ec9SWilliam A. Kennington III #include <unordered_set> 151bbe3d1eSWilliam A. Kennington III #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> 161bbe3d1eSWilliam A. Kennington III 171bbe3d1eSWilliam A. Kennington III namespace phosphor 181bbe3d1eSWilliam A. Kennington III { 191bbe3d1eSWilliam A. Kennington III namespace network 201bbe3d1eSWilliam A. Kennington III { 211bbe3d1eSWilliam A. Kennington III 221bbe3d1eSWilliam A. Kennington III using EthernetInterfaceIntf = 231bbe3d1eSWilliam A. Kennington III sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; 241bbe3d1eSWilliam A. Kennington III 251bbe3d1eSWilliam A. Kennington III constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; 261bbe3d1eSWilliam A. Kennington III constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; 271bbe3d1eSWilliam A. Kennington III constexpr auto IPV6_MAX_PREFIX_LENGTH = 128; 281bbe3d1eSWilliam A. Kennington III 291bbe3d1eSWilliam A. Kennington III namespace mac_address 301bbe3d1eSWilliam A. Kennington III { 311bbe3d1eSWilliam A. Kennington III 321bbe3d1eSWilliam A. Kennington III /** @brief gets the MAC address from the Inventory. 331bbe3d1eSWilliam A. Kennington III * @param[in] bus - DBUS Bus Object. 341bbe3d1eSWilliam A. Kennington III * @param[in] intfName - Interface name 351bbe3d1eSWilliam A. Kennington III */ 36c38b0710SPatrick Williams ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName); 371bbe3d1eSWilliam A. Kennington III 381bbe3d1eSWilliam A. Kennington III /** @brief Converts the given mac address into byte form 391bbe3d1eSWilliam A. Kennington III * @param[in] str - The mac address in human readable form 401bbe3d1eSWilliam A. Kennington III * @returns A mac address in network byte order 411bbe3d1eSWilliam A. Kennington III * @throws std::runtime_error for bad mac 421bbe3d1eSWilliam A. Kennington III */ 431c776022SWilliam A. Kennington III ether_addr fromString(const char* str); 441c776022SWilliam A. Kennington III inline ether_addr fromString(const std::string& str) 451c776022SWilliam A. Kennington III { 461c776022SWilliam A. Kennington III return fromString(str.c_str()); 471c776022SWilliam A. Kennington III } 481bbe3d1eSWilliam A. Kennington III 491bbe3d1eSWilliam A. Kennington III /** @brief Converts the given mac address bytes into a string 501bbe3d1eSWilliam A. Kennington III * @param[in] mac - The mac address 511bbe3d1eSWilliam A. Kennington III * @returns A valid mac address string 521bbe3d1eSWilliam A. Kennington III */ 531bbe3d1eSWilliam A. Kennington III std::string toString(const ether_addr& mac); 541bbe3d1eSWilliam A. Kennington III 551bbe3d1eSWilliam A. Kennington III /** @brief Determines if the mac address is empty 561bbe3d1eSWilliam A. Kennington III * @param[in] mac - The mac address 571bbe3d1eSWilliam A. Kennington III * @return True if 00:00:00:00:00:00 581bbe3d1eSWilliam A. Kennington III */ 591bbe3d1eSWilliam A. Kennington III bool isEmpty(const ether_addr& mac); 601bbe3d1eSWilliam A. Kennington III 611bbe3d1eSWilliam A. Kennington III /** @brief Determines if the mac address is a multicast address 621bbe3d1eSWilliam A. Kennington III * @param[in] mac - The mac address 631bbe3d1eSWilliam A. Kennington III * @return True if multicast bit is set 641bbe3d1eSWilliam A. Kennington III */ 651bbe3d1eSWilliam A. Kennington III bool isMulticast(const ether_addr& mac); 661bbe3d1eSWilliam A. Kennington III 671bbe3d1eSWilliam A. Kennington III /** @brief Determines if the mac address is a unicast address 681bbe3d1eSWilliam A. Kennington III * @param[in] mac - The mac address 691bbe3d1eSWilliam A. Kennington III * @return True if not multicast or empty 701bbe3d1eSWilliam A. Kennington III */ 711bbe3d1eSWilliam A. Kennington III bool isUnicast(const ether_addr& mac); 721bbe3d1eSWilliam A. Kennington III 731bbe3d1eSWilliam A. Kennington III } // namespace mac_address 741bbe3d1eSWilliam A. Kennington III 751bbe3d1eSWilliam A. Kennington III constexpr auto networkdService = "systemd-networkd.service"; 761bbe3d1eSWilliam A. Kennington III constexpr auto timeSynchdService = "systemd-timesyncd.service"; 771bbe3d1eSWilliam A. Kennington III 781bbe3d1eSWilliam A. Kennington III /* @brief converts a sockaddr for the specified address family into 791bbe3d1eSWilliam A. Kennington III * a type_safe InAddrAny. 801bbe3d1eSWilliam A. Kennington III * @param[in] addressFamily - The address family of the buf 811bbe3d1eSWilliam A. Kennington III * @param[in] buf - The network byte order address 821bbe3d1eSWilliam A. Kennington III */ 831bbe3d1eSWilliam A. Kennington III InAddrAny addrFromBuf(int addressFamily, std::string_view buf); 841bbe3d1eSWilliam A. Kennington III 851bbe3d1eSWilliam A. Kennington III /* @brief converts the ip bytes into a string representation 861bbe3d1eSWilliam A. Kennington III * @param[in] addr - input ip address to convert. 871bbe3d1eSWilliam A. Kennington III * @returns String representation of the ip. 881bbe3d1eSWilliam A. Kennington III */ 891bbe3d1eSWilliam A. Kennington III std::string toString(const InAddrAny& addr); 901bbe3d1eSWilliam A. Kennington III std::string toString(const struct in_addr& addr); 911bbe3d1eSWilliam A. Kennington III std::string toString(const struct in6_addr& addr); 921bbe3d1eSWilliam A. Kennington III 931bbe3d1eSWilliam A. Kennington III /* @brief checks that the given ip address valid or not. 941bbe3d1eSWilliam A. Kennington III * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 951bbe3d1eSWilliam A. Kennington III * @param[in] address - IP address. 961bbe3d1eSWilliam A. Kennington III * @returns true if it is valid otherwise false. 971bbe3d1eSWilliam A. Kennington III */ 981bbe3d1eSWilliam A. Kennington III bool isValidIP(int addressFamily, const std::string& address); 991bbe3d1eSWilliam A. Kennington III 1001bbe3d1eSWilliam A. Kennington III /* @brief checks that the given prefix is valid or not. 1011bbe3d1eSWilliam A. Kennington III * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 1021bbe3d1eSWilliam A. Kennington III * @param[in] prefix - prefix length. 1031bbe3d1eSWilliam A. Kennington III * @returns true if it is valid otherwise false. 1041bbe3d1eSWilliam A. Kennington III */ 1051bbe3d1eSWilliam A. Kennington III bool isValidPrefix(int addressFamily, uint8_t prefixLength); 1061bbe3d1eSWilliam A. Kennington III 1071bbe3d1eSWilliam A. Kennington III /** @brief Get all the interfaces from the system. 1081bbe3d1eSWilliam A. Kennington III * @returns list of interface names. 1091bbe3d1eSWilliam A. Kennington III */ 1101bbe3d1eSWilliam A. Kennington III InterfaceList getInterfaces(); 1111bbe3d1eSWilliam A. Kennington III 1121bbe3d1eSWilliam A. Kennington III /** @brief Delete the given interface. 1131bbe3d1eSWilliam A. Kennington III * @param[in] intf - interface name. 1141bbe3d1eSWilliam A. Kennington III */ 1151bbe3d1eSWilliam A. Kennington III void deleteInterface(const std::string& intf); 1161bbe3d1eSWilliam A. Kennington III 1171bbe3d1eSWilliam A. Kennington III /** @brief Converts the interface name into a u-boot environment 1181bbe3d1eSWilliam A. Kennington III * variable that would hold its ethernet address. 1191bbe3d1eSWilliam A. Kennington III * 1201bbe3d1eSWilliam A. Kennington III * @param[in] intf - interface name 1211bbe3d1eSWilliam A. Kennington III * @return The name of th environment key 1221bbe3d1eSWilliam A. Kennington III */ 1231bbe3d1eSWilliam A. Kennington III std::optional<std::string> interfaceToUbootEthAddr(const char* intf); 1241bbe3d1eSWilliam A. Kennington III 125a520a39dSWilliam A. Kennington III /** @brief read the IPv6AcceptRA value from the configuration file 126a520a39dSWilliam A. Kennington III * @param[in] config - The parsed configuration. 1271bbe3d1eSWilliam A. Kennington III */ 128a520a39dSWilliam A. Kennington III bool getIPv6AcceptRA(const config::Parser& config); 129a520a39dSWilliam A. Kennington III 130a520a39dSWilliam A. Kennington III /** @brief read the DHCP value from the configuration file 131a520a39dSWilliam A. Kennington III * @param[in] config - The parsed configuration. 132a520a39dSWilliam A. Kennington III */ 1338060c0daSWilliam A. Kennington III struct DHCPVal 1348060c0daSWilliam A. Kennington III { 1358060c0daSWilliam A. Kennington III bool v4, v6; 1368060c0daSWilliam A. Kennington III }; 1378060c0daSWilliam A. Kennington III DHCPVal getDHCPValue(const config::Parser& config); 1381bbe3d1eSWilliam A. Kennington III 139e94c9ffcSWilliam A. Kennington III /** @brief Read a boolean DHCP property from a conf file 140e94c9ffcSWilliam A. Kennington III * @param[in] config - The parsed configuration. 141e94c9ffcSWilliam A. Kennington III * @param[in] key - The property name. 142e94c9ffcSWilliam A. Kennington III */ 143e94c9ffcSWilliam A. Kennington III bool getDHCPProp(const config::Parser& config, std::string_view key); 144e94c9ffcSWilliam A. Kennington III 1451bbe3d1eSWilliam A. Kennington III namespace internal 1461bbe3d1eSWilliam A. Kennington III { 1471bbe3d1eSWilliam A. Kennington III 1481bbe3d1eSWilliam A. Kennington III /* @brief runs the given command in child process. 1491bbe3d1eSWilliam A. Kennington III * @param[in] path - path of the binary file which needs to be execeuted. 1501bbe3d1eSWilliam A. Kennington III * @param[in] args - arguments of the command. 1511bbe3d1eSWilliam A. Kennington III */ 1521bbe3d1eSWilliam A. Kennington III void executeCommandinChildProcess(const char* path, char** args); 1531bbe3d1eSWilliam A. Kennington III 1541bbe3d1eSWilliam A. Kennington III /** @brief Get ignored interfaces from environment */ 1551bbe3d1eSWilliam A. Kennington III std::string_view getIgnoredInterfacesEnv(); 1561bbe3d1eSWilliam A. Kennington III 1571bbe3d1eSWilliam A. Kennington III /** @brief Parse the comma separated interface names */ 158*95530ec9SWilliam A. Kennington III std::unordered_set<std::string_view> 159*95530ec9SWilliam A. Kennington III parseInterfaces(std::string_view interfaces); 1601bbe3d1eSWilliam A. Kennington III 1611bbe3d1eSWilliam A. Kennington III /** @brief Get the ignored interfaces */ 162*95530ec9SWilliam A. Kennington III const std::unordered_set<std::string_view>& getIgnoredInterfaces(); 1631bbe3d1eSWilliam A. Kennington III 1641bbe3d1eSWilliam A. Kennington III } // namespace internal 1651bbe3d1eSWilliam A. Kennington III 1661bbe3d1eSWilliam A. Kennington III /* @brief runs the given command in child process. 1671bbe3d1eSWilliam A. Kennington III * @param[in] path -path of the binary file which needs to be execeuted. 1681bbe3d1eSWilliam A. Kennington III * @param[in] tArgs - arguments of the command. 1691bbe3d1eSWilliam A. Kennington III */ 1701bbe3d1eSWilliam A. Kennington III template <typename... ArgTypes> 1711bbe3d1eSWilliam A. Kennington III void execute(const char* path, ArgTypes&&... tArgs) 1721bbe3d1eSWilliam A. Kennington III { 1731bbe3d1eSWilliam A. Kennington III using expandType = char*[]; 1741bbe3d1eSWilliam A. Kennington III 1751bbe3d1eSWilliam A. Kennington III expandType args = {const_cast<char*>(tArgs)..., nullptr}; 1761bbe3d1eSWilliam A. Kennington III 1771bbe3d1eSWilliam A. Kennington III internal::executeCommandinChildProcess(path, args); 1781bbe3d1eSWilliam A. Kennington III } 1791bbe3d1eSWilliam A. Kennington III 1801bbe3d1eSWilliam A. Kennington III } // namespace network 1811bbe3d1eSWilliam A. Kennington III 1821bbe3d1eSWilliam A. Kennington III } // namespace phosphor 183