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 | |
William A. Kennington III | 1c77602 | 2022-01-05 14:12:16 -0800 | [diff] [blame] | 8 | #include <fmt/compile.h> |
| 9 | #include <fmt/format.h> |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 10 | #include <sys/wait.h> |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 11 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 12 | #include <cctype> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 13 | #include <phosphor-logging/elog-errors.hpp> |
| 14 | #include <phosphor-logging/log.hpp> |
| 15 | #include <string> |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 16 | #include <string_view> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 17 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 18 | |
| 19 | namespace phosphor |
| 20 | { |
| 21 | namespace network |
| 22 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 23 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 24 | using std::literals::string_view_literals::operator""sv; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 25 | using namespace phosphor::logging; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 26 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 27 | |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 28 | namespace internal |
| 29 | { |
| 30 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 31 | void executeCommandinChildProcess(stdplus::const_zstring path, char** args) |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 32 | { |
| 33 | using namespace std::string_literals; |
| 34 | pid_t pid = fork(); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 35 | |
| 36 | if (pid == 0) |
| 37 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 38 | execv(path.c_str(), args); |
| 39 | exit(255); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 40 | } |
| 41 | else if (pid < 0) |
| 42 | { |
| 43 | auto error = errno; |
| 44 | log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); |
| 45 | elog<InternalFailure>(); |
| 46 | } |
| 47 | else if (pid > 0) |
| 48 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 49 | int status; |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 50 | while (waitpid(pid, &status, 0) == -1) |
| 51 | { |
| 52 | if (errno != EINTR) |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 53 | { |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 54 | status = -1; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (status < 0) |
| 60 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 61 | fmt::memory_buffer buf; |
| 62 | fmt::format_to(fmt::appender(buf), "`{}`", path); |
| 63 | for (size_t i = 0; args[i] != nullptr; ++i) |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 64 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 65 | fmt::format_to(fmt::appender(buf), " `{}`", args[i]); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 66 | } |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 67 | buf.push_back('\0'); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 68 | log<level::ERR>("Unable to execute the command", |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 69 | entry("CMD=%s", buf.data()), |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 70 | entry("STATUS=%d", status)); |
| 71 | elog<InternalFailure>(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 76 | /** @brief Get ignored interfaces from environment */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 77 | std::string_view getIgnoredInterfacesEnv() |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 78 | { |
| 79 | auto r = std::getenv("IGNORED_INTERFACES"); |
| 80 | if (r == nullptr) |
| 81 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 82 | return ""; |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 83 | } |
| 84 | return r; |
| 85 | } |
| 86 | |
| 87 | /** @brief Parse the comma separated interface names */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 88 | std::unordered_set<std::string_view> |
| 89 | parseInterfaces(std::string_view interfaces) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 90 | { |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 91 | std::unordered_set<std::string_view> result; |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 92 | while (true) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 93 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 94 | auto sep = interfaces.find(','); |
| 95 | auto interface = interfaces.substr(0, sep); |
| 96 | while (!interface.empty() && std::isspace(interface.front())) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 97 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 98 | interface.remove_prefix(1); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 99 | } |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 100 | while (!interface.empty() && std::isspace(interface.back())) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 101 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 102 | interface.remove_suffix(1); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 103 | } |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 104 | if (!interface.empty()) |
| 105 | { |
| 106 | result.insert(interface); |
| 107 | } |
| 108 | if (sep == interfaces.npos) |
| 109 | { |
| 110 | break; |
| 111 | } |
| 112 | interfaces = interfaces.substr(sep + 1); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | /** @brief Get the ignored interfaces */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 118 | const std::unordered_set<std::string_view>& getIgnoredInterfaces() |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 119 | { |
| 120 | static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv()); |
| 121 | return ignoredInterfaces; |
| 122 | } |
| 123 | |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 124 | } // namespace internal |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 125 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 126 | std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 127 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 128 | constexpr auto pfx = "eth"sv; |
| 129 | if (!intf.starts_with(pfx)) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 130 | { |
| 131 | return std::nullopt; |
| 132 | } |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 133 | intf.remove_prefix(pfx.size()); |
William A. Kennington III | d6bd8fb | 2022-11-17 18:10:06 -0800 | [diff] [blame] | 134 | unsigned idx; |
| 135 | try |
| 136 | { |
| 137 | idx = DecodeInt<unsigned, 10>{}(intf); |
| 138 | } |
| 139 | catch (...) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 140 | { |
| 141 | return std::nullopt; |
| 142 | } |
| 143 | if (idx == 0) |
| 144 | { |
| 145 | return "ethaddr"; |
| 146 | } |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 147 | return fmt::format(FMT_COMPILE("eth{}addr"), idx); |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 148 | } |
| 149 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 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 | { |
William A. Kennington III | 4a688fc | 2022-11-15 15:58:44 -0800 | [diff] [blame] | 171 | if (!config.getFileExists()) |
| 172 | { |
| 173 | } |
| 174 | else if (auto str = config.map.getLastValueString(section, key); |
| 175 | str == nullptr) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 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 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 196 | bool getIPv6AcceptRA(const config::Parser& config) |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 197 | { |
William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 198 | #ifdef ENABLE_IPV6_ACCEPT_RA |
| 199 | constexpr bool def = true; |
| 200 | #else |
| 201 | constexpr bool def = false; |
| 202 | #endif |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 203 | return systemdParseLast(config, "Network", "IPv6AcceptRA", |
| 204 | config::parseBool) |
| 205 | .value_or(def); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 206 | } |
| 207 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 208 | DHCPVal getDHCPValue(const config::Parser& config) |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 209 | { |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 210 | return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP) |
| 211 | .value_or(DHCPVal{.v4 = true, .v6 = true}); |
| 212 | } |
William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 213 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 214 | bool getDHCPProp(const config::Parser& config, std::string_view key) |
| 215 | { |
| 216 | return systemdParseLast(config, "DHCP", key, config::parseBool) |
| 217 | .value_or(true); |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 218 | } |
| 219 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 220 | namespace mac_address |
| 221 | { |
| 222 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 223 | bool isEmpty(const ether_addr& mac) |
| 224 | { |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 225 | return mac == ether_addr{}; |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 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 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 238 | } // namespace mac_address |
| 239 | } // namespace network |
| 240 | } // namespace phosphor |