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 Converts the given mac address bytes into a string 47 * @param[in] mac - The mac address 48 * @returns A valid mac address string 49 */ 50 std::string toString(const ether_addr& mac); 51 52 /** @brief Determines if the mac address is empty 53 * @param[in] mac - The mac address 54 * @return True if 00:00:00:00:00:00 55 */ 56 bool isEmpty(const ether_addr& mac); 57 58 /** @brief Determines if the mac address is a multicast address 59 * @param[in] mac - The mac address 60 * @return True if multicast bit is set 61 */ 62 bool isMulticast(const ether_addr& mac); 63 64 /** @brief Determines if the mac address is a unicast address 65 * @param[in] mac - The mac address 66 * @return True if not multicast or empty 67 */ 68 bool isUnicast(const ether_addr& mac); 69 70 } // namespace mac_address 71 72 constexpr auto networkdService = "systemd-networkd.service"; 73 constexpr auto timeSynchdService = "systemd-timesyncd.service"; 74 75 template <int family> 76 struct FamilyTraits 77 { 78 }; 79 80 template <> 81 struct FamilyTraits<AF_INET> 82 { 83 using addr = in_addr; 84 }; 85 86 template <> 87 struct FamilyTraits<AF_INET6> 88 { 89 using addr = in6_addr; 90 }; 91 92 /* @brief converts a sockaddr for the specified address family into 93 * a type_safe InAddrAny. 94 * @param[in] addressFamily - The address family of the buf 95 * @param[in] buf - The network byte order address 96 */ 97 InAddrAny addrFromBuf(int addressFamily, std::string_view buf); 98 99 /* @brief converts the ip bytes into a string representation 100 * @param[in] addr - input ip address to convert. 101 * @returns String representation of the ip. 102 */ 103 std::string toString(const InAddrAny& addr); 104 std::string toString(const struct in_addr& addr); 105 std::string toString(const struct in6_addr& addr); 106 107 /* @brief checks that the given ip address valid or not. 108 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 109 * @param[in] address - IP address. 110 * @returns true if it is valid otherwise false. 111 */ 112 bool isValidIP(int addressFamily, stdplus::const_zstring address); 113 114 /* @brief checks that the given prefix is valid or not. 115 * @param[in] family - IP address family(AF_INET/AF_INET6). 116 * @param[in] prefix - prefix length. 117 * @returns true if it is valid otherwise false. 118 */ 119 template <int family> 120 constexpr bool isValidPrefix(uint8_t prefix) noexcept 121 { 122 return prefix <= sizeof(typename FamilyTraits<family>::addr) * 8; 123 } 124 bool isValidPrefix(int family, uint8_t prefixLength); 125 126 /** @brief Get all the interfaces from the system. 127 * @returns list of interface names. 128 */ 129 InterfaceList getInterfaces(); 130 131 /** @brief Delete the given interface. 132 * @param[in] intf - interface name. 133 */ 134 void deleteInterface(stdplus::const_zstring intf); 135 136 /** @brief Converts the interface name into a u-boot environment 137 * variable that would hold its ethernet address. 138 * 139 * @param[in] intf - interface name 140 * @return The name of th environment key 141 */ 142 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf); 143 144 /** @brief read the IPv6AcceptRA value from the configuration file 145 * @param[in] config - The parsed configuration. 146 */ 147 bool getIPv6AcceptRA(const config::Parser& config); 148 149 /** @brief read the DHCP value from the configuration file 150 * @param[in] config - The parsed configuration. 151 */ 152 struct DHCPVal 153 { 154 bool v4, v6; 155 }; 156 DHCPVal getDHCPValue(const config::Parser& config); 157 158 /** @brief Read a boolean DHCP property from a conf file 159 * @param[in] config - The parsed configuration. 160 * @param[in] key - The property name. 161 */ 162 bool getDHCPProp(const config::Parser& config, std::string_view key); 163 164 namespace internal 165 { 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] args - arguments of the command. 170 */ 171 void executeCommandinChildProcess(stdplus::const_zstring path, char** args); 172 173 /** @brief Get ignored interfaces from environment */ 174 std::string_view getIgnoredInterfacesEnv(); 175 176 /** @brief Parse the comma separated interface names */ 177 std::unordered_set<std::string_view> 178 parseInterfaces(std::string_view interfaces); 179 180 /** @brief Get the ignored interfaces */ 181 const std::unordered_set<std::string_view>& getIgnoredInterfaces(); 182 183 } // namespace internal 184 185 /* @brief runs the given command in child process. 186 * @param[in] path -path of the binary file which needs to be execeuted. 187 * @param[in] tArgs - arguments of the command. 188 */ 189 template <typename... ArgTypes> 190 void execute(stdplus::const_zstring path, ArgTypes&&... tArgs) 191 { 192 using expandType = char*[]; 193 194 expandType args = {const_cast<char*>(tArgs)..., nullptr}; 195 196 internal::executeCommandinChildProcess(path, args); 197 } 198 199 } // namespace network 200 201 } // namespace phosphor 202