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