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 <string> 13 #include <string_view> 14 #include <unordered_set> 15 #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> 16 17 namespace phosphor 18 { 19 namespace network 20 { 21 22 using EthernetInterfaceIntf = 23 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; 24 25 constexpr auto IPV4_MIN_PREFIX_LENGTH = 1; 26 constexpr auto IPV4_MAX_PREFIX_LENGTH = 32; 27 constexpr auto IPV6_MAX_PREFIX_LENGTH = 128; 28 29 namespace mac_address 30 { 31 32 /** @brief gets the MAC address from the Inventory. 33 * @param[in] bus - DBUS Bus Object. 34 * @param[in] intfName - Interface name 35 */ 36 ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName); 37 38 /** @brief Converts the given mac address into byte form 39 * @param[in] str - The mac address in human readable form 40 * @returns A mac address in network byte order 41 * @throws std::runtime_error for bad mac 42 */ 43 ether_addr fromString(const char* str); 44 inline ether_addr fromString(const std::string& str) 45 { 46 return fromString(str.c_str()); 47 } 48 49 /** @brief Converts the given mac address bytes into a string 50 * @param[in] mac - The mac address 51 * @returns A valid mac address string 52 */ 53 std::string toString(const ether_addr& mac); 54 55 /** @brief Determines if the mac address is empty 56 * @param[in] mac - The mac address 57 * @return True if 00:00:00:00:00:00 58 */ 59 bool isEmpty(const ether_addr& mac); 60 61 /** @brief Determines if the mac address is a multicast address 62 * @param[in] mac - The mac address 63 * @return True if multicast bit is set 64 */ 65 bool isMulticast(const ether_addr& mac); 66 67 /** @brief Determines if the mac address is a unicast address 68 * @param[in] mac - The mac address 69 * @return True if not multicast or empty 70 */ 71 bool isUnicast(const ether_addr& mac); 72 73 } // namespace mac_address 74 75 constexpr auto networkdService = "systemd-networkd.service"; 76 constexpr auto timeSynchdService = "systemd-timesyncd.service"; 77 78 /* @brief converts a sockaddr for the specified address family into 79 * a type_safe InAddrAny. 80 * @param[in] addressFamily - The address family of the buf 81 * @param[in] buf - The network byte order address 82 */ 83 InAddrAny addrFromBuf(int addressFamily, std::string_view buf); 84 85 /* @brief converts the ip bytes into a string representation 86 * @param[in] addr - input ip address to convert. 87 * @returns String representation of the ip. 88 */ 89 std::string toString(const InAddrAny& addr); 90 std::string toString(const struct in_addr& addr); 91 std::string toString(const struct in6_addr& addr); 92 93 /* @brief checks that the given ip address valid or not. 94 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 95 * @param[in] address - IP address. 96 * @returns true if it is valid otherwise false. 97 */ 98 bool isValidIP(int addressFamily, const std::string& address); 99 100 /* @brief checks that the given prefix is valid or not. 101 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). 102 * @param[in] prefix - prefix length. 103 * @returns true if it is valid otherwise false. 104 */ 105 bool isValidPrefix(int addressFamily, uint8_t prefixLength); 106 107 /** @brief Get all the interfaces from the system. 108 * @returns list of interface names. 109 */ 110 InterfaceList getInterfaces(); 111 112 /** @brief Delete the given interface. 113 * @param[in] intf - interface name. 114 */ 115 void deleteInterface(const std::string& 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(const char* 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(const char* 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(const char* 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