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