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