xref: /openbmc/phosphor-networkd/src/util.cpp (revision 89d734b9886c40fa530f9fd6e67eb87b6955ec08)
1 #include "config.h"
2 
3 #include "util.hpp"
4 
5 #include "config_parser.hpp"
6 #include "types.hpp"
7 
8 #include <fmt/compile.h>
9 #include <fmt/format.h>
10 #include <sys/wait.h>
11 
12 #include <phosphor-logging/elog-errors.hpp>
13 #include <phosphor-logging/lg2.hpp>
14 #include <xyz/openbmc_project/Common/error.hpp>
15 
16 #include <cctype>
17 #include <string>
18 #include <string_view>
19 
20 namespace phosphor
21 {
22 namespace network
23 {
24 
25 using std::literals::string_view_literals::operator""sv;
26 using namespace phosphor::logging;
27 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
28 
29 namespace internal
30 {
31 
32 void executeCommandinChildProcess(stdplus::const_zstring path, char** args)
33 {
34     using namespace std::string_literals;
35     pid_t pid = fork();
36 
37     if (pid == 0)
38     {
39         execv(path.c_str(), args);
40         exit(255);
41     }
42     else if (pid < 0)
43     {
44         auto error = errno;
45         lg2::error("Error occurred during fork: {ERRNO}", "ERRNO", error);
46         elog<InternalFailure>();
47     }
48     else if (pid > 0)
49     {
50         int status;
51         while (waitpid(pid, &status, 0) == -1)
52         {
53             if (errno != EINTR)
54             {
55                 status = -1;
56                 break;
57             }
58         }
59 
60         if (status < 0)
61         {
62             fmt::memory_buffer buf;
63             fmt::format_to(fmt::appender(buf), "`{}`", path);
64             for (size_t i = 0; args[i] != nullptr; ++i)
65             {
66                 fmt::format_to(fmt::appender(buf), " `{}`", args[i]);
67             }
68             buf.push_back('\0');
69             lg2::error("Unable to execute the command {CMD}: {STATUS}", "CMD",
70                        buf.data(), "STATUS", status);
71             elog<InternalFailure>();
72         }
73     }
74 }
75 
76 /** @brief Get ignored interfaces from environment */
77 std::string_view getIgnoredInterfacesEnv()
78 {
79     auto r = std::getenv("IGNORED_INTERFACES");
80     if (r == nullptr)
81     {
82         return "";
83     }
84     return r;
85 }
86 
87 /** @brief Parse the comma separated interface names */
88 std::unordered_set<std::string_view>
89     parseInterfaces(std::string_view interfaces)
90 {
91     std::unordered_set<std::string_view> result;
92     while (true)
93     {
94         auto sep = interfaces.find(',');
95         auto interface = interfaces.substr(0, sep);
96         while (!interface.empty() && std::isspace(interface.front()))
97         {
98             interface.remove_prefix(1);
99         }
100         while (!interface.empty() && std::isspace(interface.back()))
101         {
102             interface.remove_suffix(1);
103         }
104         if (!interface.empty())
105         {
106             result.insert(interface);
107         }
108         if (sep == interfaces.npos)
109         {
110             break;
111         }
112         interfaces = interfaces.substr(sep + 1);
113     }
114     return result;
115 }
116 
117 /** @brief Get the ignored interfaces */
118 const std::unordered_set<std::string_view>& getIgnoredInterfaces()
119 {
120     static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv());
121     return ignoredInterfaces;
122 }
123 
124 } // namespace internal
125 
126 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf)
127 {
128     constexpr auto pfx = "eth"sv;
129     if (!intf.starts_with(pfx))
130     {
131         return std::nullopt;
132     }
133     intf.remove_prefix(pfx.size());
134     unsigned idx;
135     try
136     {
137         idx = DecodeInt<unsigned, 10>{}(intf);
138     }
139     catch (...)
140     {
141         return std::nullopt;
142     }
143     if (idx == 0)
144     {
145         return "ethaddr";
146     }
147     return fmt::format(FMT_COMPILE("eth{}addr"), idx);
148 }
149 
150 static std::optional<DHCPVal> systemdParseDHCP(std::string_view str)
151 {
152     if (config::icaseeq(str, "ipv4"))
153     {
154         return DHCPVal{.v4 = true, .v6 = false};
155     }
156     if (config::icaseeq(str, "ipv6"))
157     {
158         return DHCPVal{.v4 = false, .v6 = true};
159     }
160     if (auto b = config::parseBool(str); b)
161     {
162         return DHCPVal{.v4 = *b, .v6 = *b};
163     }
164     return std::nullopt;
165 }
166 
167 inline auto systemdParseLast(const config::Parser& config,
168                              std::string_view section, std::string_view key,
169                              auto&& fun)
170 {
171     if (!config.getFileExists())
172     {}
173     else if (auto str = config.map.getLastValueString(section, key);
174              str == nullptr)
175     {
176         lg2::notice("Unable to get the value of {SECTION}[{KEY}] from {FILE}",
177                     "SECTION", section, "KEY", key, "FILE",
178                     config.getFilename());
179     }
180     else if (auto val = fun(*str); !val)
181     {
182         lg2::notice("Invalid value of {SECTION}[{KEY}] from {FILE}: {VALUE}",
183                     "SECTION", section, "KEY", key, "FILE",
184                     config.getFilename(), "VALUE", *str);
185     }
186     else
187     {
188         return val;
189     }
190     return decltype(fun(std::string_view{}))(std::nullopt);
191 }
192 
193 bool getIPv6AcceptRA(const config::Parser& config)
194 {
195 #ifdef ENABLE_IPV6_ACCEPT_RA
196     constexpr bool def = true;
197 #else
198     constexpr bool def = false;
199 #endif
200     return systemdParseLast(config, "Network", "IPv6AcceptRA",
201                             config::parseBool)
202         .value_or(def);
203 }
204 
205 DHCPVal getDHCPValue(const config::Parser& config)
206 {
207     return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP)
208         .value_or(DHCPVal{.v4 = true, .v6 = true});
209 }
210 
211 bool getDHCPProp(const config::Parser& config, std::string_view key)
212 {
213     return systemdParseLast(config, "DHCP", key, config::parseBool)
214         .value_or(true);
215 }
216 
217 namespace mac_address
218 {
219 
220 bool isEmpty(const ether_addr& mac)
221 {
222     return mac == ether_addr{};
223 }
224 
225 bool isMulticast(const ether_addr& mac)
226 {
227     return mac.ether_addr_octet[0] & 0b1;
228 }
229 
230 bool isUnicast(const ether_addr& mac)
231 {
232     return !isEmpty(mac) && !isMulticast(mac);
233 }
234 
235 } // namespace mac_address
236 } // namespace network
237 } // namespace phosphor
238