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