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