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 | |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame^] | 179 | std::string toString(const struct in_addr& addr) |
| 180 | { |
| 181 | std::string ip(INET_ADDRSTRLEN, '\0'); |
| 182 | if (inet_ntop(AF_INET, &addr, ip.data(), ip.size()) == nullptr) |
| 183 | { |
| 184 | throw std::runtime_error("Failed to convert IP4 to string"); |
| 185 | } |
| 186 | |
| 187 | ip.resize(strlen(ip.c_str())); |
| 188 | return ip; |
| 189 | } |
| 190 | |
| 191 | std::string toString(const struct in6_addr& addr) |
| 192 | { |
| 193 | std::string ip(INET6_ADDRSTRLEN, '\0'); |
| 194 | if (inet_ntop(AF_INET6, &addr, ip.data(), ip.size()) == nullptr) |
| 195 | { |
| 196 | throw std::runtime_error("Failed to convert IP6 to string"); |
| 197 | } |
| 198 | |
| 199 | ip.resize(strlen(ip.c_str())); |
| 200 | return ip; |
| 201 | } |
| 202 | |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 203 | std::string toString(const InAddrAny& addr) |
| 204 | { |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 205 | if (std::holds_alternative<struct in_addr>(addr)) |
| 206 | { |
| 207 | const auto& v = std::get<struct in_addr>(addr); |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame^] | 208 | return toString(v); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 209 | } |
| 210 | else if (std::holds_alternative<struct in6_addr>(addr)) |
| 211 | { |
| 212 | const auto& v = std::get<struct in6_addr>(addr); |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame^] | 213 | return toString(v); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 214 | } |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame^] | 215 | |
| 216 | throw std::runtime_error("Invalid addr type"); |
William A. Kennington III | 5058f57 | 2019-01-30 17:18:14 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 219 | bool isLinkLocalIP(const std::string& address) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 220 | { |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 221 | return address.find(IPV4_PREFIX) == 0 || address.find(IPV6_PREFIX) == 0; |
| 222 | } |
| 223 | |
| 224 | bool isValidIP(int addressFamily, const std::string& address) |
| 225 | { |
| 226 | unsigned char buf[sizeof(struct in6_addr)]; |
| 227 | |
| 228 | return inet_pton(addressFamily, address.c_str(), buf) > 0; |
| 229 | } |
| 230 | |
| 231 | bool isValidPrefix(int addressFamily, uint8_t prefixLength) |
| 232 | { |
| 233 | if (addressFamily == AF_INET) |
| 234 | { |
| 235 | if (prefixLength < IPV4_MIN_PREFIX_LENGTH || |
| 236 | prefixLength > IPV4_MAX_PREFIX_LENGTH) |
| 237 | { |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (addressFamily == AF_INET6) |
| 243 | { |
| 244 | if (prefixLength < IPV4_MIN_PREFIX_LENGTH || |
| 245 | prefixLength > IPV6_MAX_PREFIX_LENGTH) |
| 246 | { |
| 247 | return false; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return true; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 252 | } |
| 253 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 254 | IntfAddrMap getInterfaceAddrs() |
| 255 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 256 | IntfAddrMap intfMap{}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 257 | struct ifaddrs* ifaddr = nullptr; |
| 258 | |
| 259 | // attempt to fill struct with ifaddrs |
| 260 | if (getifaddrs(&ifaddr) == -1) |
| 261 | { |
| 262 | auto error = errno; |
| 263 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 264 | entry("ERRNO=%s", strerror(error))); |
| 265 | elog<InternalFailure>(); |
| 266 | } |
| 267 | |
| 268 | AddrPtr ifaddrPtr(ifaddr); |
| 269 | ifaddr = nullptr; |
| 270 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 271 | std::string intfName{}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 272 | |
| 273 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 274 | { |
| 275 | // walk interfaces |
| 276 | if (ifa->ifa_addr == nullptr) |
| 277 | { |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | // get only INET interfaces not ipv6 |
| 282 | if (ifa->ifa_addr->sa_family == AF_INET || |
| 283 | ifa->ifa_addr->sa_family == AF_INET6) |
| 284 | { |
| 285 | // if loopback, or not running ignore |
| 286 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 287 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 288 | { |
| 289 | continue; |
| 290 | } |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 291 | intfName = ifa->ifa_name; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 292 | AddrInfo info{}; |
| 293 | char ip[INET6_ADDRSTRLEN] = {0}; |
| 294 | char subnetMask[INET6_ADDRSTRLEN] = {0}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 295 | |
| 296 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 297 | { |
| 298 | |
| 299 | inet_ntop(ifa->ifa_addr->sa_family, |
| 300 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_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_in*)(ifa->ifa_netmask))->sin_addr), |
| 306 | subnetMask, sizeof(subnetMask)); |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 307 | } |
| 308 | else |
| 309 | { |
| 310 | inet_ntop(ifa->ifa_addr->sa_family, |
| 311 | &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 312 | ip, sizeof(ip)); |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 313 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 314 | inet_ntop( |
| 315 | ifa->ifa_addr->sa_family, |
| 316 | &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr), |
| 317 | subnetMask, sizeof(subnetMask)); |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | info.addrType = ifa->ifa_addr->sa_family; |
| 321 | info.ipaddress = ip; |
| 322 | info.prefix = toCidr(info.addrType, std::string(subnetMask)); |
David Cobbley | 269501c | 2018-05-25 15:55:56 -0700 | [diff] [blame] | 323 | intfMap[intfName].push_back(info); |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 324 | } |
| 325 | } |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 326 | return intfMap; |
| 327 | } |
| 328 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 329 | InterfaceList getInterfaces() |
| 330 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 331 | InterfaceList interfaces{}; |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 332 | struct ifaddrs* ifaddr = nullptr; |
| 333 | |
| 334 | // attempt to fill struct with ifaddrs |
| 335 | if (getifaddrs(&ifaddr) == -1) |
| 336 | { |
| 337 | auto error = errno; |
| 338 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 339 | entry("ERRNO=%d", error)); |
| 340 | elog<InternalFailure>(); |
| 341 | } |
| 342 | |
| 343 | AddrPtr ifaddrPtr(ifaddr); |
| 344 | ifaddr = nullptr; |
| 345 | |
| 346 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 347 | { |
| 348 | // walk interfaces |
William A. Kennington III | f273d2b | 2019-03-21 14:38:36 -0700 | [diff] [blame] | 349 | // if loopback ignore |
| 350 | if (ifa->ifa_flags & IFF_LOOPBACK) |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 351 | { |
| 352 | continue; |
| 353 | } |
| 354 | interfaces.emplace(ifa->ifa_name); |
| 355 | } |
| 356 | return interfaces; |
| 357 | } |
| 358 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 359 | void deleteInterface(const std::string& intf) |
| 360 | { |
| 361 | pid_t pid = fork(); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 362 | int status{}; |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 363 | |
| 364 | if (pid == 0) |
| 365 | { |
| 366 | |
| 367 | execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr); |
| 368 | auto error = errno; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 369 | log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error), |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 370 | entry("INTF=%s", intf.c_str())); |
| 371 | elog<InternalFailure>(); |
| 372 | } |
| 373 | else if (pid < 0) |
| 374 | { |
| 375 | auto error = errno; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 376 | log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 377 | elog<InternalFailure>(); |
| 378 | } |
| 379 | else if (pid > 0) |
| 380 | { |
| 381 | while (waitpid(pid, &status, 0) == -1) |
| 382 | { |
| 383 | if (errno != EINTR) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 384 | { /* Error other than EINTR */ |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 385 | status = -1; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 390 | if (status < 0) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 391 | { |
| 392 | log<level::ERR>("Unable to delete the interface", |
Joseph Reynolds | 02653ca | 2018-05-10 15:55:09 -0500 | [diff] [blame] | 393 | entry("INTF=%s", intf.c_str()), |
| 394 | entry("STATUS=%d", status)); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 395 | elog<InternalFailure>(); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 400 | std::optional<std::string> interfaceToUbootEthAddr(const char* intf) |
| 401 | { |
| 402 | constexpr char ethPrefix[] = "eth"; |
| 403 | constexpr size_t ethPrefixLen = sizeof(ethPrefix) - 1; |
| 404 | if (strncmp(ethPrefix, intf, ethPrefixLen) != 0) |
| 405 | { |
| 406 | return std::nullopt; |
| 407 | } |
| 408 | const auto intfSuffix = intf + ethPrefixLen; |
| 409 | if (intfSuffix[0] == '\0') |
| 410 | { |
| 411 | return std::nullopt; |
| 412 | } |
| 413 | char* end; |
| 414 | unsigned long idx = strtoul(intfSuffix, &end, 10); |
| 415 | if (end[0] != '\0') |
| 416 | { |
| 417 | return std::nullopt; |
| 418 | } |
| 419 | if (idx == 0) |
| 420 | { |
| 421 | return "ethaddr"; |
| 422 | } |
| 423 | return "eth" + std::to_string(idx) + "addr"; |
| 424 | } |
| 425 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 426 | EthernetInterfaceIntf::DHCPConf getDHCPValue(const std::string& confDir, |
| 427 | const std::string& intf) |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 428 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 429 | EthernetInterfaceIntf::DHCPConf dhcp = |
| 430 | EthernetInterfaceIntf::DHCPConf::none; |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 431 | // Get the interface mode value from systemd conf |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 432 | // using namespace std::string_literals; |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 433 | fs::path confPath = confDir; |
| 434 | std::string fileName = systemd::config::networkFilePrefix + intf + |
| 435 | systemd::config::networkFileSuffix; |
| 436 | confPath /= fileName; |
| 437 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 438 | auto rc = config::ReturnCode::SUCCESS; |
| 439 | config::ValueList values; |
| 440 | config::Parser parser(confPath.string()); |
| 441 | |
| 442 | std::tie(rc, values) = parser.getValues("Network", "DHCP"); |
| 443 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 444 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 445 | log<level::DEBUG>("Unable to get the value for Network[DHCP]", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 446 | entry("RC=%d", rc)); |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 447 | return dhcp; |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 448 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 449 | // There will be only single value for DHCP key. |
| 450 | if (values[0] == "true") |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 451 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 452 | dhcp = EthernetInterfaceIntf::DHCPConf::both; |
| 453 | } |
| 454 | else if (values[0] == "ipv4") |
| 455 | { |
| 456 | dhcp = EthernetInterfaceIntf::DHCPConf::v4; |
| 457 | } |
| 458 | else if (values[0] == "ipv6") |
| 459 | { |
| 460 | dhcp = EthernetInterfaceIntf::DHCPConf::v6; |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 461 | } |
| 462 | return dhcp; |
| 463 | } |
| 464 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 465 | namespace internal |
| 466 | { |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 467 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 468 | void executeCommandinChildProcess(const char* path, char** args) |
| 469 | { |
| 470 | using namespace std::string_literals; |
| 471 | pid_t pid = fork(); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 472 | int status{}; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 473 | |
| 474 | if (pid == 0) |
| 475 | { |
| 476 | execv(path, args); |
| 477 | auto error = errno; |
| 478 | // create the command from var args. |
| 479 | std::string command = path + " "s; |
| 480 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 481 | for (int i = 0; args[i]; i++) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 482 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 483 | command += args[i] + " "s; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | log<level::ERR>("Couldn't exceute the command", |
| 487 | entry("ERRNO=%d", error), |
| 488 | entry("CMD=%s", command.c_str())); |
| 489 | elog<InternalFailure>(); |
| 490 | } |
| 491 | else if (pid < 0) |
| 492 | { |
| 493 | auto error = errno; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 494 | log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 495 | elog<InternalFailure>(); |
| 496 | } |
| 497 | else if (pid > 0) |
| 498 | { |
| 499 | while (waitpid(pid, &status, 0) == -1) |
| 500 | { |
| 501 | if (errno != EINTR) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 502 | { // Error other than EINTR |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 503 | status = -1; |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 508 | if (status < 0) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 509 | { |
| 510 | std::string command = path + " "s; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 511 | for (int i = 0; args[i]; i++) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 512 | { |
| 513 | command += args[i] + " "s; |
| 514 | } |
| 515 | |
| 516 | log<level::ERR>("Unable to execute the command", |
Joseph Reynolds | 02653ca | 2018-05-10 15:55:09 -0500 | [diff] [blame] | 517 | entry("CMD=%s", command.c_str()), |
| 518 | entry("STATUS=%d", status)); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 519 | elog<InternalFailure>(); |
| 520 | } |
| 521 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 522 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 523 | } // namespace internal |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 524 | |
| 525 | namespace mac_address |
| 526 | { |
| 527 | |
| 528 | constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper"; |
| 529 | constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper"; |
| 530 | constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 531 | constexpr auto propIntf = "org.freedesktop.DBus.Properties"; |
| 532 | constexpr auto methodGet = "Get"; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 533 | constexpr auto configFile = "/usr/share/network/config.json"; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 534 | |
| 535 | using DbusObjectPath = std::string; |
| 536 | using DbusService = std::string; |
| 537 | using DbusInterface = std::string; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 538 | using ObjectTree = |
| 539 | std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 540 | |
| 541 | constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager"; |
| 542 | constexpr auto invNetworkIntf = |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 543 | "xyz.openbmc_project.Inventory.Item.NetworkInterface"; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 544 | constexpr auto invRoot = "/xyz/openbmc_project/inventory"; |
| 545 | |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 546 | ether_addr getfromInventory(sdbusplus::bus::bus& bus, |
| 547 | const std::string& intfName) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 548 | { |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 549 | |
| 550 | std::string interfaceName = intfName; |
| 551 | |
| 552 | #if SYNC_MAC_FROM_INVENTORY |
| 553 | // load the config JSON from the Read Only Path |
| 554 | std::ifstream in(configFile); |
| 555 | nlohmann::json configJson; |
| 556 | in >> configJson; |
| 557 | interfaceName = configJson[intfName]; |
| 558 | #endif |
| 559 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 560 | std::vector<DbusInterface> interfaces; |
| 561 | interfaces.emplace_back(invNetworkIntf); |
| 562 | |
| 563 | auto depth = 0; |
| 564 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 565 | auto mapperCall = |
| 566 | bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree"); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 567 | |
| 568 | mapperCall.append(invRoot, depth, interfaces); |
| 569 | |
| 570 | auto mapperReply = bus.call(mapperCall); |
| 571 | if (mapperReply.is_method_error()) |
| 572 | { |
| 573 | log<level::ERR>("Error in mapper call"); |
| 574 | elog<InternalFailure>(); |
| 575 | } |
| 576 | |
| 577 | ObjectTree objectTree; |
| 578 | mapperReply.read(objectTree); |
| 579 | |
| 580 | if (objectTree.empty()) |
| 581 | { |
| 582 | log<level::ERR>("No Object has implemented the interface", |
| 583 | entry("INTERFACE=%s", invNetworkIntf)); |
| 584 | elog<InternalFailure>(); |
| 585 | } |
| 586 | |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 587 | DbusObjectPath objPath; |
| 588 | DbusService service; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 589 | |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 590 | if (1 == objectTree.size()) |
| 591 | { |
| 592 | objPath = objectTree.begin()->first; |
| 593 | service = objectTree.begin()->second.begin()->first; |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | // If there are more than 2 objects, object path must contain the |
| 598 | // interface name |
| 599 | for (auto const& object : objectTree) |
| 600 | { |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 601 | log<level::INFO>("interface", |
| 602 | entry("INT=%s", interfaceName.c_str())); |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 603 | log<level::INFO>("object", entry("OBJ=%s", object.first.c_str())); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 604 | |
| 605 | if (std::string::npos != object.first.find(interfaceName.c_str())) |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 606 | { |
| 607 | objPath = object.first; |
| 608 | service = object.second.begin()->first; |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (objPath.empty()) |
| 614 | { |
| 615 | log<level::ERR>("Can't find the object for the interface", |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 616 | entry("intfName=%s", interfaceName.c_str())); |
Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 617 | elog<InternalFailure>(); |
| 618 | } |
| 619 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 620 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 621 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 622 | propIntf, methodGet); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 623 | |
| 624 | method.append(invNetworkIntf, "MACAddress"); |
| 625 | |
| 626 | auto reply = bus.call(method); |
| 627 | if (reply.is_method_error()) |
| 628 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 629 | log<level::ERR>("Failed to get MACAddress", |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 630 | entry("PATH=%s", objPath.c_str()), |
| 631 | entry("INTERFACE=%s", invNetworkIntf)); |
| 632 | elog<InternalFailure>(); |
| 633 | } |
| 634 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 635 | std::variant<std::string> value; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 636 | reply.read(value); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 637 | return fromString(std::get<std::string>(value)); |
| 638 | } |
| 639 | |
| 640 | ether_addr fromString(const char* str) |
| 641 | { |
| 642 | struct ether_addr* mac = ether_aton(str); |
| 643 | if (mac == nullptr) |
| 644 | { |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 645 | throw std::invalid_argument("Invalid MAC Address"); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 646 | } |
| 647 | return *mac; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 648 | } |
| 649 | |
William A. Kennington III | 6ca08d8 | 2019-04-20 16:04:18 -0700 | [diff] [blame] | 650 | std::string toString(const ether_addr& mac) |
William A. Kennington III | a14879e | 2019-02-01 21:43:11 -0800 | [diff] [blame] | 651 | { |
Karthick Sundarrajan | dbd328d | 2019-11-20 15:19:08 -0800 | [diff] [blame] | 652 | char buf[18] = {0}; |
| 653 | snprintf(buf, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac.ether_addr_octet[0], |
| 654 | mac.ether_addr_octet[1], mac.ether_addr_octet[2], |
| 655 | mac.ether_addr_octet[3], mac.ether_addr_octet[4], |
| 656 | mac.ether_addr_octet[5]); |
| 657 | return buf; |
William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 658 | } |
| 659 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 660 | bool isEmpty(const ether_addr& mac) |
| 661 | { |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 662 | return stdplus::raw::equal(mac, ether_addr{}); |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | bool isMulticast(const ether_addr& mac) |
| 666 | { |
| 667 | return mac.ether_addr_octet[0] & 0b1; |
| 668 | } |
| 669 | |
| 670 | bool isUnicast(const ether_addr& mac) |
| 671 | { |
| 672 | return !isEmpty(mac) && !isMulticast(mac); |
| 673 | } |
| 674 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 675 | } // namespace mac_address |
| 676 | } // namespace network |
| 677 | } // namespace phosphor |