blob: 111c090810690863706d038738bc2633295b207a [file] [log] [blame]
William A. Kennington III324d2602022-08-18 18:32:56 -07001#include "config.h"
2
Ratan Gupta3681a502017-06-17 19:20:04 +05303#include "util.hpp"
Ratan Gupta11cef802017-05-29 08:41:48 +05304
Patrick Venture189d44e2018-07-09 12:30:59 -07005#include "config_parser.hpp"
6#include "types.hpp"
Ratan Gupta8804feb2017-05-25 10:49:57 +05307
Ratan Gupta3681a502017-06-17 19:20:04 +05308#include <arpa/inet.h>
William A. Kennington III1c776022022-01-05 14:12:16 -08009#include <fmt/compile.h>
10#include <fmt/format.h>
Ratan Guptabc886292017-07-25 18:29:57 +053011#include <sys/wait.h>
Ratan Gupta3681a502017-06-17 19:20:04 +053012
Ratan Gupta8804feb2017-05-25 10:49:57 +053013#include <algorithm>
Lei YU307554e2021-03-18 14:56:50 +080014#include <cctype>
William A. Kennington III69f45542022-09-24 23:28:14 -070015#include <charconv>
Manojkiran Edacc099a82020-05-11 14:25:16 +053016#include <fstream>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070017#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +053018#include <nlohmann/json.hpp>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070019#endif
Patrick Venture189d44e2018-07-09 12:30:59 -070020#include <phosphor-logging/elog-errors.hpp>
21#include <phosphor-logging/log.hpp>
William A. Kennington III5058f572019-01-30 17:18:14 -080022#include <stdexcept>
William A. Kennington III12beaad2020-06-13 19:30:41 -070023#include <stdplus/raw.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070024#include <string>
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -070025#include <string_view>
William A. Kennington III1137a972019-04-20 20:49:58 -070026#include <variant>
Patrick Venture189d44e2018-07-09 12:30:59 -070027#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053028
29namespace phosphor
30{
31namespace network
32{
Ratan Guptabc886292017-07-25 18:29:57 +053033
William A. Kennington III69f45542022-09-24 23:28:14 -070034using std::literals::string_view_literals::operator""sv;
Ratan Gupta8804feb2017-05-25 10:49:57 +053035using namespace phosphor::logging;
Ratan Gupta11cef802017-05-29 08:41:48 +053036using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta8804feb2017-05-25 10:49:57 +053037
Lei YU3894ce72021-03-18 14:53:42 +080038namespace internal
39{
40
William A. Kennington III69f45542022-09-24 23:28:14 -070041void executeCommandinChildProcess(stdplus::const_zstring path, char** args)
Lei YU3894ce72021-03-18 14:53:42 +080042{
43 using namespace std::string_literals;
44 pid_t pid = fork();
Lei YU3894ce72021-03-18 14:53:42 +080045
46 if (pid == 0)
47 {
William A. Kennington III69f45542022-09-24 23:28:14 -070048 execv(path.c_str(), args);
49 exit(255);
Lei YU3894ce72021-03-18 14:53:42 +080050 }
51 else if (pid < 0)
52 {
53 auto error = errno;
54 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
55 elog<InternalFailure>();
56 }
57 else if (pid > 0)
58 {
William A. Kennington III69f45542022-09-24 23:28:14 -070059 int status;
Lei YU3894ce72021-03-18 14:53:42 +080060 while (waitpid(pid, &status, 0) == -1)
61 {
62 if (errno != EINTR)
William A. Kennington III69f45542022-09-24 23:28:14 -070063 {
Lei YU3894ce72021-03-18 14:53:42 +080064 status = -1;
65 break;
66 }
67 }
68
69 if (status < 0)
70 {
William A. Kennington III69f45542022-09-24 23:28:14 -070071 fmt::memory_buffer buf;
72 fmt::format_to(fmt::appender(buf), "`{}`", path);
73 for (size_t i = 0; args[i] != nullptr; ++i)
Lei YU3894ce72021-03-18 14:53:42 +080074 {
William A. Kennington III69f45542022-09-24 23:28:14 -070075 fmt::format_to(fmt::appender(buf), " `{}`", args[i]);
Lei YU3894ce72021-03-18 14:53:42 +080076 }
William A. Kennington III69f45542022-09-24 23:28:14 -070077 buf.push_back('\0');
Lei YU3894ce72021-03-18 14:53:42 +080078 log<level::ERR>("Unable to execute the command",
William A. Kennington III69f45542022-09-24 23:28:14 -070079 entry("CMD=%s", buf.data()),
Lei YU3894ce72021-03-18 14:53:42 +080080 entry("STATUS=%d", status));
81 elog<InternalFailure>();
82 }
83 }
84}
85
Lei YU307554e2021-03-18 14:56:50 +080086/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070087std::string_view getIgnoredInterfacesEnv()
Lei YU307554e2021-03-18 14:56:50 +080088{
89 auto r = std::getenv("IGNORED_INTERFACES");
90 if (r == nullptr)
91 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070092 return "";
Lei YU307554e2021-03-18 14:56:50 +080093 }
94 return r;
95}
96
97/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -070098std::unordered_set<std::string_view>
99 parseInterfaces(std::string_view interfaces)
Lei YU307554e2021-03-18 14:56:50 +0800100{
William A. Kennington III95530ec2022-08-19 01:44:39 -0700101 std::unordered_set<std::string_view> result;
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700102 while (true)
Lei YU307554e2021-03-18 14:56:50 +0800103 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700104 auto sep = interfaces.find(',');
105 auto interface = interfaces.substr(0, sep);
106 while (!interface.empty() && std::isspace(interface.front()))
Lei YU307554e2021-03-18 14:56:50 +0800107 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700108 interface.remove_prefix(1);
Lei YU307554e2021-03-18 14:56:50 +0800109 }
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700110 while (!interface.empty() && std::isspace(interface.back()))
Lei YU307554e2021-03-18 14:56:50 +0800111 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700112 interface.remove_suffix(1);
Lei YU307554e2021-03-18 14:56:50 +0800113 }
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700114 if (!interface.empty())
115 {
116 result.insert(interface);
117 }
118 if (sep == interfaces.npos)
119 {
120 break;
121 }
122 interfaces = interfaces.substr(sep + 1);
Lei YU307554e2021-03-18 14:56:50 +0800123 }
124 return result;
125}
126
127/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700128const std::unordered_set<std::string_view>& getIgnoredInterfaces()
Lei YU307554e2021-03-18 14:56:50 +0800129{
130 static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv());
131 return ignoredInterfaces;
132}
133
Lei YU3894ce72021-03-18 14:53:42 +0800134} // namespace internal
Ratan Gupta8804feb2017-05-25 10:49:57 +0530135
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700136constexpr auto familyVisit(auto&& visitor, int family)
137{
138 if (family == AF_INET)
139 {
140 return visitor.template operator()<AF_INET>();
141 }
142 else if (family == AF_INET6)
143 {
144 return visitor.template operator()<AF_INET6>();
145 }
146 throw std::invalid_argument("Invalid addr family");
147}
148
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700149template <int family>
150typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf)
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800151{
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700152 return stdplus::raw::copyFromStrict<typename FamilyTraits<family>::addr>(
153 buf);
154}
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800155
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700156InAddrAny addrFromBuf(int family, std::string_view buf)
157{
158 return familyVisit(
159 [=]<int f>() -> InAddrAny { return addrFromBuf<f>(buf); }, family);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800160}
161
William A. Kennington III9f228302022-10-07 14:02:49 -0700162bool isValidIP(int family, stdplus::const_zstring address) noexcept
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500163{
164 unsigned char buf[sizeof(struct in6_addr)];
William A. Kennington III9f228302022-10-07 14:02:49 -0700165 return inet_pton(family, address.c_str(), buf) > 0;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500166}
167
William A. Kennington IIIff12acb2022-10-07 19:06:56 -0700168bool isValidIP(stdplus::const_zstring address) noexcept
169{
170 return isValidIP(AF_INET, address) || isValidIP(AF_INET6, address);
171}
172
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700173bool isValidPrefix(int family, uint8_t prefix)
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500174{
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700175 return familyVisit(
176 [=]<int f>() noexcept { return isValidPrefix<f>(prefix); }, family);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530177}
178
William A. Kennington III69f45542022-09-24 23:28:14 -0700179void deleteInterface(stdplus::const_zstring intf)
Ratan Guptabc886292017-07-25 18:29:57 +0530180{
181 pid_t pid = fork();
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182 int status{};
Ratan Guptabc886292017-07-25 18:29:57 +0530183
184 if (pid == 0)
185 {
186
187 execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr);
188 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500189 log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error),
Ratan Guptabc886292017-07-25 18:29:57 +0530190 entry("INTF=%s", intf.c_str()));
191 elog<InternalFailure>();
192 }
193 else if (pid < 0)
194 {
195 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500196 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
Ratan Guptabc886292017-07-25 18:29:57 +0530197 elog<InternalFailure>();
198 }
199 else if (pid > 0)
200 {
201 while (waitpid(pid, &status, 0) == -1)
202 {
203 if (errno != EINTR)
Gunnar Mills57d9c502018-09-14 14:42:34 -0500204 { /* Error other than EINTR */
Ratan Guptabc886292017-07-25 18:29:57 +0530205 status = -1;
206 break;
207 }
208 }
209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 if (status < 0)
Ratan Guptabc886292017-07-25 18:29:57 +0530211 {
212 log<level::ERR>("Unable to delete the interface",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500213 entry("INTF=%s", intf.c_str()),
214 entry("STATUS=%d", status));
Ratan Guptabc886292017-07-25 18:29:57 +0530215 elog<InternalFailure>();
216 }
217 }
218}
219
William A. Kennington III69f45542022-09-24 23:28:14 -0700220std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700221{
William A. Kennington III69f45542022-09-24 23:28:14 -0700222 constexpr auto pfx = "eth"sv;
223 if (!intf.starts_with(pfx))
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700224 {
225 return std::nullopt;
226 }
William A. Kennington III69f45542022-09-24 23:28:14 -0700227 intf.remove_prefix(pfx.size());
228 auto last = intf.data() + intf.size();
229 unsigned long idx;
230 auto res = std::from_chars(intf.data(), last, idx);
231 if (res.ec != std::errc() || res.ptr != last)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700232 {
233 return std::nullopt;
234 }
235 if (idx == 0)
236 {
237 return "ethaddr";
238 }
William A. Kennington III69f45542022-09-24 23:28:14 -0700239 return fmt::format(FMT_COMPILE("eth{}addr"), idx);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700240}
241
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700242static std::optional<DHCPVal> systemdParseDHCP(std::string_view str)
243{
244 if (config::icaseeq(str, "ipv4"))
245 {
246 return DHCPVal{.v4 = true, .v6 = false};
247 }
248 if (config::icaseeq(str, "ipv6"))
249 {
250 return DHCPVal{.v4 = false, .v6 = true};
251 }
252 if (auto b = config::parseBool(str); b)
253 {
254 return DHCPVal{.v4 = *b, .v6 = *b};
255 }
256 return std::nullopt;
257}
258
259inline auto systemdParseLast(const config::Parser& config,
260 std::string_view section, std::string_view key,
261 auto&& fun)
262{
263 if (auto str = config.map.getLastValueString(section, key); str == nullptr)
264 {
265 auto err = fmt::format("Unable to get the value of {}[{}] from {}",
266 section, key, config.getFilename().native());
267 log<level::NOTICE>(err.c_str(),
268 entry("FILE=%s", config.getFilename().c_str()));
269 }
270 else if (auto val = fun(*str); !val)
271 {
272 auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section,
273 key, config.getFilename().native(), *str);
274 log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()),
275 entry("FILE=%s", config.getFilename().c_str()));
276 }
277 else
278 {
279 return val;
280 }
281 return decltype(fun(std::string_view{}))(std::nullopt);
282}
283
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700284bool getIPv6AcceptRA(const config::Parser& config)
Ratan Gupta56187e72017-08-13 09:40:14 +0530285{
William A. Kennington III324d2602022-08-18 18:32:56 -0700286#ifdef ENABLE_IPV6_ACCEPT_RA
287 constexpr bool def = true;
288#else
289 constexpr bool def = false;
290#endif
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700291 return systemdParseLast(config, "Network", "IPv6AcceptRA",
292 config::parseBool)
293 .value_or(def);
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700294}
295
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700296DHCPVal getDHCPValue(const config::Parser& config)
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700297{
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700298 return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP)
299 .value_or(DHCPVal{.v4 = true, .v6 = true});
300}
William A. Kennington III324d2602022-08-18 18:32:56 -0700301
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700302bool getDHCPProp(const config::Parser& config, std::string_view key)
303{
304 return systemdParseLast(config, "DHCP", key, config::parseBool)
305 .value_or(true);
Ratan Gupta56187e72017-08-13 09:40:14 +0530306}
307
Ratan Guptabd303b12017-08-18 17:10:07 +0530308namespace mac_address
309{
310
311constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper";
312constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper";
313constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
314constexpr auto propIntf = "org.freedesktop.DBus.Properties";
315constexpr auto methodGet = "Get";
Manojkiran Edacc099a82020-05-11 14:25:16 +0530316constexpr auto configFile = "/usr/share/network/config.json";
Ratan Guptabd303b12017-08-18 17:10:07 +0530317
318using DbusObjectPath = std::string;
319using DbusService = std::string;
320using DbusInterface = std::string;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500321using ObjectTree =
322 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Ratan Guptabd303b12017-08-18 17:10:07 +0530323
324constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager";
325constexpr auto invNetworkIntf =
Gunnar Mills57d9c502018-09-14 14:42:34 -0500326 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
Ratan Guptabd303b12017-08-18 17:10:07 +0530327constexpr auto invRoot = "/xyz/openbmc_project/inventory";
328
Patrick Williamsc38b0712022-07-22 19:26:54 -0500329ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName)
Ratan Guptabd303b12017-08-18 17:10:07 +0530330{
Manojkiran Edacc099a82020-05-11 14:25:16 +0530331
332 std::string interfaceName = intfName;
333
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700334#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530335 // load the config JSON from the Read Only Path
336 std::ifstream in(configFile);
337 nlohmann::json configJson;
338 in >> configJson;
339 interfaceName = configJson[intfName];
340#endif
341
Ratan Guptabd303b12017-08-18 17:10:07 +0530342 std::vector<DbusInterface> interfaces;
343 interfaces.emplace_back(invNetworkIntf);
344
345 auto depth = 0;
346
Gunnar Mills57d9c502018-09-14 14:42:34 -0500347 auto mapperCall =
348 bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree");
Ratan Guptabd303b12017-08-18 17:10:07 +0530349
350 mapperCall.append(invRoot, depth, interfaces);
351
352 auto mapperReply = bus.call(mapperCall);
353 if (mapperReply.is_method_error())
354 {
355 log<level::ERR>("Error in mapper call");
356 elog<InternalFailure>();
357 }
358
359 ObjectTree objectTree;
360 mapperReply.read(objectTree);
361
362 if (objectTree.empty())
363 {
364 log<level::ERR>("No Object has implemented the interface",
365 entry("INTERFACE=%s", invNetworkIntf));
366 elog<InternalFailure>();
367 }
368
Alvin Wang38a63c32019-08-29 22:56:46 +0800369 DbusObjectPath objPath;
370 DbusService service;
Ratan Guptabd303b12017-08-18 17:10:07 +0530371
Alvin Wang38a63c32019-08-29 22:56:46 +0800372 if (1 == objectTree.size())
373 {
374 objPath = objectTree.begin()->first;
375 service = objectTree.begin()->second.begin()->first;
376 }
377 else
378 {
379 // If there are more than 2 objects, object path must contain the
380 // interface name
381 for (auto const& object : objectTree)
382 {
Manojkiran Edacc099a82020-05-11 14:25:16 +0530383 log<level::INFO>("interface",
384 entry("INT=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800385 log<level::INFO>("object", entry("OBJ=%s", object.first.c_str()));
Manojkiran Edacc099a82020-05-11 14:25:16 +0530386
387 if (std::string::npos != object.first.find(interfaceName.c_str()))
Alvin Wang38a63c32019-08-29 22:56:46 +0800388 {
389 objPath = object.first;
390 service = object.second.begin()->first;
391 break;
392 }
393 }
394
395 if (objPath.empty())
396 {
397 log<level::ERR>("Can't find the object for the interface",
Manojkiran Edacc099a82020-05-11 14:25:16 +0530398 entry("intfName=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800399 elog<InternalFailure>();
400 }
401 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530402
Gunnar Mills57d9c502018-09-14 14:42:34 -0500403 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
404 propIntf, methodGet);
Ratan Guptabd303b12017-08-18 17:10:07 +0530405
406 method.append(invNetworkIntf, "MACAddress");
407
408 auto reply = bus.call(method);
409 if (reply.is_method_error())
410 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500411 log<level::ERR>("Failed to get MACAddress",
Ratan Guptabd303b12017-08-18 17:10:07 +0530412 entry("PATH=%s", objPath.c_str()),
413 entry("INTERFACE=%s", invNetworkIntf));
414 elog<InternalFailure>();
415 }
416
William A. Kennington III1137a972019-04-20 20:49:58 -0700417 std::variant<std::string> value;
Ratan Guptabd303b12017-08-18 17:10:07 +0530418 reply.read(value);
William A. Kennington III1137a972019-04-20 20:49:58 -0700419 return fromString(std::get<std::string>(value));
420}
421
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700422static uint8_t decodeHex(std::string_view str)
William A. Kennington III1137a972019-04-20 20:49:58 -0700423{
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700424 uint8_t ret;
425 auto res = std::from_chars(str.begin(), str.end(), ret, 16);
426 if (res.ptr != str.end() || res.ec != std::errc())
427 {
428 throw std::invalid_argument("Not hex");
429 }
430 return ret;
431}
Potin Laida0b1d42021-12-26 20:08:20 +0800432
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700433ether_addr fromString(std::string_view str)
434{
435 ether_addr ret;
William A. Kennington III69f45542022-09-24 23:28:14 -0700436 if (str.size() == 12 && str.find(":") == str.npos)
William A. Kennington III1137a972019-04-20 20:49:58 -0700437 {
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700438 for (size_t i = 0; i < 6; ++i)
439 {
440 ret.ether_addr_octet[i] = decodeHex(str.substr(i * 2, 2));
441 }
William A. Kennington III1137a972019-04-20 20:49:58 -0700442 }
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700443 else
Potin Laida0b1d42021-12-26 20:08:20 +0800444 {
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700445 for (size_t i = 0; i < 5; ++i)
446 {
447 auto loc = str.find(":");
448 ret.ether_addr_octet[i] = decodeHex(str.substr(0, loc));
449 str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
450 if (str.empty())
451 {
452 throw std::invalid_argument("Missing mac data");
453 }
454 }
455 ret.ether_addr_octet[5] = decodeHex(str);
Potin Laida0b1d42021-12-26 20:08:20 +0800456 }
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700457 return ret;
Ratan Guptabd303b12017-08-18 17:10:07 +0530458}
459
William A. Kennington III1137a972019-04-20 20:49:58 -0700460bool isEmpty(const ether_addr& mac)
461{
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700462 return mac == ether_addr{};
William A. Kennington III1137a972019-04-20 20:49:58 -0700463}
464
465bool isMulticast(const ether_addr& mac)
466{
467 return mac.ether_addr_octet[0] & 0b1;
468}
469
470bool isUnicast(const ether_addr& mac)
471{
472 return !isEmpty(mac) && !isMulticast(mac);
473}
474
Gunnar Mills57d9c502018-09-14 14:42:34 -0500475} // namespace mac_address
476} // namespace network
477} // namespace phosphor