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