blob: 63b8f7ee1f27edd3253c0785b3d413c51eb7d421 [file] [log] [blame]
Ratan Gupta3681a502017-06-17 19:20:04 +05301#include "util.hpp"
Ratan Gupta11cef802017-05-29 08:41:48 +05302
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include "config_parser.hpp"
4#include "types.hpp"
Ratan Gupta8804feb2017-05-25 10:49:57 +05305
Ratan Gupta3681a502017-06-17 19:20:04 +05306#include <arpa/inet.h>
7#include <dirent.h>
8#include <net/if.h>
Ratan Guptabc886292017-07-25 18:29:57 +05309#include <sys/wait.h>
Ratan Gupta3681a502017-06-17 19:20:04 +053010
Ratan Gupta8804feb2017-05-25 10:49:57 +053011#include <algorithm>
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070012#include <cstdlib>
13#include <cstring>
Manojkiran Edaa879baa2020-06-13 14:39:08 +053014#include <filesystem>
Manojkiran Edacc099a82020-05-11 14:25:16 +053015#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -070016#include <list>
Manojkiran Edacc099a82020-05-11 14:25:16 +053017#include <nlohmann/json.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070018#include <phosphor-logging/elog-errors.hpp>
19#include <phosphor-logging/log.hpp>
William A. Kennington III5058f572019-01-30 17:18:14 -080020#include <stdexcept>
William A. Kennington III12beaad2020-06-13 19:30:41 -070021#include <stdplus/raw.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070022#include <string>
William A. Kennington III1137a972019-04-20 20:49:58 -070023#include <variant>
Patrick Venture189d44e2018-07-09 12:30:59 -070024#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053025
26namespace phosphor
27{
28namespace network
29{
Ratan Guptabc886292017-07-25 18:29:57 +053030
Ratan Gupta8804feb2017-05-25 10:49:57 +053031namespace
32{
33
34using namespace phosphor::logging;
Ratan Gupta11cef802017-05-29 08:41:48 +053035using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Manojkiran Edaa879baa2020-06-13 14:39:08 +053036namespace fs = std::filesystem;
Ratan Gupta8804feb2017-05-25 10:49:57 +053037
38uint8_t toV6Cidr(const std::string& subnetMask)
39{
40 uint8_t pos = 0;
41 uint8_t prevPos = 0;
42 uint8_t cidr = 0;
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 uint16_t buff{};
Ratan Gupta8804feb2017-05-25 10:49:57 +053044 do
45 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 // subnet mask look like ffff:ffff::
Ratan Gupta8804feb2017-05-25 10:49:57 +053047 // or ffff:c000::
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 pos = subnetMask.find(":", prevPos);
Ratan Gupta8804feb2017-05-25 10:49:57 +053049 if (pos == std::string::npos)
50 {
51 break;
52 }
53
54 auto str = subnetMask.substr(prevPos, (pos - prevPos));
55 prevPos = pos + 1;
56
57 // String length is 0
58 if (!str.length())
59 {
60 return cidr;
61 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 // converts it into number.
Ratan Gupta8804feb2017-05-25 10:49:57 +053063 if (sscanf(str.c_str(), "%hx", &buff) <= 0)
64 {
65 log<level::ERR>("Invalid Mask",
Joseph Reynolds02653ca2018-05-10 15:55:09 -050066 entry("SUBNETMASK=%s", subnetMask.c_str()));
Ratan Gupta8804feb2017-05-25 10:49:57 +053067
68 return 0;
69 }
70
71 // convert the number into bitset
72 // and check for how many ones are there.
73 // if we don't have all the ones then make
74 // sure that all the ones should be left justify.
75
76 if (__builtin_popcount(buff) != 16)
77 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) !=
79 __builtin_popcount(buff))
Ratan Gupta8804feb2017-05-25 10:49:57 +053080 {
81 log<level::ERR>("Invalid Mask",
Joseph Reynolds02653ca2018-05-10 15:55:09 -050082 entry("SUBNETMASK=%s", subnetMask.c_str()));
Ratan Gupta8804feb2017-05-25 10:49:57 +053083
84 return 0;
85 }
86 cidr += __builtin_popcount(buff);
87 return cidr;
88 }
89
90 cidr += 16;
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 } while (1);
Ratan Gupta8804feb2017-05-25 10:49:57 +053092
93 return cidr;
94}
Gunnar Mills57d9c502018-09-14 14:42:34 -050095} // anonymous namespace
Ratan Gupta8804feb2017-05-25 10:49:57 +053096
97uint8_t toCidr(int addressFamily, const std::string& subnetMask)
98{
99 if (addressFamily == AF_INET6)
100 {
101 return toV6Cidr(subnetMask);
102 }
103
104 uint32_t buff;
105
106 auto rc = inet_pton(addressFamily, subnetMask.c_str(), &buff);
107 if (rc <= 0)
108 {
109 log<level::ERR>("inet_pton failed:",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500110 entry("SUBNETMASK=%s", subnetMask.c_str()));
Ratan Gupta8804feb2017-05-25 10:49:57 +0530111 return 0;
112 }
113
114 buff = be32toh(buff);
115 // total no of bits - total no of leading zero == total no of ones
Gunnar Mills57d9c502018-09-14 14:42:34 -0500116 if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) ==
117 __builtin_popcount(buff))
Ratan Gupta8804feb2017-05-25 10:49:57 +0530118 {
119 return __builtin_popcount(buff);
120 }
121 else
122 {
123 log<level::ERR>("Invalid Mask",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500124 entry("SUBNETMASK=%s", subnetMask.c_str()));
Ratan Gupta8804feb2017-05-25 10:49:57 +0530125 return 0;
126 }
127}
128
129std::string toMask(int addressFamily, uint8_t prefix)
130{
131 if (addressFamily == AF_INET6)
132 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500133 // TODO:- conversion for v6
Ratan Gupta8804feb2017-05-25 10:49:57 +0530134 return "";
135 }
136
137 if (prefix < 1 || prefix > 30)
138 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500139 log<level::ERR>("Invalid Prefix", entry("PREFIX=%d", prefix));
Ratan Gupta8804feb2017-05-25 10:49:57 +0530140 return "";
141 }
142 /* Create the netmask from the number of bits */
143 unsigned long mask = 0;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500144 for (auto i = 0; i < prefix; i++)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530145 {
146 mask |= 1 << (31 - i);
147 }
148 struct in_addr netmask;
149 netmask.s_addr = htonl(mask);
150 return inet_ntoa(netmask);
151}
152
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800153InAddrAny addrFromBuf(int addressFamily, std::string_view buf)
154{
155 if (addressFamily == AF_INET)
156 {
157 struct in_addr ret;
158 if (buf.size() != sizeof(ret))
159 {
160 throw std::runtime_error("Buf not in_addr sized");
161 }
162 memcpy(&ret, buf.data(), sizeof(ret));
163 return ret;
164 }
165 else if (addressFamily == AF_INET6)
166 {
167 struct in6_addr ret;
168 if (buf.size() != sizeof(ret))
169 {
170 throw std::runtime_error("Buf not in6_addr sized");
171 }
172 memcpy(&ret, buf.data(), sizeof(ret));
173 return ret;
174 }
175
176 throw std::runtime_error("Unsupported address family");
177}
178
William A. Kennington III5058f572019-01-30 17:18:14 -0800179std::string toString(const InAddrAny& addr)
180{
181 std::string ip;
182 if (std::holds_alternative<struct in_addr>(addr))
183 {
184 const auto& v = std::get<struct in_addr>(addr);
185 ip.resize(INET_ADDRSTRLEN);
186 if (inet_ntop(AF_INET, &v, ip.data(), ip.size()) == NULL)
187 {
188 throw std::runtime_error("Failed to convert IP4 to string");
189 }
190 }
191 else if (std::holds_alternative<struct in6_addr>(addr))
192 {
193 const auto& v = std::get<struct in6_addr>(addr);
194 ip.resize(INET6_ADDRSTRLEN);
195 if (inet_ntop(AF_INET6, &v, ip.data(), ip.size()) == NULL)
196 {
197 throw std::runtime_error("Failed to convert IP6 to string");
198 }
199 }
200 else
201 {
202 throw std::runtime_error("Invalid addr type");
203 }
204 ip.resize(strlen(ip.c_str()));
205 return ip;
206}
207
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500208bool isLinkLocalIP(const std::string& address)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530209{
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500210 return address.find(IPV4_PREFIX) == 0 || address.find(IPV6_PREFIX) == 0;
211}
212
213bool isValidIP(int addressFamily, const std::string& address)
214{
215 unsigned char buf[sizeof(struct in6_addr)];
216
217 return inet_pton(addressFamily, address.c_str(), buf) > 0;
218}
219
220bool isValidPrefix(int addressFamily, uint8_t prefixLength)
221{
222 if (addressFamily == AF_INET)
223 {
224 if (prefixLength < IPV4_MIN_PREFIX_LENGTH ||
225 prefixLength > IPV4_MAX_PREFIX_LENGTH)
226 {
227 return false;
228 }
229 }
230
231 if (addressFamily == AF_INET6)
232 {
233 if (prefixLength < IPV4_MIN_PREFIX_LENGTH ||
234 prefixLength > IPV6_MAX_PREFIX_LENGTH)
235 {
236 return false;
237 }
238 }
239
240 return true;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530241}
242
Ratan Gupta3681a502017-06-17 19:20:04 +0530243IntfAddrMap getInterfaceAddrs()
244{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500245 IntfAddrMap intfMap{};
Ratan Gupta3681a502017-06-17 19:20:04 +0530246 struct ifaddrs* ifaddr = nullptr;
247
248 // attempt to fill struct with ifaddrs
249 if (getifaddrs(&ifaddr) == -1)
250 {
251 auto error = errno;
252 log<level::ERR>("Error occurred during the getifaddrs call",
253 entry("ERRNO=%s", strerror(error)));
254 elog<InternalFailure>();
255 }
256
257 AddrPtr ifaddrPtr(ifaddr);
258 ifaddr = nullptr;
259
Gunnar Mills57d9c502018-09-14 14:42:34 -0500260 std::string intfName{};
Ratan Gupta3681a502017-06-17 19:20:04 +0530261
262 for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
263 {
264 // walk interfaces
265 if (ifa->ifa_addr == nullptr)
266 {
267 continue;
268 }
269
270 // get only INET interfaces not ipv6
271 if (ifa->ifa_addr->sa_family == AF_INET ||
272 ifa->ifa_addr->sa_family == AF_INET6)
273 {
274 // if loopback, or not running ignore
275 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
276 !(ifa->ifa_flags & IFF_RUNNING))
277 {
278 continue;
279 }
Ratan Gupta3681a502017-06-17 19:20:04 +0530280 intfName = ifa->ifa_name;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500281 AddrInfo info{};
282 char ip[INET6_ADDRSTRLEN] = {0};
283 char subnetMask[INET6_ADDRSTRLEN] = {0};
Ratan Gupta3681a502017-06-17 19:20:04 +0530284
285 if (ifa->ifa_addr->sa_family == AF_INET)
286 {
287
288 inet_ntop(ifa->ifa_addr->sa_family,
289 &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr),
Gunnar Mills57d9c502018-09-14 14:42:34 -0500290 ip, sizeof(ip));
Ratan Gupta3681a502017-06-17 19:20:04 +0530291
Gunnar Mills57d9c502018-09-14 14:42:34 -0500292 inet_ntop(
293 ifa->ifa_addr->sa_family,
294 &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr),
295 subnetMask, sizeof(subnetMask));
Ratan Gupta3681a502017-06-17 19:20:04 +0530296 }
297 else
298 {
299 inet_ntop(ifa->ifa_addr->sa_family,
300 &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr),
Gunnar Mills57d9c502018-09-14 14:42:34 -0500301 ip, sizeof(ip));
Ratan Gupta3681a502017-06-17 19:20:04 +0530302
Gunnar Mills57d9c502018-09-14 14:42:34 -0500303 inet_ntop(
304 ifa->ifa_addr->sa_family,
305 &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr),
306 subnetMask, sizeof(subnetMask));
Ratan Gupta3681a502017-06-17 19:20:04 +0530307 }
308
309 info.addrType = ifa->ifa_addr->sa_family;
310 info.ipaddress = ip;
311 info.prefix = toCidr(info.addrType, std::string(subnetMask));
David Cobbley269501c2018-05-25 15:55:56 -0700312 intfMap[intfName].push_back(info);
Ratan Gupta3681a502017-06-17 19:20:04 +0530313 }
314 }
Ratan Gupta3681a502017-06-17 19:20:04 +0530315 return intfMap;
316}
317
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530318InterfaceList getInterfaces()
319{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500320 InterfaceList interfaces{};
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530321 struct ifaddrs* ifaddr = nullptr;
322
323 // attempt to fill struct with ifaddrs
324 if (getifaddrs(&ifaddr) == -1)
325 {
326 auto error = errno;
327 log<level::ERR>("Error occurred during the getifaddrs call",
328 entry("ERRNO=%d", error));
329 elog<InternalFailure>();
330 }
331
332 AddrPtr ifaddrPtr(ifaddr);
333 ifaddr = nullptr;
334
335 for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
336 {
337 // walk interfaces
William A. Kennington IIIf273d2b2019-03-21 14:38:36 -0700338 // if loopback ignore
339 if (ifa->ifa_flags & IFF_LOOPBACK)
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530340 {
341 continue;
342 }
343 interfaces.emplace(ifa->ifa_name);
344 }
345 return interfaces;
346}
347
Ratan Guptabc886292017-07-25 18:29:57 +0530348void deleteInterface(const std::string& intf)
349{
350 pid_t pid = fork();
Gunnar Mills57d9c502018-09-14 14:42:34 -0500351 int status{};
Ratan Guptabc886292017-07-25 18:29:57 +0530352
353 if (pid == 0)
354 {
355
356 execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr);
357 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500358 log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error),
Ratan Guptabc886292017-07-25 18:29:57 +0530359 entry("INTF=%s", intf.c_str()));
360 elog<InternalFailure>();
361 }
362 else if (pid < 0)
363 {
364 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500365 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
Ratan Guptabc886292017-07-25 18:29:57 +0530366 elog<InternalFailure>();
367 }
368 else if (pid > 0)
369 {
370 while (waitpid(pid, &status, 0) == -1)
371 {
372 if (errno != EINTR)
Gunnar Mills57d9c502018-09-14 14:42:34 -0500373 { /* Error other than EINTR */
Ratan Guptabc886292017-07-25 18:29:57 +0530374 status = -1;
375 break;
376 }
377 }
378
Gunnar Mills57d9c502018-09-14 14:42:34 -0500379 if (status < 0)
Ratan Guptabc886292017-07-25 18:29:57 +0530380 {
381 log<level::ERR>("Unable to delete the interface",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500382 entry("INTF=%s", intf.c_str()),
383 entry("STATUS=%d", status));
Ratan Guptabc886292017-07-25 18:29:57 +0530384 elog<InternalFailure>();
385 }
386 }
387}
388
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700389std::optional<std::string> interfaceToUbootEthAddr(const char* intf)
390{
391 constexpr char ethPrefix[] = "eth";
392 constexpr size_t ethPrefixLen = sizeof(ethPrefix) - 1;
393 if (strncmp(ethPrefix, intf, ethPrefixLen) != 0)
394 {
395 return std::nullopt;
396 }
397 const auto intfSuffix = intf + ethPrefixLen;
398 if (intfSuffix[0] == '\0')
399 {
400 return std::nullopt;
401 }
402 char* end;
403 unsigned long idx = strtoul(intfSuffix, &end, 10);
404 if (end[0] != '\0')
405 {
406 return std::nullopt;
407 }
408 if (idx == 0)
409 {
410 return "ethaddr";
411 }
412 return "eth" + std::to_string(idx) + "addr";
413}
414
Ratan Gupta56187e72017-08-13 09:40:14 +0530415bool getDHCPValue(const std::string& confDir, const std::string& intf)
416{
417 bool dhcp = false;
418 // Get the interface mode value from systemd conf
Gunnar Mills57d9c502018-09-14 14:42:34 -0500419 // using namespace std::string_literals;
Ratan Gupta56187e72017-08-13 09:40:14 +0530420 fs::path confPath = confDir;
421 std::string fileName = systemd::config::networkFilePrefix + intf +
422 systemd::config::networkFileSuffix;
423 confPath /= fileName;
424
Ratan Guptac27170a2017-11-22 15:44:42 +0530425 auto rc = config::ReturnCode::SUCCESS;
426 config::ValueList values;
427 config::Parser parser(confPath.string());
428
429 std::tie(rc, values) = parser.getValues("Network", "DHCP");
430 if (rc != config::ReturnCode::SUCCESS)
Ratan Gupta56187e72017-08-13 09:40:14 +0530431 {
Ratan Guptac27170a2017-11-22 15:44:42 +0530432 log<level::DEBUG>("Unable to get the value for Network[DHCP]",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500433 entry("RC=%d", rc));
Ratan Guptac27170a2017-11-22 15:44:42 +0530434 return dhcp;
Ratan Gupta56187e72017-08-13 09:40:14 +0530435 }
Ratan Guptac27170a2017-11-22 15:44:42 +0530436 // There will be only single value for DHCP key.
437 if (values[0] == "true")
Ratan Gupta56187e72017-08-13 09:40:14 +0530438 {
Ratan Guptac27170a2017-11-22 15:44:42 +0530439 dhcp = true;
Ratan Gupta56187e72017-08-13 09:40:14 +0530440 }
441 return dhcp;
442}
443
Ratan Guptabd303b12017-08-18 17:10:07 +0530444namespace internal
445{
Ratan Gupta56187e72017-08-13 09:40:14 +0530446
Ratan Guptabd303b12017-08-18 17:10:07 +0530447void executeCommandinChildProcess(const char* path, char** args)
448{
449 using namespace std::string_literals;
450 pid_t pid = fork();
Gunnar Mills57d9c502018-09-14 14:42:34 -0500451 int status{};
Ratan Guptabd303b12017-08-18 17:10:07 +0530452
453 if (pid == 0)
454 {
455 execv(path, args);
456 auto error = errno;
457 // create the command from var args.
458 std::string command = path + " "s;
459
Gunnar Mills57d9c502018-09-14 14:42:34 -0500460 for (int i = 0; args[i]; i++)
Ratan Guptabd303b12017-08-18 17:10:07 +0530461 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500462 command += args[i] + " "s;
Ratan Guptabd303b12017-08-18 17:10:07 +0530463 }
464
465 log<level::ERR>("Couldn't exceute the command",
466 entry("ERRNO=%d", error),
467 entry("CMD=%s", command.c_str()));
468 elog<InternalFailure>();
469 }
470 else if (pid < 0)
471 {
472 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500473 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
Ratan Guptabd303b12017-08-18 17:10:07 +0530474 elog<InternalFailure>();
475 }
476 else if (pid > 0)
477 {
478 while (waitpid(pid, &status, 0) == -1)
479 {
480 if (errno != EINTR)
Gunnar Mills57d9c502018-09-14 14:42:34 -0500481 { // Error other than EINTR
Ratan Guptabd303b12017-08-18 17:10:07 +0530482 status = -1;
483 break;
484 }
485 }
486
Gunnar Mills57d9c502018-09-14 14:42:34 -0500487 if (status < 0)
Ratan Guptabd303b12017-08-18 17:10:07 +0530488 {
489 std::string command = path + " "s;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500490 for (int i = 0; args[i]; i++)
Ratan Guptabd303b12017-08-18 17:10:07 +0530491 {
492 command += args[i] + " "s;
493 }
494
495 log<level::ERR>("Unable to execute the command",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500496 entry("CMD=%s", command.c_str()),
497 entry("STATUS=%d", status));
Ratan Guptabd303b12017-08-18 17:10:07 +0530498 elog<InternalFailure>();
499 }
500 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530501}
Gunnar Mills57d9c502018-09-14 14:42:34 -0500502} // namespace internal
Ratan Guptabd303b12017-08-18 17:10:07 +0530503
504namespace mac_address
505{
506
507constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper";
508constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper";
509constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
510constexpr auto propIntf = "org.freedesktop.DBus.Properties";
511constexpr auto methodGet = "Get";
Manojkiran Edacc099a82020-05-11 14:25:16 +0530512constexpr auto configFile = "/usr/share/network/config.json";
Ratan Guptabd303b12017-08-18 17:10:07 +0530513
514using DbusObjectPath = std::string;
515using DbusService = std::string;
516using DbusInterface = std::string;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500517using ObjectTree =
518 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Ratan Guptabd303b12017-08-18 17:10:07 +0530519
520constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager";
521constexpr auto invNetworkIntf =
Gunnar Mills57d9c502018-09-14 14:42:34 -0500522 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
Ratan Guptabd303b12017-08-18 17:10:07 +0530523constexpr auto invRoot = "/xyz/openbmc_project/inventory";
524
Alvin Wang38a63c32019-08-29 22:56:46 +0800525ether_addr getfromInventory(sdbusplus::bus::bus& bus,
526 const std::string& intfName)
Ratan Guptabd303b12017-08-18 17:10:07 +0530527{
Manojkiran Edacc099a82020-05-11 14:25:16 +0530528
529 std::string interfaceName = intfName;
530
531#if SYNC_MAC_FROM_INVENTORY
532 // load the config JSON from the Read Only Path
533 std::ifstream in(configFile);
534 nlohmann::json configJson;
535 in >> configJson;
536 interfaceName = configJson[intfName];
537#endif
538
Ratan Guptabd303b12017-08-18 17:10:07 +0530539 std::vector<DbusInterface> interfaces;
540 interfaces.emplace_back(invNetworkIntf);
541
542 auto depth = 0;
543
Gunnar Mills57d9c502018-09-14 14:42:34 -0500544 auto mapperCall =
545 bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree");
Ratan Guptabd303b12017-08-18 17:10:07 +0530546
547 mapperCall.append(invRoot, depth, interfaces);
548
549 auto mapperReply = bus.call(mapperCall);
550 if (mapperReply.is_method_error())
551 {
552 log<level::ERR>("Error in mapper call");
553 elog<InternalFailure>();
554 }
555
556 ObjectTree objectTree;
557 mapperReply.read(objectTree);
558
559 if (objectTree.empty())
560 {
561 log<level::ERR>("No Object has implemented the interface",
562 entry("INTERFACE=%s", invNetworkIntf));
563 elog<InternalFailure>();
564 }
565
Alvin Wang38a63c32019-08-29 22:56:46 +0800566 DbusObjectPath objPath;
567 DbusService service;
Ratan Guptabd303b12017-08-18 17:10:07 +0530568
Alvin Wang38a63c32019-08-29 22:56:46 +0800569 if (1 == objectTree.size())
570 {
571 objPath = objectTree.begin()->first;
572 service = objectTree.begin()->second.begin()->first;
573 }
574 else
575 {
576 // If there are more than 2 objects, object path must contain the
577 // interface name
578 for (auto const& object : objectTree)
579 {
Manojkiran Edacc099a82020-05-11 14:25:16 +0530580 log<level::INFO>("interface",
581 entry("INT=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800582 log<level::INFO>("object", entry("OBJ=%s", object.first.c_str()));
Manojkiran Edacc099a82020-05-11 14:25:16 +0530583
584 if (std::string::npos != object.first.find(interfaceName.c_str()))
Alvin Wang38a63c32019-08-29 22:56:46 +0800585 {
586 objPath = object.first;
587 service = object.second.begin()->first;
588 break;
589 }
590 }
591
592 if (objPath.empty())
593 {
594 log<level::ERR>("Can't find the object for the interface",
Manojkiran Edacc099a82020-05-11 14:25:16 +0530595 entry("intfName=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800596 elog<InternalFailure>();
597 }
598 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530599
Gunnar Mills57d9c502018-09-14 14:42:34 -0500600 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
601 propIntf, methodGet);
Ratan Guptabd303b12017-08-18 17:10:07 +0530602
603 method.append(invNetworkIntf, "MACAddress");
604
605 auto reply = bus.call(method);
606 if (reply.is_method_error())
607 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500608 log<level::ERR>("Failed to get MACAddress",
Ratan Guptabd303b12017-08-18 17:10:07 +0530609 entry("PATH=%s", objPath.c_str()),
610 entry("INTERFACE=%s", invNetworkIntf));
611 elog<InternalFailure>();
612 }
613
William A. Kennington III1137a972019-04-20 20:49:58 -0700614 std::variant<std::string> value;
Ratan Guptabd303b12017-08-18 17:10:07 +0530615 reply.read(value);
William A. Kennington III1137a972019-04-20 20:49:58 -0700616 return fromString(std::get<std::string>(value));
617}
618
619ether_addr fromString(const char* str)
620{
621 struct ether_addr* mac = ether_aton(str);
622 if (mac == nullptr)
623 {
624 throw std::runtime_error("Invalid mac address string");
625 }
626 return *mac;
Ratan Guptabd303b12017-08-18 17:10:07 +0530627}
628
William A. Kennington III6ca08d82019-04-20 16:04:18 -0700629std::string toString(const ether_addr& mac)
William A. Kennington IIIa14879e2019-02-01 21:43:11 -0800630{
Karthick Sundarrajandbd328d2019-11-20 15:19:08 -0800631 char buf[18] = {0};
632 snprintf(buf, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac.ether_addr_octet[0],
633 mac.ether_addr_octet[1], mac.ether_addr_octet[2],
634 mac.ether_addr_octet[3], mac.ether_addr_octet[4],
635 mac.ether_addr_octet[5]);
636 return buf;
William A. Kennington IIId27410f2019-01-30 17:15:43 -0800637}
638
William A. Kennington III1137a972019-04-20 20:49:58 -0700639bool isEmpty(const ether_addr& mac)
640{
William A. Kennington III12beaad2020-06-13 19:30:41 -0700641 return stdplus::raw::equal(mac, ether_addr{});
William A. Kennington III1137a972019-04-20 20:49:58 -0700642}
643
644bool isMulticast(const ether_addr& mac)
645{
646 return mac.ether_addr_octet[0] & 0b1;
647}
648
649bool isUnicast(const ether_addr& mac)
650{
651 return !isEmpty(mac) && !isMulticast(mac);
652}
653
654bool isLocalAdmin(const ether_addr& mac)
655{
656 return mac.ether_addr_octet[0] & 0b10;
657}
658
Gunnar Mills57d9c502018-09-14 14:42:34 -0500659} // namespace mac_address
660} // namespace network
661} // namespace phosphor