blob: 1feab813db096590c2c0d5bf90d6c4b25d719972 [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
Lei YU307554e2021-03-18 14:56:50 +080013#include <cctype>
William A. Kennington III69f45542022-09-24 23:28:14 -070014#include <charconv>
Manojkiran Edacc099a82020-05-11 14:25:16 +053015#include <fstream>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070016#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +053017#include <nlohmann/json.hpp>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070018#endif
Patrick Venture189d44e2018-07-09 12:30:59 -070019#include <phosphor-logging/elog-errors.hpp>
20#include <phosphor-logging/log.hpp>
William A. Kennington III5058f572019-01-30 17:18:14 -080021#include <stdexcept>
William A. Kennington III12beaad2020-06-13 19:30:41 -070022#include <stdplus/raw.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070023#include <string>
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -070024#include <string_view>
William A. Kennington III1137a972019-04-20 20:49:58 -070025#include <variant>
Patrick Venture189d44e2018-07-09 12:30:59 -070026#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053027
28namespace phosphor
29{
30namespace network
31{
Ratan Guptabc886292017-07-25 18:29:57 +053032
William A. Kennington III69f45542022-09-24 23:28:14 -070033using std::literals::string_view_literals::operator""sv;
Ratan Gupta8804feb2017-05-25 10:49:57 +053034using namespace phosphor::logging;
Ratan Gupta11cef802017-05-29 08:41:48 +053035using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta8804feb2017-05-25 10:49:57 +053036
Lei YU3894ce72021-03-18 14:53:42 +080037namespace internal
38{
39
William A. Kennington III69f45542022-09-24 23:28:14 -070040void executeCommandinChildProcess(stdplus::const_zstring path, char** args)
Lei YU3894ce72021-03-18 14:53:42 +080041{
42 using namespace std::string_literals;
43 pid_t pid = fork();
Lei YU3894ce72021-03-18 14:53:42 +080044
45 if (pid == 0)
46 {
William A. Kennington III69f45542022-09-24 23:28:14 -070047 execv(path.c_str(), args);
48 exit(255);
Lei YU3894ce72021-03-18 14:53:42 +080049 }
50 else if (pid < 0)
51 {
52 auto error = errno;
53 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
54 elog<InternalFailure>();
55 }
56 else if (pid > 0)
57 {
William A. Kennington III69f45542022-09-24 23:28:14 -070058 int status;
Lei YU3894ce72021-03-18 14:53:42 +080059 while (waitpid(pid, &status, 0) == -1)
60 {
61 if (errno != EINTR)
William A. Kennington III69f45542022-09-24 23:28:14 -070062 {
Lei YU3894ce72021-03-18 14:53:42 +080063 status = -1;
64 break;
65 }
66 }
67
68 if (status < 0)
69 {
William A. Kennington III69f45542022-09-24 23:28:14 -070070 fmt::memory_buffer buf;
71 fmt::format_to(fmt::appender(buf), "`{}`", path);
72 for (size_t i = 0; args[i] != nullptr; ++i)
Lei YU3894ce72021-03-18 14:53:42 +080073 {
William A. Kennington III69f45542022-09-24 23:28:14 -070074 fmt::format_to(fmt::appender(buf), " `{}`", args[i]);
Lei YU3894ce72021-03-18 14:53:42 +080075 }
William A. Kennington III69f45542022-09-24 23:28:14 -070076 buf.push_back('\0');
Lei YU3894ce72021-03-18 14:53:42 +080077 log<level::ERR>("Unable to execute the command",
William A. Kennington III69f45542022-09-24 23:28:14 -070078 entry("CMD=%s", buf.data()),
Lei YU3894ce72021-03-18 14:53:42 +080079 entry("STATUS=%d", status));
80 elog<InternalFailure>();
81 }
82 }
83}
84
Lei YU307554e2021-03-18 14:56:50 +080085/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070086std::string_view getIgnoredInterfacesEnv()
Lei YU307554e2021-03-18 14:56:50 +080087{
88 auto r = std::getenv("IGNORED_INTERFACES");
89 if (r == nullptr)
90 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070091 return "";
Lei YU307554e2021-03-18 14:56:50 +080092 }
93 return r;
94}
95
96/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -070097std::unordered_set<std::string_view>
98 parseInterfaces(std::string_view interfaces)
Lei YU307554e2021-03-18 14:56:50 +080099{
William A. Kennington III95530ec2022-08-19 01:44:39 -0700100 std::unordered_set<std::string_view> result;
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700101 while (true)
Lei YU307554e2021-03-18 14:56:50 +0800102 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700103 auto sep = interfaces.find(',');
104 auto interface = interfaces.substr(0, sep);
105 while (!interface.empty() && std::isspace(interface.front()))
Lei YU307554e2021-03-18 14:56:50 +0800106 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700107 interface.remove_prefix(1);
Lei YU307554e2021-03-18 14:56:50 +0800108 }
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700109 while (!interface.empty() && std::isspace(interface.back()))
Lei YU307554e2021-03-18 14:56:50 +0800110 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700111 interface.remove_suffix(1);
Lei YU307554e2021-03-18 14:56:50 +0800112 }
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700113 if (!interface.empty())
114 {
115 result.insert(interface);
116 }
117 if (sep == interfaces.npos)
118 {
119 break;
120 }
121 interfaces = interfaces.substr(sep + 1);
Lei YU307554e2021-03-18 14:56:50 +0800122 }
123 return result;
124}
125
126/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700127const std::unordered_set<std::string_view>& getIgnoredInterfaces()
Lei YU307554e2021-03-18 14:56:50 +0800128{
129 static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv());
130 return ignoredInterfaces;
131}
132
Lei YU3894ce72021-03-18 14:53:42 +0800133} // namespace internal
Ratan Gupta8804feb2017-05-25 10:49:57 +0530134
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700135constexpr auto familyVisit(auto&& visitor, int family)
136{
137 if (family == AF_INET)
138 {
139 return visitor.template operator()<AF_INET>();
140 }
141 else if (family == AF_INET6)
142 {
143 return visitor.template operator()<AF_INET6>();
144 }
145 throw std::invalid_argument("Invalid addr family");
146}
147
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700148template <int family>
149typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf)
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800150{
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700151 return stdplus::raw::copyFromStrict<typename FamilyTraits<family>::addr>(
152 buf);
153}
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800154
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700155InAddrAny addrFromBuf(int family, std::string_view buf)
156{
157 return familyVisit(
158 [=]<int f>() -> InAddrAny { return addrFromBuf<f>(buf); }, family);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800159}
160
William A. Kennington III9f228302022-10-07 14:02:49 -0700161bool isValidIP(int family, stdplus::const_zstring address) noexcept
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500162{
163 unsigned char buf[sizeof(struct in6_addr)];
William A. Kennington III9f228302022-10-07 14:02:49 -0700164 return inet_pton(family, address.c_str(), buf) > 0;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500165}
166
William A. Kennington IIIff12acb2022-10-07 19:06:56 -0700167bool isValidIP(stdplus::const_zstring address) noexcept
168{
169 return isValidIP(AF_INET, address) || isValidIP(AF_INET6, address);
170}
171
William A. Kennington III69f45542022-09-24 23:28:14 -0700172void deleteInterface(stdplus::const_zstring intf)
Ratan Guptabc886292017-07-25 18:29:57 +0530173{
174 pid_t pid = fork();
Gunnar Mills57d9c502018-09-14 14:42:34 -0500175 int status{};
Ratan Guptabc886292017-07-25 18:29:57 +0530176
177 if (pid == 0)
178 {
179
180 execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr);
181 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182 log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error),
Ratan Guptabc886292017-07-25 18:29:57 +0530183 entry("INTF=%s", intf.c_str()));
184 elog<InternalFailure>();
185 }
186 else if (pid < 0)
187 {
188 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500189 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
Ratan Guptabc886292017-07-25 18:29:57 +0530190 elog<InternalFailure>();
191 }
192 else if (pid > 0)
193 {
194 while (waitpid(pid, &status, 0) == -1)
195 {
196 if (errno != EINTR)
Gunnar Mills57d9c502018-09-14 14:42:34 -0500197 { /* Error other than EINTR */
Ratan Guptabc886292017-07-25 18:29:57 +0530198 status = -1;
199 break;
200 }
201 }
202
Gunnar Mills57d9c502018-09-14 14:42:34 -0500203 if (status < 0)
Ratan Guptabc886292017-07-25 18:29:57 +0530204 {
205 log<level::ERR>("Unable to delete the interface",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500206 entry("INTF=%s", intf.c_str()),
207 entry("STATUS=%d", status));
Ratan Guptabc886292017-07-25 18:29:57 +0530208 elog<InternalFailure>();
209 }
210 }
211}
212
William A. Kennington III69f45542022-09-24 23:28:14 -0700213std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700214{
William A. Kennington III69f45542022-09-24 23:28:14 -0700215 constexpr auto pfx = "eth"sv;
216 if (!intf.starts_with(pfx))
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700217 {
218 return std::nullopt;
219 }
William A. Kennington III69f45542022-09-24 23:28:14 -0700220 intf.remove_prefix(pfx.size());
221 auto last = intf.data() + intf.size();
222 unsigned long idx;
223 auto res = std::from_chars(intf.data(), last, idx);
224 if (res.ec != std::errc() || res.ptr != last)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700225 {
226 return std::nullopt;
227 }
228 if (idx == 0)
229 {
230 return "ethaddr";
231 }
William A. Kennington III69f45542022-09-24 23:28:14 -0700232 return fmt::format(FMT_COMPILE("eth{}addr"), idx);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700233}
234
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700235static std::optional<DHCPVal> systemdParseDHCP(std::string_view str)
236{
237 if (config::icaseeq(str, "ipv4"))
238 {
239 return DHCPVal{.v4 = true, .v6 = false};
240 }
241 if (config::icaseeq(str, "ipv6"))
242 {
243 return DHCPVal{.v4 = false, .v6 = true};
244 }
245 if (auto b = config::parseBool(str); b)
246 {
247 return DHCPVal{.v4 = *b, .v6 = *b};
248 }
249 return std::nullopt;
250}
251
252inline auto systemdParseLast(const config::Parser& config,
253 std::string_view section, std::string_view key,
254 auto&& fun)
255{
256 if (auto str = config.map.getLastValueString(section, key); str == nullptr)
257 {
258 auto err = fmt::format("Unable to get the value of {}[{}] from {}",
259 section, key, config.getFilename().native());
260 log<level::NOTICE>(err.c_str(),
261 entry("FILE=%s", config.getFilename().c_str()));
262 }
263 else if (auto val = fun(*str); !val)
264 {
265 auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section,
266 key, config.getFilename().native(), *str);
267 log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()),
268 entry("FILE=%s", config.getFilename().c_str()));
269 }
270 else
271 {
272 return val;
273 }
274 return decltype(fun(std::string_view{}))(std::nullopt);
275}
276
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700277bool getIPv6AcceptRA(const config::Parser& config)
Ratan Gupta56187e72017-08-13 09:40:14 +0530278{
William A. Kennington III324d2602022-08-18 18:32:56 -0700279#ifdef ENABLE_IPV6_ACCEPT_RA
280 constexpr bool def = true;
281#else
282 constexpr bool def = false;
283#endif
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700284 return systemdParseLast(config, "Network", "IPv6AcceptRA",
285 config::parseBool)
286 .value_or(def);
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700287}
288
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700289DHCPVal getDHCPValue(const config::Parser& config)
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700290{
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700291 return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP)
292 .value_or(DHCPVal{.v4 = true, .v6 = true});
293}
William A. Kennington III324d2602022-08-18 18:32:56 -0700294
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700295bool getDHCPProp(const config::Parser& config, std::string_view key)
296{
297 return systemdParseLast(config, "DHCP", key, config::parseBool)
298 .value_or(true);
Ratan Gupta56187e72017-08-13 09:40:14 +0530299}
300
Ratan Guptabd303b12017-08-18 17:10:07 +0530301namespace mac_address
302{
303
304constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper";
305constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper";
306constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
307constexpr auto propIntf = "org.freedesktop.DBus.Properties";
308constexpr auto methodGet = "Get";
Manojkiran Edacc099a82020-05-11 14:25:16 +0530309constexpr auto configFile = "/usr/share/network/config.json";
Ratan Guptabd303b12017-08-18 17:10:07 +0530310
311using DbusObjectPath = std::string;
312using DbusService = std::string;
313using DbusInterface = std::string;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500314using ObjectTree =
315 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Ratan Guptabd303b12017-08-18 17:10:07 +0530316
317constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager";
318constexpr auto invNetworkIntf =
Gunnar Mills57d9c502018-09-14 14:42:34 -0500319 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
Ratan Guptabd303b12017-08-18 17:10:07 +0530320constexpr auto invRoot = "/xyz/openbmc_project/inventory";
321
Patrick Williamsc38b0712022-07-22 19:26:54 -0500322ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName)
Ratan Guptabd303b12017-08-18 17:10:07 +0530323{
Manojkiran Edacc099a82020-05-11 14:25:16 +0530324
325 std::string interfaceName = intfName;
326
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700327#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530328 // load the config JSON from the Read Only Path
329 std::ifstream in(configFile);
330 nlohmann::json configJson;
331 in >> configJson;
332 interfaceName = configJson[intfName];
333#endif
334
Ratan Guptabd303b12017-08-18 17:10:07 +0530335 std::vector<DbusInterface> interfaces;
336 interfaces.emplace_back(invNetworkIntf);
337
338 auto depth = 0;
339
Gunnar Mills57d9c502018-09-14 14:42:34 -0500340 auto mapperCall =
341 bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree");
Ratan Guptabd303b12017-08-18 17:10:07 +0530342
343 mapperCall.append(invRoot, depth, interfaces);
344
345 auto mapperReply = bus.call(mapperCall);
346 if (mapperReply.is_method_error())
347 {
348 log<level::ERR>("Error in mapper call");
349 elog<InternalFailure>();
350 }
351
352 ObjectTree objectTree;
353 mapperReply.read(objectTree);
354
355 if (objectTree.empty())
356 {
357 log<level::ERR>("No Object has implemented the interface",
358 entry("INTERFACE=%s", invNetworkIntf));
359 elog<InternalFailure>();
360 }
361
Alvin Wang38a63c32019-08-29 22:56:46 +0800362 DbusObjectPath objPath;
363 DbusService service;
Ratan Guptabd303b12017-08-18 17:10:07 +0530364
Alvin Wang38a63c32019-08-29 22:56:46 +0800365 if (1 == objectTree.size())
366 {
367 objPath = objectTree.begin()->first;
368 service = objectTree.begin()->second.begin()->first;
369 }
370 else
371 {
372 // If there are more than 2 objects, object path must contain the
373 // interface name
374 for (auto const& object : objectTree)
375 {
Manojkiran Edacc099a82020-05-11 14:25:16 +0530376 log<level::INFO>("interface",
377 entry("INT=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800378 log<level::INFO>("object", entry("OBJ=%s", object.first.c_str()));
Manojkiran Edacc099a82020-05-11 14:25:16 +0530379
380 if (std::string::npos != object.first.find(interfaceName.c_str()))
Alvin Wang38a63c32019-08-29 22:56:46 +0800381 {
382 objPath = object.first;
383 service = object.second.begin()->first;
384 break;
385 }
386 }
387
388 if (objPath.empty())
389 {
390 log<level::ERR>("Can't find the object for the interface",
Manojkiran Edacc099a82020-05-11 14:25:16 +0530391 entry("intfName=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800392 elog<InternalFailure>();
393 }
394 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530395
Gunnar Mills57d9c502018-09-14 14:42:34 -0500396 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
397 propIntf, methodGet);
Ratan Guptabd303b12017-08-18 17:10:07 +0530398
399 method.append(invNetworkIntf, "MACAddress");
400
401 auto reply = bus.call(method);
402 if (reply.is_method_error())
403 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500404 log<level::ERR>("Failed to get MACAddress",
Ratan Guptabd303b12017-08-18 17:10:07 +0530405 entry("PATH=%s", objPath.c_str()),
406 entry("INTERFACE=%s", invNetworkIntf));
407 elog<InternalFailure>();
408 }
409
William A. Kennington III1137a972019-04-20 20:49:58 -0700410 std::variant<std::string> value;
Ratan Guptabd303b12017-08-18 17:10:07 +0530411 reply.read(value);
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -0700412 return ToAddr<ether_addr>{}(std::get<std::string>(value));
Ratan Guptabd303b12017-08-18 17:10:07 +0530413}
414
William A. Kennington III1137a972019-04-20 20:49:58 -0700415bool isEmpty(const ether_addr& mac)
416{
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700417 return mac == ether_addr{};
William A. Kennington III1137a972019-04-20 20:49:58 -0700418}
419
420bool isMulticast(const ether_addr& mac)
421{
422 return mac.ether_addr_octet[0] & 0b1;
423}
424
425bool isUnicast(const ether_addr& mac)
426{
427 return !isEmpty(mac) && !isMulticast(mac);
428}
429
Gunnar Mills57d9c502018-09-14 14:42:34 -0500430} // namespace mac_address
431} // namespace network
432} // namespace phosphor