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