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 | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 8 | #include <arpa/inet.h> |
| 9 | #include <dirent.h> |
William A. Kennington III | 1c77602 | 2022-01-05 14:12:16 -0800 | [diff] [blame] | 10 | #include <fmt/compile.h> |
| 11 | #include <fmt/format.h> |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 12 | #include <net/if.h> |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 13 | #include <sys/wait.h> |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 14 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 15 | #include <algorithm> |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 16 | #include <cctype> |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 17 | #include <charconv> |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 18 | #include <cstdlib> |
| 19 | #include <cstring> |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 20 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 21 | #include <list> |
William A. Kennington III | de433b7 | 2021-05-17 22:59:28 -0700 | [diff] [blame] | 22 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 23 | #include <nlohmann/json.hpp> |
William A. Kennington III | de433b7 | 2021-05-17 22:59:28 -0700 | [diff] [blame] | 24 | #endif |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 25 | #include <phosphor-logging/elog-errors.hpp> |
| 26 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 27 | #include <stdexcept> |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 28 | #include <stdplus/raw.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 29 | #include <string> |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 30 | #include <string_view> |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 31 | #include <variant> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 32 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 33 | |
| 34 | namespace phosphor |
| 35 | { |
| 36 | namespace network |
| 37 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 38 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 39 | using std::literals::string_view_literals::operator""sv; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 40 | using namespace phosphor::logging; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 41 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 42 | |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 43 | namespace internal |
| 44 | { |
| 45 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 46 | void executeCommandinChildProcess(stdplus::const_zstring path, char** args) |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 47 | { |
| 48 | using namespace std::string_literals; |
| 49 | pid_t pid = fork(); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 50 | |
| 51 | if (pid == 0) |
| 52 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 53 | execv(path.c_str(), args); |
| 54 | exit(255); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 55 | } |
| 56 | else if (pid < 0) |
| 57 | { |
| 58 | auto error = errno; |
| 59 | log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); |
| 60 | elog<InternalFailure>(); |
| 61 | } |
| 62 | else if (pid > 0) |
| 63 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 64 | int status; |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 65 | while (waitpid(pid, &status, 0) == -1) |
| 66 | { |
| 67 | if (errno != EINTR) |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 68 | { |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 69 | status = -1; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (status < 0) |
| 75 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 76 | fmt::memory_buffer buf; |
| 77 | fmt::format_to(fmt::appender(buf), "`{}`", path); |
| 78 | for (size_t i = 0; args[i] != nullptr; ++i) |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 79 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 80 | fmt::format_to(fmt::appender(buf), " `{}`", args[i]); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 81 | } |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 82 | buf.push_back('\0'); |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 83 | log<level::ERR>("Unable to execute the command", |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 84 | entry("CMD=%s", buf.data()), |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 85 | entry("STATUS=%d", status)); |
| 86 | elog<InternalFailure>(); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 91 | /** @brief Get ignored interfaces from environment */ |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 92 | std::string_view getIgnoredInterfacesEnv() |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 93 | { |
| 94 | auto r = std::getenv("IGNORED_INTERFACES"); |
| 95 | if (r == nullptr) |
| 96 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 97 | return ""; |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 98 | } |
| 99 | return r; |
| 100 | } |
| 101 | |
| 102 | /** @brief Parse the comma separated interface names */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 103 | std::unordered_set<std::string_view> |
| 104 | parseInterfaces(std::string_view interfaces) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 105 | { |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 106 | std::unordered_set<std::string_view> result; |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 107 | while (true) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 108 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 109 | auto sep = interfaces.find(','); |
| 110 | auto interface = interfaces.substr(0, sep); |
| 111 | while (!interface.empty() && std::isspace(interface.front())) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 112 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 113 | interface.remove_prefix(1); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 114 | } |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 115 | while (!interface.empty() && std::isspace(interface.back())) |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 116 | { |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 117 | interface.remove_suffix(1); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 118 | } |
William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 119 | if (!interface.empty()) |
| 120 | { |
| 121 | result.insert(interface); |
| 122 | } |
| 123 | if (sep == interfaces.npos) |
| 124 | { |
| 125 | break; |
| 126 | } |
| 127 | interfaces = interfaces.substr(sep + 1); |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 128 | } |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | /** @brief Get the ignored interfaces */ |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 133 | const std::unordered_set<std::string_view>& getIgnoredInterfaces() |
Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 134 | { |
| 135 | static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv()); |
| 136 | return ignoredInterfaces; |
| 137 | } |
| 138 | |
Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 139 | } // namespace internal |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 140 | |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 141 | constexpr auto familyVisit(auto&& visitor, int family) |
| 142 | { |
| 143 | if (family == AF_INET) |
| 144 | { |
| 145 | return visitor.template operator()<AF_INET>(); |
| 146 | } |
| 147 | else if (family == AF_INET6) |
| 148 | { |
| 149 | return visitor.template operator()<AF_INET6>(); |
| 150 | } |
| 151 | throw std::invalid_argument("Invalid addr family"); |
| 152 | } |
| 153 | |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 154 | template <int family> |
| 155 | typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf) |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 156 | { |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 157 | return stdplus::raw::copyFromStrict<typename FamilyTraits<family>::addr>( |
| 158 | buf); |
| 159 | } |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 160 | |
William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 161 | InAddrAny addrFromBuf(int family, std::string_view buf) |
| 162 | { |
| 163 | return familyVisit( |
| 164 | [=]<int f>() -> InAddrAny { return addrFromBuf<f>(buf); }, family); |
William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 167 | std::string toString(const struct in_addr& addr) |
| 168 | { |
| 169 | std::string ip(INET_ADDRSTRLEN, '\0'); |
| 170 | if (inet_ntop(AF_INET, &addr, ip.data(), ip.size()) == nullptr) |
| 171 | { |
| 172 | throw std::runtime_error("Failed to convert IP4 to string"); |
| 173 | } |
| 174 | |
| 175 | ip.resize(strlen(ip.c_str())); |
| 176 | return ip; |
| 177 | } |
| 178 | |
| 179 | std::string toString(const struct in6_addr& addr) |
| 180 | { |
| 181 | std::string ip(INET6_ADDRSTRLEN, '\0'); |
| 182 | if (inet_ntop(AF_INET6, &addr, ip.data(), ip.size()) == nullptr) |
| 183 | { |
| 184 | throw std::runtime_error("Failed to convert IP6 to string"); |
| 185 | } |
| 186 | |
| 187 | ip.resize(strlen(ip.c_str())); |
| 188 | return ip; |
| 189 | } |
| 190 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 191 | std::string toString(const InAddrAny& addr) |
| 192 | { |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 193 | if (std::holds_alternative<struct in_addr>(addr)) |
| 194 | { |
| 195 | const auto& v = std::get<struct in_addr>(addr); |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 196 | return toString(v); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 197 | } |
| 198 | else if (std::holds_alternative<struct in6_addr>(addr)) |
| 199 | { |
| 200 | const auto& v = std::get<struct in6_addr>(addr); |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 201 | return toString(v); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 202 | } |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 203 | |
| 204 | throw std::runtime_error("Invalid addr type"); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 205 | } |
| 206 | |
William A. Kennington III | 9f22830 | 2022-10-07 14:02:49 -0700 | [diff] [blame^] | 207 | bool isValidIP(int family, stdplus::const_zstring address) noexcept |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 208 | { |
| 209 | unsigned char buf[sizeof(struct in6_addr)]; |
William A. Kennington III | 9f22830 | 2022-10-07 14:02:49 -0700 | [diff] [blame^] | 210 | return inet_pton(family, address.c_str(), buf) > 0; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 211 | } |
| 212 | |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 213 | bool isValidPrefix(int family, uint8_t prefix) |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 214 | { |
William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 215 | return familyVisit( |
| 216 | [=]<int f>() noexcept { return isValidPrefix<f>(prefix); }, family); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 217 | } |
| 218 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 219 | InterfaceList getInterfaces() |
| 220 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 221 | InterfaceList interfaces{}; |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 222 | struct ifaddrs* ifaddr = nullptr; |
| 223 | |
| 224 | // attempt to fill struct with ifaddrs |
| 225 | if (getifaddrs(&ifaddr) == -1) |
| 226 | { |
| 227 | auto error = errno; |
| 228 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 229 | entry("ERRNO=%d", error)); |
| 230 | elog<InternalFailure>(); |
| 231 | } |
| 232 | |
| 233 | AddrPtr ifaddrPtr(ifaddr); |
| 234 | ifaddr = nullptr; |
Lei YU | efda98b | 2021-03-18 15:52:19 +0800 | [diff] [blame] | 235 | const auto& ignoredInterfaces = internal::getIgnoredInterfaces(); |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 236 | |
| 237 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 238 | { |
| 239 | // walk interfaces |
William A. Kennington III | f273d2b | 2019-03-21 14:38:36 -0700 | [diff] [blame] | 240 | // if loopback ignore |
Lei YU | efda98b | 2021-03-18 15:52:19 +0800 | [diff] [blame] | 241 | if (ifa->ifa_flags & IFF_LOOPBACK || |
| 242 | ignoredInterfaces.find(ifa->ifa_name) != ignoredInterfaces.end()) |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 243 | { |
| 244 | continue; |
| 245 | } |
Willy Tu | f7dce2e | 2022-10-07 05:48:08 +0000 | [diff] [blame] | 246 | interfaces.emplace(ifa->ifa_name); |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 247 | } |
| 248 | return interfaces; |
| 249 | } |
| 250 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 251 | void deleteInterface(stdplus::const_zstring intf) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 252 | { |
| 253 | pid_t pid = fork(); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 254 | int status{}; |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 255 | |
| 256 | if (pid == 0) |
| 257 | { |
| 258 | |
| 259 | execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr); |
| 260 | auto error = errno; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 261 | log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error), |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 262 | entry("INTF=%s", intf.c_str())); |
| 263 | elog<InternalFailure>(); |
| 264 | } |
| 265 | else if (pid < 0) |
| 266 | { |
| 267 | auto error = errno; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 268 | log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 269 | elog<InternalFailure>(); |
| 270 | } |
| 271 | else if (pid > 0) |
| 272 | { |
| 273 | while (waitpid(pid, &status, 0) == -1) |
| 274 | { |
| 275 | if (errno != EINTR) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 276 | { /* Error other than EINTR */ |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 277 | status = -1; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 282 | if (status < 0) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 283 | { |
| 284 | log<level::ERR>("Unable to delete the interface", |
Joseph Reynolds | 02653ca | 2018-05-10 15:55:09 -0500 | [diff] [blame] | 285 | entry("INTF=%s", intf.c_str()), |
| 286 | entry("STATUS=%d", status)); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 287 | elog<InternalFailure>(); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 292 | std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 293 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 294 | constexpr auto pfx = "eth"sv; |
| 295 | if (!intf.starts_with(pfx)) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 296 | { |
| 297 | return std::nullopt; |
| 298 | } |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 299 | intf.remove_prefix(pfx.size()); |
| 300 | auto last = intf.data() + intf.size(); |
| 301 | unsigned long idx; |
| 302 | auto res = std::from_chars(intf.data(), last, idx); |
| 303 | if (res.ec != std::errc() || res.ptr != last) |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 304 | { |
| 305 | return std::nullopt; |
| 306 | } |
| 307 | if (idx == 0) |
| 308 | { |
| 309 | return "ethaddr"; |
| 310 | } |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 311 | return fmt::format(FMT_COMPILE("eth{}addr"), idx); |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 312 | } |
| 313 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 314 | static std::optional<DHCPVal> systemdParseDHCP(std::string_view str) |
| 315 | { |
| 316 | if (config::icaseeq(str, "ipv4")) |
| 317 | { |
| 318 | return DHCPVal{.v4 = true, .v6 = false}; |
| 319 | } |
| 320 | if (config::icaseeq(str, "ipv6")) |
| 321 | { |
| 322 | return DHCPVal{.v4 = false, .v6 = true}; |
| 323 | } |
| 324 | if (auto b = config::parseBool(str); b) |
| 325 | { |
| 326 | return DHCPVal{.v4 = *b, .v6 = *b}; |
| 327 | } |
| 328 | return std::nullopt; |
| 329 | } |
| 330 | |
| 331 | inline auto systemdParseLast(const config::Parser& config, |
| 332 | std::string_view section, std::string_view key, |
| 333 | auto&& fun) |
| 334 | { |
| 335 | if (auto str = config.map.getLastValueString(section, key); str == nullptr) |
| 336 | { |
| 337 | auto err = fmt::format("Unable to get the value of {}[{}] from {}", |
| 338 | section, key, config.getFilename().native()); |
| 339 | log<level::NOTICE>(err.c_str(), |
| 340 | entry("FILE=%s", config.getFilename().c_str())); |
| 341 | } |
| 342 | else if (auto val = fun(*str); !val) |
| 343 | { |
| 344 | auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section, |
| 345 | key, config.getFilename().native(), *str); |
| 346 | log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()), |
| 347 | entry("FILE=%s", config.getFilename().c_str())); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | return val; |
| 352 | } |
| 353 | return decltype(fun(std::string_view{}))(std::nullopt); |
| 354 | } |
| 355 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 356 | bool getIPv6AcceptRA(const config::Parser& config) |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 357 | { |
William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 358 | #ifdef ENABLE_IPV6_ACCEPT_RA |
| 359 | constexpr bool def = true; |
| 360 | #else |
| 361 | constexpr bool def = false; |
| 362 | #endif |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 363 | return systemdParseLast(config, "Network", "IPv6AcceptRA", |
| 364 | config::parseBool) |
| 365 | .value_or(def); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 366 | } |
| 367 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 368 | DHCPVal getDHCPValue(const config::Parser& config) |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 369 | { |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 370 | return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP) |
| 371 | .value_or(DHCPVal{.v4 = true, .v6 = true}); |
| 372 | } |
William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 373 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 374 | bool getDHCPProp(const config::Parser& config, std::string_view key) |
| 375 | { |
| 376 | return systemdParseLast(config, "DHCP", key, config::parseBool) |
| 377 | .value_or(true); |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 378 | } |
| 379 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 380 | namespace mac_address |
| 381 | { |
| 382 | |
| 383 | constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper"; |
| 384 | constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper"; |
| 385 | constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 386 | constexpr auto propIntf = "org.freedesktop.DBus.Properties"; |
| 387 | constexpr auto methodGet = "Get"; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 388 | constexpr auto configFile = "/usr/share/network/config.json"; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 389 | |
| 390 | using DbusObjectPath = std::string; |
| 391 | using DbusService = std::string; |
| 392 | using DbusInterface = std::string; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 393 | using ObjectTree = |
| 394 | std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 395 | |
| 396 | constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager"; |
| 397 | constexpr auto invNetworkIntf = |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 398 | "xyz.openbmc_project.Inventory.Item.NetworkInterface"; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 399 | constexpr auto invRoot = "/xyz/openbmc_project/inventory"; |
| 400 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 401 | ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 402 | { |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 403 | |
| 404 | std::string interfaceName = intfName; |
| 405 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 406 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 407 | // load the config JSON from the Read Only Path |
| 408 | std::ifstream in(configFile); |
| 409 | nlohmann::json configJson; |
| 410 | in >> configJson; |
| 411 | interfaceName = configJson[intfName]; |
| 412 | #endif |
| 413 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 414 | std::vector<DbusInterface> interfaces; |
| 415 | interfaces.emplace_back(invNetworkIntf); |
| 416 | |
| 417 | auto depth = 0; |
| 418 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 419 | auto mapperCall = |
| 420 | bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree"); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 421 | |
| 422 | mapperCall.append(invRoot, depth, interfaces); |
| 423 | |
| 424 | auto mapperReply = bus.call(mapperCall); |
| 425 | if (mapperReply.is_method_error()) |
| 426 | { |
| 427 | log<level::ERR>("Error in mapper call"); |
| 428 | elog<InternalFailure>(); |
| 429 | } |
| 430 | |
| 431 | ObjectTree objectTree; |
| 432 | mapperReply.read(objectTree); |
| 433 | |
| 434 | if (objectTree.empty()) |
| 435 | { |
| 436 | log<level::ERR>("No Object has implemented the interface", |
| 437 | entry("INTERFACE=%s", invNetworkIntf)); |
| 438 | elog<InternalFailure>(); |
| 439 | } |
| 440 | |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 441 | DbusObjectPath objPath; |
| 442 | DbusService service; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 443 | |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 444 | if (1 == objectTree.size()) |
| 445 | { |
| 446 | objPath = objectTree.begin()->first; |
| 447 | service = objectTree.begin()->second.begin()->first; |
| 448 | } |
| 449 | else |
| 450 | { |
| 451 | // If there are more than 2 objects, object path must contain the |
| 452 | // interface name |
| 453 | for (auto const& object : objectTree) |
| 454 | { |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 455 | log<level::INFO>("interface", |
| 456 | entry("INT=%s", interfaceName.c_str())); |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 457 | log<level::INFO>("object", entry("OBJ=%s", object.first.c_str())); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 458 | |
| 459 | if (std::string::npos != object.first.find(interfaceName.c_str())) |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 460 | { |
| 461 | objPath = object.first; |
| 462 | service = object.second.begin()->first; |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (objPath.empty()) |
| 468 | { |
| 469 | log<level::ERR>("Can't find the object for the interface", |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 470 | entry("intfName=%s", interfaceName.c_str())); |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 471 | elog<InternalFailure>(); |
| 472 | } |
| 473 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 474 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 475 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 476 | propIntf, methodGet); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 477 | |
| 478 | method.append(invNetworkIntf, "MACAddress"); |
| 479 | |
| 480 | auto reply = bus.call(method); |
| 481 | if (reply.is_method_error()) |
| 482 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 483 | log<level::ERR>("Failed to get MACAddress", |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 484 | entry("PATH=%s", objPath.c_str()), |
| 485 | entry("INTERFACE=%s", invNetworkIntf)); |
| 486 | elog<InternalFailure>(); |
| 487 | } |
| 488 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 489 | std::variant<std::string> value; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 490 | reply.read(value); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 491 | return fromString(std::get<std::string>(value)); |
| 492 | } |
| 493 | |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 494 | static uint8_t decodeHex(std::string_view str) |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 495 | { |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 496 | uint8_t ret; |
| 497 | auto res = std::from_chars(str.begin(), str.end(), ret, 16); |
| 498 | if (res.ptr != str.end() || res.ec != std::errc()) |
| 499 | { |
| 500 | throw std::invalid_argument("Not hex"); |
| 501 | } |
| 502 | return ret; |
| 503 | } |
Potin Lai | da0b1d4 | 2021-12-26 20:08:20 +0800 | [diff] [blame] | 504 | |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 505 | ether_addr fromString(std::string_view str) |
| 506 | { |
| 507 | ether_addr ret; |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 508 | if (str.size() == 12 && str.find(":") == str.npos) |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 509 | { |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 510 | for (size_t i = 0; i < 6; ++i) |
| 511 | { |
| 512 | ret.ether_addr_octet[i] = decodeHex(str.substr(i * 2, 2)); |
| 513 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 514 | } |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 515 | else |
Potin Lai | da0b1d4 | 2021-12-26 20:08:20 +0800 | [diff] [blame] | 516 | { |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 517 | for (size_t i = 0; i < 5; ++i) |
| 518 | { |
| 519 | auto loc = str.find(":"); |
| 520 | ret.ether_addr_octet[i] = decodeHex(str.substr(0, loc)); |
| 521 | str.remove_prefix(loc == str.npos ? str.size() : loc + 1); |
| 522 | if (str.empty()) |
| 523 | { |
| 524 | throw std::invalid_argument("Missing mac data"); |
| 525 | } |
| 526 | } |
| 527 | ret.ether_addr_octet[5] = decodeHex(str); |
Potin Lai | da0b1d4 | 2021-12-26 20:08:20 +0800 | [diff] [blame] | 528 | } |
William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 529 | return ret; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 530 | } |
| 531 | |
William A. Kennington III | 6ca08d8 | 2019-04-20 16:04:18 -0700 | [diff] [blame] | 532 | std::string toString(const ether_addr& mac) |
William A. Kennington III | a14879e | 2019-02-01 21:43:11 -0800 | [diff] [blame] | 533 | { |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 534 | return fmt::format(FMT_COMPILE("{:02x}"), |
| 535 | fmt::join(mac.ether_addr_octet, ":")); |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 536 | } |
| 537 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 538 | bool isEmpty(const ether_addr& mac) |
| 539 | { |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 540 | return stdplus::raw::equal(mac, ether_addr{}); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | bool isMulticast(const ether_addr& mac) |
| 544 | { |
| 545 | return mac.ether_addr_octet[0] & 0b1; |
| 546 | } |
| 547 | |
| 548 | bool isUnicast(const ether_addr& mac) |
| 549 | { |
| 550 | return !isEmpty(mac) && !isMulticast(mac); |
| 551 | } |
| 552 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 553 | } // namespace mac_address |
| 554 | } // namespace network |
| 555 | } // namespace phosphor |