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