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