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