blob: 0c5dbff239296893fa0e5e490b5a01db82086fac [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
Johnathan Mantey817012a2020-01-30 15:07:39 -0800415EthernetInterfaceIntf::DHCPConf getDHCPValue(const std::string& confDir,
416 const std::string& intf)
Ratan Gupta56187e72017-08-13 09:40:14 +0530417{
Johnathan Mantey817012a2020-01-30 15:07:39 -0800418 EthernetInterfaceIntf::DHCPConf dhcp =
419 EthernetInterfaceIntf::DHCPConf::none;
Ratan Gupta56187e72017-08-13 09:40:14 +0530420 // Get the interface mode value from systemd conf
Gunnar Mills57d9c502018-09-14 14:42:34 -0500421 // using namespace std::string_literals;
Ratan Gupta56187e72017-08-13 09:40:14 +0530422 fs::path confPath = confDir;
423 std::string fileName = systemd::config::networkFilePrefix + intf +
424 systemd::config::networkFileSuffix;
425 confPath /= fileName;
426
Ratan Guptac27170a2017-11-22 15:44:42 +0530427 auto rc = config::ReturnCode::SUCCESS;
428 config::ValueList values;
429 config::Parser parser(confPath.string());
430
431 std::tie(rc, values) = parser.getValues("Network", "DHCP");
432 if (rc != config::ReturnCode::SUCCESS)
Ratan Gupta56187e72017-08-13 09:40:14 +0530433 {
Ratan Guptac27170a2017-11-22 15:44:42 +0530434 log<level::DEBUG>("Unable to get the value for Network[DHCP]",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500435 entry("RC=%d", rc));
Ratan Guptac27170a2017-11-22 15:44:42 +0530436 return dhcp;
Ratan Gupta56187e72017-08-13 09:40:14 +0530437 }
Ratan Guptac27170a2017-11-22 15:44:42 +0530438 // There will be only single value for DHCP key.
439 if (values[0] == "true")
Ratan Gupta56187e72017-08-13 09:40:14 +0530440 {
Johnathan Mantey817012a2020-01-30 15:07:39 -0800441 dhcp = EthernetInterfaceIntf::DHCPConf::both;
442 }
443 else if (values[0] == "ipv4")
444 {
445 dhcp = EthernetInterfaceIntf::DHCPConf::v4;
446 }
447 else if (values[0] == "ipv6")
448 {
449 dhcp = EthernetInterfaceIntf::DHCPConf::v6;
Ratan Gupta56187e72017-08-13 09:40:14 +0530450 }
451 return dhcp;
452}
453
Ratan Guptabd303b12017-08-18 17:10:07 +0530454namespace internal
455{
Ratan Gupta56187e72017-08-13 09:40:14 +0530456
Ratan Guptabd303b12017-08-18 17:10:07 +0530457void executeCommandinChildProcess(const char* path, char** args)
458{
459 using namespace std::string_literals;
460 pid_t pid = fork();
Gunnar Mills57d9c502018-09-14 14:42:34 -0500461 int status{};
Ratan Guptabd303b12017-08-18 17:10:07 +0530462
463 if (pid == 0)
464 {
465 execv(path, args);
466 auto error = errno;
467 // create the command from var args.
468 std::string command = path + " "s;
469
Gunnar Mills57d9c502018-09-14 14:42:34 -0500470 for (int i = 0; args[i]; i++)
Ratan Guptabd303b12017-08-18 17:10:07 +0530471 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500472 command += args[i] + " "s;
Ratan Guptabd303b12017-08-18 17:10:07 +0530473 }
474
475 log<level::ERR>("Couldn't exceute the command",
476 entry("ERRNO=%d", error),
477 entry("CMD=%s", command.c_str()));
478 elog<InternalFailure>();
479 }
480 else if (pid < 0)
481 {
482 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500483 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
Ratan Guptabd303b12017-08-18 17:10:07 +0530484 elog<InternalFailure>();
485 }
486 else if (pid > 0)
487 {
488 while (waitpid(pid, &status, 0) == -1)
489 {
490 if (errno != EINTR)
Gunnar Mills57d9c502018-09-14 14:42:34 -0500491 { // Error other than EINTR
Ratan Guptabd303b12017-08-18 17:10:07 +0530492 status = -1;
493 break;
494 }
495 }
496
Gunnar Mills57d9c502018-09-14 14:42:34 -0500497 if (status < 0)
Ratan Guptabd303b12017-08-18 17:10:07 +0530498 {
499 std::string command = path + " "s;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500500 for (int i = 0; args[i]; i++)
Ratan Guptabd303b12017-08-18 17:10:07 +0530501 {
502 command += args[i] + " "s;
503 }
504
505 log<level::ERR>("Unable to execute the command",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500506 entry("CMD=%s", command.c_str()),
507 entry("STATUS=%d", status));
Ratan Guptabd303b12017-08-18 17:10:07 +0530508 elog<InternalFailure>();
509 }
510 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530511}
Gunnar Mills57d9c502018-09-14 14:42:34 -0500512} // namespace internal
Ratan Guptabd303b12017-08-18 17:10:07 +0530513
514namespace mac_address
515{
516
517constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper";
518constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper";
519constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
520constexpr auto propIntf = "org.freedesktop.DBus.Properties";
521constexpr auto methodGet = "Get";
Manojkiran Edacc099a82020-05-11 14:25:16 +0530522constexpr auto configFile = "/usr/share/network/config.json";
Ratan Guptabd303b12017-08-18 17:10:07 +0530523
524using DbusObjectPath = std::string;
525using DbusService = std::string;
526using DbusInterface = std::string;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500527using ObjectTree =
528 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Ratan Guptabd303b12017-08-18 17:10:07 +0530529
530constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager";
531constexpr auto invNetworkIntf =
Gunnar Mills57d9c502018-09-14 14:42:34 -0500532 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
Ratan Guptabd303b12017-08-18 17:10:07 +0530533constexpr auto invRoot = "/xyz/openbmc_project/inventory";
534
Alvin Wang38a63c32019-08-29 22:56:46 +0800535ether_addr getfromInventory(sdbusplus::bus::bus& bus,
536 const std::string& intfName)
Ratan Guptabd303b12017-08-18 17:10:07 +0530537{
Manojkiran Edacc099a82020-05-11 14:25:16 +0530538
539 std::string interfaceName = intfName;
540
541#if SYNC_MAC_FROM_INVENTORY
542 // load the config JSON from the Read Only Path
543 std::ifstream in(configFile);
544 nlohmann::json configJson;
545 in >> configJson;
546 interfaceName = configJson[intfName];
547#endif
548
Ratan Guptabd303b12017-08-18 17:10:07 +0530549 std::vector<DbusInterface> interfaces;
550 interfaces.emplace_back(invNetworkIntf);
551
552 auto depth = 0;
553
Gunnar Mills57d9c502018-09-14 14:42:34 -0500554 auto mapperCall =
555 bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree");
Ratan Guptabd303b12017-08-18 17:10:07 +0530556
557 mapperCall.append(invRoot, depth, interfaces);
558
559 auto mapperReply = bus.call(mapperCall);
560 if (mapperReply.is_method_error())
561 {
562 log<level::ERR>("Error in mapper call");
563 elog<InternalFailure>();
564 }
565
566 ObjectTree objectTree;
567 mapperReply.read(objectTree);
568
569 if (objectTree.empty())
570 {
571 log<level::ERR>("No Object has implemented the interface",
572 entry("INTERFACE=%s", invNetworkIntf));
573 elog<InternalFailure>();
574 }
575
Alvin Wang38a63c32019-08-29 22:56:46 +0800576 DbusObjectPath objPath;
577 DbusService service;
Ratan Guptabd303b12017-08-18 17:10:07 +0530578
Alvin Wang38a63c32019-08-29 22:56:46 +0800579 if (1 == objectTree.size())
580 {
581 objPath = objectTree.begin()->first;
582 service = objectTree.begin()->second.begin()->first;
583 }
584 else
585 {
586 // If there are more than 2 objects, object path must contain the
587 // interface name
588 for (auto const& object : objectTree)
589 {
Manojkiran Edacc099a82020-05-11 14:25:16 +0530590 log<level::INFO>("interface",
591 entry("INT=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800592 log<level::INFO>("object", entry("OBJ=%s", object.first.c_str()));
Manojkiran Edacc099a82020-05-11 14:25:16 +0530593
594 if (std::string::npos != object.first.find(interfaceName.c_str()))
Alvin Wang38a63c32019-08-29 22:56:46 +0800595 {
596 objPath = object.first;
597 service = object.second.begin()->first;
598 break;
599 }
600 }
601
602 if (objPath.empty())
603 {
604 log<level::ERR>("Can't find the object for the interface",
Manojkiran Edacc099a82020-05-11 14:25:16 +0530605 entry("intfName=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800606 elog<InternalFailure>();
607 }
608 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530609
Gunnar Mills57d9c502018-09-14 14:42:34 -0500610 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
611 propIntf, methodGet);
Ratan Guptabd303b12017-08-18 17:10:07 +0530612
613 method.append(invNetworkIntf, "MACAddress");
614
615 auto reply = bus.call(method);
616 if (reply.is_method_error())
617 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500618 log<level::ERR>("Failed to get MACAddress",
Ratan Guptabd303b12017-08-18 17:10:07 +0530619 entry("PATH=%s", objPath.c_str()),
620 entry("INTERFACE=%s", invNetworkIntf));
621 elog<InternalFailure>();
622 }
623
William A. Kennington III1137a972019-04-20 20:49:58 -0700624 std::variant<std::string> value;
Ratan Guptabd303b12017-08-18 17:10:07 +0530625 reply.read(value);
William A. Kennington III1137a972019-04-20 20:49:58 -0700626 return fromString(std::get<std::string>(value));
627}
628
629ether_addr fromString(const char* str)
630{
631 struct ether_addr* mac = ether_aton(str);
632 if (mac == nullptr)
633 {
Asmitha Karunanithi86f659e2021-01-05 00:16:03 -0600634 throw std::invalid_argument("Invalid MAC Address");
William A. Kennington III1137a972019-04-20 20:49:58 -0700635 }
636 return *mac;
Ratan Guptabd303b12017-08-18 17:10:07 +0530637}
638
William A. Kennington III6ca08d82019-04-20 16:04:18 -0700639std::string toString(const ether_addr& mac)
William A. Kennington IIIa14879e2019-02-01 21:43:11 -0800640{
Karthick Sundarrajandbd328d2019-11-20 15:19:08 -0800641 char buf[18] = {0};
642 snprintf(buf, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac.ether_addr_octet[0],
643 mac.ether_addr_octet[1], mac.ether_addr_octet[2],
644 mac.ether_addr_octet[3], mac.ether_addr_octet[4],
645 mac.ether_addr_octet[5]);
646 return buf;
William A. Kennington IIId27410f2019-01-30 17:15:43 -0800647}
648
William A. Kennington III1137a972019-04-20 20:49:58 -0700649bool isEmpty(const ether_addr& mac)
650{
William A. Kennington III12beaad2020-06-13 19:30:41 -0700651 return stdplus::raw::equal(mac, ether_addr{});
William A. Kennington III1137a972019-04-20 20:49:58 -0700652}
653
654bool isMulticast(const ether_addr& mac)
655{
656 return mac.ether_addr_octet[0] & 0b1;
657}
658
659bool isUnicast(const ether_addr& mac)
660{
661 return !isEmpty(mac) && !isMulticast(mac);
662}
663
Gunnar Mills57d9c502018-09-14 14:42:34 -0500664} // namespace mac_address
665} // namespace network
666} // namespace phosphor