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