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