1 #pragma once 2 #include "types.hpp" 3 4 #include <netinet/ether.h> 5 #include <unistd.h> 6 7 #include <cstring> 8 #include <filesystem> 9 #include <optional> 10 #include <sdbusplus/bus.hpp> 11 #include <stdplus/zstring.hpp> 12 #include <stdplus/zstring_view.hpp> 13 #include <string> 14 #include <string_view> 15 #include <unordered_set> 16 #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> 17 18 namespace phosphor 19 { 20 namespace network 21 { 22 namespace config 23 { 24 class Parser; 25 } 26 27 using EthernetInterfaceIntf = 28 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; 29 30 constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; 31 constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; 32 constexpr auto IPV6_MAX_PREFIX_LENGTH = 128; 33 34 namespace mac_address 35 { 36 37 /** @brief gets the MAC address from the Inventory. 38 * @param[in] bus - DBUS Bus Object. 39 * @param[in] intfName - Interface name 40 */ 41 ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName); 42 43 /** @brief Converts the given mac address into byte form 44 * @param[in] str - The mac address in human readable form 45 * @returns A mac address in network byte order 46 * @throws std::runtime_error for bad mac 47 */ 48 ether_addr fromString(stdplus::zstring_view str); 49 50 /** @brief Converts the given mac address bytes into a string 51 * @param[in] mac - The mac address 52 * @returns A valid mac address string 53 */ 54 std::string toString(const ether_addr& mac); 55 56 /** @brief Determines if the mac address is empty 57 * @param[in] mac - The mac address 58 * @return True if 00:00:00:00:00:00 59 */ 60 bool isEmpty(const ether_addr& mac); 61 62 /** @brief Determines if the mac address is a multicast address 63 * @param[in] mac - The mac address 64 * @return True if multicast bit is set 65 */ 66 bool isMulticast(const ether_addr& mac); 67 68 /** @brief Determines if the mac address is a unicast address 69 * @param[in] mac - The mac address 70 * @return True if not multicast or empty 71 */ 72 bool isUnicast(const ether_addr& mac); 73 74 } // namespace mac_address 75 76 constexpr auto networkdService = "systemd-networkd.service"; 77 constexpr auto timeSynchdService = "systemd-timesyncd.service"; 78 79 /* @brief converts a sockaddr for the specified address family into 80 * a type_safe InAddrAny. 81 * @param[in] addressFamily - The address family of the buf 82 * @param[in] buf - The network byte order address 83 */ 84 InAddrAny addrFromBuf(int addressFamily, std::string_view buf); 85 86 /* @brief converts the ip bytes into a string representation 87 * @param[in] addr - input ip address to convert. 88 * @returns String representation of the ip. 89 */ 90 std::string toString(const InAddrAny& addr); 91 std::string toString(const struct in_addr& addr); 92 std::string toString(const struct in6_addr& addr); 93 94 /* @brief checks that the given ip address valid or not. 95 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 96 * @param[in] address - IP address. 97 * @returns true if it is valid otherwise false. 98 */ 99 bool isValidIP(int addressFamily, stdplus::const_zstring address); 100 101 /* @brief checks that the given prefix is valid or not. 102 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 103 * @param[in] prefix - prefix length. 104 * @returns true if it is valid otherwise false. 105 */ 106 bool isValidPrefix(int addressFamily, uint8_t prefixLength); 107 108 /** @brief Get all the interfaces from the system. 109 * @returns list of interface names. 110 */ 111 InterfaceList getInterfaces(); 112 113 /** @brief Delete the given interface. 114 * @param[in] intf - interface name. 115 */ 116 void deleteInterface(stdplus::const_zstring intf); 117 118 /** @brief Converts the interface name into a u-boot environment 119 * variable that would hold its ethernet address. 120 * 121 * @param[in] intf - interface name 122 * @return The name of th environment key 123 */ 124 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf); 125 126 /** @brief read the IPv6AcceptRA value from the configuration file 127 * @param[in] config - The parsed configuration. 128 */ 129 bool getIPv6AcceptRA(const config::Parser& config); 130 131 /** @brief read the DHCP value from the configuration file 132 * @param[in] config - The parsed configuration. 133 */ 134 struct DHCPVal 135 { 136 bool v4, v6; 137 }; 138 DHCPVal getDHCPValue(const config::Parser& config); 139 140 /** @brief Read a boolean DHCP property from a conf file 141 * @param[in] config - The parsed configuration. 142 * @param[in] key - The property name. 143 */ 144 bool getDHCPProp(const config::Parser& config, std::string_view key); 145 146 namespace internal 147 { 148 149 /* @brief runs the given command in child process. 150 * @param[in] path - path of the binary file which needs to be execeuted. 151 * @param[in] args - arguments of the command. 152 */ 153 void executeCommandinChildProcess(stdplus::const_zstring path, char** args); 154 155 /** @brief Get ignored interfaces from environment */ 156 std::string_view getIgnoredInterfacesEnv(); 157 158 /** @brief Parse the comma separated interface names */ 159 std::unordered_set<std::string_view> 160 parseInterfaces(std::string_view interfaces); 161 162 /** @brief Get the ignored interfaces */ 163 const std::unordered_set<std::string_view>& getIgnoredInterfaces(); 164 165 } // namespace internal 166 167 /* @brief runs the given command in child process. 168 * @param[in] path -path of the binary file which needs to be execeuted. 169 * @param[in] tArgs - arguments of the command. 170 */ 171 template <typename... ArgTypes> 172 void execute(stdplus::const_zstring path, ArgTypes&&... tArgs) 173 { 174 using expandType = char*[]; 175 176 expandType args = {const_cast<char*>(tArgs)..., nullptr}; 177 178 internal::executeCommandinChildProcess(path, args); 179 } 180 181 } // namespace network 182 183 } // namespace phosphor 184