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