1 #include "config.h" 2 3 #include "util.hpp" 4 5 #include "config_parser.hpp" 6 #include "types.hpp" 7 8 #include <fmt/compile.h> 9 #include <fmt/format.h> 10 #include <sys/wait.h> 11 12 #include <cctype> 13 #include <phosphor-logging/elog-errors.hpp> 14 #include <phosphor-logging/log.hpp> 15 #include <string> 16 #include <string_view> 17 #include <variant> 18 #include <xyz/openbmc_project/Common/error.hpp> 19 20 namespace phosphor 21 { 22 namespace network 23 { 24 25 using std::literals::string_view_literals::operator""sv; 26 using namespace phosphor::logging; 27 using namespace sdbusplus::xyz::openbmc_project::Common::Error; 28 29 namespace internal 30 { 31 32 void executeCommandinChildProcess(stdplus::const_zstring path, char** args) 33 { 34 using namespace std::string_literals; 35 pid_t pid = fork(); 36 37 if (pid == 0) 38 { 39 execv(path.c_str(), args); 40 exit(255); 41 } 42 else if (pid < 0) 43 { 44 auto error = errno; 45 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); 46 elog<InternalFailure>(); 47 } 48 else if (pid > 0) 49 { 50 int status; 51 while (waitpid(pid, &status, 0) == -1) 52 { 53 if (errno != EINTR) 54 { 55 status = -1; 56 break; 57 } 58 } 59 60 if (status < 0) 61 { 62 fmt::memory_buffer buf; 63 fmt::format_to(fmt::appender(buf), "`{}`", path); 64 for (size_t i = 0; args[i] != nullptr; ++i) 65 { 66 fmt::format_to(fmt::appender(buf), " `{}`", args[i]); 67 } 68 buf.push_back('\0'); 69 log<level::ERR>("Unable to execute the command", 70 entry("CMD=%s", buf.data()), 71 entry("STATUS=%d", status)); 72 elog<InternalFailure>(); 73 } 74 } 75 } 76 77 /** @brief Get ignored interfaces from environment */ 78 std::string_view getIgnoredInterfacesEnv() 79 { 80 auto r = std::getenv("IGNORED_INTERFACES"); 81 if (r == nullptr) 82 { 83 return ""; 84 } 85 return r; 86 } 87 88 /** @brief Parse the comma separated interface names */ 89 std::unordered_set<std::string_view> 90 parseInterfaces(std::string_view interfaces) 91 { 92 std::unordered_set<std::string_view> result; 93 while (true) 94 { 95 auto sep = interfaces.find(','); 96 auto interface = interfaces.substr(0, sep); 97 while (!interface.empty() && std::isspace(interface.front())) 98 { 99 interface.remove_prefix(1); 100 } 101 while (!interface.empty() && std::isspace(interface.back())) 102 { 103 interface.remove_suffix(1); 104 } 105 if (!interface.empty()) 106 { 107 result.insert(interface); 108 } 109 if (sep == interfaces.npos) 110 { 111 break; 112 } 113 interfaces = interfaces.substr(sep + 1); 114 } 115 return result; 116 } 117 118 /** @brief Get the ignored interfaces */ 119 const std::unordered_set<std::string_view>& getIgnoredInterfaces() 120 { 121 static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv()); 122 return ignoredInterfaces; 123 } 124 125 } // namespace internal 126 127 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf) 128 { 129 constexpr auto pfx = "eth"sv; 130 if (!intf.starts_with(pfx)) 131 { 132 return std::nullopt; 133 } 134 intf.remove_prefix(pfx.size()); 135 unsigned idx; 136 try 137 { 138 idx = DecodeInt<unsigned, 10>{}(intf); 139 } 140 catch (...) 141 { 142 return std::nullopt; 143 } 144 if (idx == 0) 145 { 146 return "ethaddr"; 147 } 148 return fmt::format(FMT_COMPILE("eth{}addr"), idx); 149 } 150 151 static std::optional<DHCPVal> systemdParseDHCP(std::string_view str) 152 { 153 if (config::icaseeq(str, "ipv4")) 154 { 155 return DHCPVal{.v4 = true, .v6 = false}; 156 } 157 if (config::icaseeq(str, "ipv6")) 158 { 159 return DHCPVal{.v4 = false, .v6 = true}; 160 } 161 if (auto b = config::parseBool(str); b) 162 { 163 return DHCPVal{.v4 = *b, .v6 = *b}; 164 } 165 return std::nullopt; 166 } 167 168 inline auto systemdParseLast(const config::Parser& config, 169 std::string_view section, std::string_view key, 170 auto&& fun) 171 { 172 if (!config.getFileExists()) 173 { 174 } 175 else if (auto str = config.map.getLastValueString(section, key); 176 str == nullptr) 177 { 178 auto err = fmt::format("Unable to get the value of {}[{}] from {}", 179 section, key, config.getFilename().native()); 180 log<level::NOTICE>(err.c_str(), 181 entry("FILE=%s", config.getFilename().c_str())); 182 } 183 else if (auto val = fun(*str); !val) 184 { 185 auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section, 186 key, config.getFilename().native(), *str); 187 log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()), 188 entry("FILE=%s", config.getFilename().c_str())); 189 } 190 else 191 { 192 return val; 193 } 194 return decltype(fun(std::string_view{}))(std::nullopt); 195 } 196 197 bool getIPv6AcceptRA(const config::Parser& config) 198 { 199 #ifdef ENABLE_IPV6_ACCEPT_RA 200 constexpr bool def = true; 201 #else 202 constexpr bool def = false; 203 #endif 204 return systemdParseLast(config, "Network", "IPv6AcceptRA", 205 config::parseBool) 206 .value_or(def); 207 } 208 209 DHCPVal getDHCPValue(const config::Parser& config) 210 { 211 return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP) 212 .value_or(DHCPVal{.v4 = true, .v6 = true}); 213 } 214 215 bool getDHCPProp(const config::Parser& config, std::string_view key) 216 { 217 return systemdParseLast(config, "DHCP", key, config::parseBool) 218 .value_or(true); 219 } 220 221 namespace mac_address 222 { 223 224 bool isEmpty(const ether_addr& mac) 225 { 226 return mac == ether_addr{}; 227 } 228 229 bool isMulticast(const ether_addr& mac) 230 { 231 return mac.ether_addr_octet[0] & 0b1; 232 } 233 234 bool isUnicast(const ether_addr& mac) 235 { 236 return !isEmpty(mac) && !isMulticast(mac); 237 } 238 239 } // namespace mac_address 240 } // namespace network 241 } // namespace phosphor 242