William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 3 | #include "util.hpp" |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 4 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 5 | #include "config_parser.hpp" |
| 6 | #include "types.hpp" |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 7 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 8 | #include <sys/wait.h> |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 9 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Jagpal Singh Gill | f78a415 | 2023-04-17 21:17:56 -0700 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 9b2a20d | 2023-06-17 14:05:48 -0700 | [diff] [blame] | 12 | #include <stdplus/numeric/str.hpp> |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame^] | 13 | #include <stdplus/str/buf.hpp> |
| 14 | #include <stdplus/str/cat.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Common/error.hpp> |
| 16 | |
| 17 | #include <cctype> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 18 | #include <string> |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 19 | #include <string_view> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 20 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace network |
| 24 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 25 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 26 | using std::literals::string_view_literals::operator""sv; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 27 | using namespace phosphor::logging; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 29 | |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 30 | namespace internal |
| 31 | { |
| 32 | |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame^] | 33 | void executeCommandinChildProcess(stdplus::zstring_view path, char** args) |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 34 | { |
| 35 | using namespace std::string_literals; |
| 36 | pid_t pid = fork(); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 37 | |
| 38 | if (pid == 0) |
| 39 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 40 | execv(path.c_str(), args); |
| 41 | exit(255); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 42 | } |
| 43 | else if (pid < 0) |
| 44 | { |
| 45 | auto error = errno; |
Jagpal Singh Gill | f78a415 | 2023-04-17 21:17:56 -0700 | [diff] [blame] | 46 | lg2::error("Error occurred during fork: {ERRNO}", "ERRNO", error); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 47 | elog<InternalFailure>(); |
| 48 | } |
| 49 | else if (pid > 0) |
| 50 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 51 | int status; |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 52 | while (waitpid(pid, &status, 0) == -1) |
| 53 | { |
| 54 | if (errno != EINTR) |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 55 | { |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 56 | status = -1; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (status < 0) |
| 62 | { |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame^] | 63 | stdplus::StrBuf buf; |
| 64 | stdplus::strAppend(buf, "`"sv, path, "`"sv); |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 65 | for (size_t i = 0; args[i] != nullptr; ++i) |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 66 | { |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame^] | 67 | stdplus::strAppend(buf, " `"sv, args[i], "`"sv); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 68 | } |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 69 | buf.push_back('\0'); |
Jagpal Singh Gill | f78a415 | 2023-04-17 21:17:56 -0700 | [diff] [blame] | 70 | lg2::error("Unable to execute the command {CMD}: {STATUS}", "CMD", |
| 71 | buf.data(), "STATUS", status); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 72 | elog<InternalFailure>(); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 77 | /** @brief Get ignored interfaces from environment */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 78 | std::string_view getIgnoredInterfacesEnv() |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 79 | { |
| 80 | auto r = std::getenv("IGNORED_INTERFACES"); |
| 81 | if (r == nullptr) |
| 82 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 83 | return ""; |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 84 | } |
| 85 | return r; |
| 86 | } |
| 87 | |
| 88 | /** @brief Parse the comma separated interface names */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 89 | std::unordered_set<std::string_view> |
| 90 | parseInterfaces(std::string_view interfaces) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 91 | { |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 92 | std::unordered_set<std::string_view> result; |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 93 | while (true) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 94 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 95 | auto sep = interfaces.find(','); |
| 96 | auto interface = interfaces.substr(0, sep); |
| 97 | while (!interface.empty() && std::isspace(interface.front())) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 98 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 99 | interface.remove_prefix(1); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 100 | } |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 101 | while (!interface.empty() && std::isspace(interface.back())) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 102 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 103 | interface.remove_suffix(1); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 104 | } |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 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); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 114 | } |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | /** @brief Get the ignored interfaces */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 119 | const std::unordered_set<std::string_view>& getIgnoredInterfaces() |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 120 | { |
| 121 | static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv()); |
| 122 | return ignoredInterfaces; |
| 123 | } |
| 124 | |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 125 | } // namespace internal |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 126 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 127 | std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 128 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 129 | constexpr auto pfx = "eth"sv; |
| 130 | if (!intf.starts_with(pfx)) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 131 | { |
| 132 | return std::nullopt; |
| 133 | } |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 134 | intf.remove_prefix(pfx.size()); |
William A. Kennington III | d6bd8fb | 2022-11-17 18:10:06 -0800 | [diff] [blame] | 135 | unsigned idx; |
| 136 | try |
| 137 | { |
William A. Kennington III | 9b2a20d | 2023-06-17 14:05:48 -0700 | [diff] [blame] | 138 | idx = stdplus::StrToInt<10, unsigned>{}(intf); |
William A. Kennington III | d6bd8fb | 2022-11-17 18:10:06 -0800 | [diff] [blame] | 139 | } |
| 140 | catch (...) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 141 | { |
| 142 | return std::nullopt; |
| 143 | } |
| 144 | if (idx == 0) |
| 145 | { |
| 146 | return "ethaddr"; |
| 147 | } |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame^] | 148 | stdplus::ToStrHandle<stdplus::IntToStr<10, unsigned>> tsh; |
| 149 | return stdplus::strCat("eth"sv, tsh(idx), "addr"sv); |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 150 | } |
| 151 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 152 | static std::optional<DHCPVal> systemdParseDHCP(std::string_view str) |
| 153 | { |
| 154 | if (config::icaseeq(str, "ipv4")) |
| 155 | { |
| 156 | return DHCPVal{.v4 = true, .v6 = false}; |
| 157 | } |
| 158 | if (config::icaseeq(str, "ipv6")) |
| 159 | { |
| 160 | return DHCPVal{.v4 = false, .v6 = true}; |
| 161 | } |
| 162 | if (auto b = config::parseBool(str); b) |
| 163 | { |
| 164 | return DHCPVal{.v4 = *b, .v6 = *b}; |
| 165 | } |
| 166 | return std::nullopt; |
| 167 | } |
| 168 | |
| 169 | inline auto systemdParseLast(const config::Parser& config, |
| 170 | std::string_view section, std::string_view key, |
| 171 | auto&& fun) |
| 172 | { |
William A. Kennington III | 4a688fc | 2022-11-15 15:58:44 -0800 | [diff] [blame] | 173 | if (!config.getFileExists()) |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 174 | {} |
William A. Kennington III | 4a688fc | 2022-11-15 15:58:44 -0800 | [diff] [blame] | 175 | else if (auto str = config.map.getLastValueString(section, key); |
| 176 | str == nullptr) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 177 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 178 | lg2::notice( |
| 179 | "Unable to get the value of {CFG_SEC}[{CFG_KEY}] from {CFG_FILE}", |
| 180 | "CFG_SEC", section, "CFG_KEY", key, "CFG_FILE", |
| 181 | config.getFilename()); |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 182 | } |
| 183 | else if (auto val = fun(*str); !val) |
| 184 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 185 | lg2::notice( |
| 186 | "Invalid value of {CFG_SEC}[{CFG_KEY}] from {CFG_FILE}: {CFG_VAL}", |
| 187 | "CFG_SEC", section, "CFG_KEY", key, "CFG_FILE", |
| 188 | config.getFilename(), "CFG_VAL", *str); |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 189 | } |
| 190 | else |
| 191 | { |
| 192 | return val; |
| 193 | } |
| 194 | return decltype(fun(std::string_view{}))(std::nullopt); |
| 195 | } |
| 196 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 197 | bool getIPv6AcceptRA(const config::Parser& config) |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 198 | { |
William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 199 | #ifdef ENABLE_IPV6_ACCEPT_RA |
| 200 | constexpr bool def = true; |
| 201 | #else |
| 202 | constexpr bool def = false; |
| 203 | #endif |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 204 | return systemdParseLast(config, "Network", "IPv6AcceptRA", |
| 205 | config::parseBool) |
| 206 | .value_or(def); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 207 | } |
| 208 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 209 | DHCPVal getDHCPValue(const config::Parser& config) |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 210 | { |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 211 | return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP) |
| 212 | .value_or(DHCPVal{.v4 = true, .v6 = true}); |
| 213 | } |
William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 214 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 215 | bool getDHCPProp(const config::Parser& config, std::string_view key) |
| 216 | { |
| 217 | return systemdParseLast(config, "DHCP", key, config::parseBool) |
| 218 | .value_or(true); |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 219 | } |
| 220 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 221 | } // namespace network |
| 222 | } // namespace phosphor |