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