1 #include "config.h"
2 
3 #include "network_manager.hpp"
4 
5 #include "ipaddress.hpp"
6 #include "network_config.hpp"
7 #include "types.hpp"
8 #include "util.hpp"
9 
10 #include <arpa/inet.h>
11 #include <dirent.h>
12 #include <net/if.h>
13 
14 #include <algorithm>
15 #include <charconv>
16 #include <filesystem>
17 #include <fstream>
18 #include <phosphor-logging/elog-errors.hpp>
19 #include <phosphor-logging/log.hpp>
20 #include <string>
21 #include <xyz/openbmc_project/Common/error.hpp>
22 
23 constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
24 constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
25 constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
26 constexpr auto FirstBootFile = "/var/lib/network/firstBoot_";
27 
28 constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1";
29 constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1";
30 constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager";
31 
32 namespace phosphor
33 {
34 namespace network
35 {
36 
37 extern std::unique_ptr<Timer> refreshObjectTimer;
38 extern std::unique_ptr<Timer> reloadTimer;
39 using namespace phosphor::logging;
40 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
41 using Argument = xyz::openbmc_project::Common::InvalidArgument;
42 
43 Manager::Manager(sdbusplus::bus_t& bus, const char* objPath,
44                  const fs::path& confDir) :
45     details::VLANCreateIface(bus, objPath,
46                              details::VLANCreateIface::action::defer_emit),
47     bus(bus), objectPath(objPath)
48 {
49     setConfDir(confDir);
50 }
51 
52 bool Manager::createDefaultNetworkFiles()
53 {
54     auto isCreated = false;
55     try
56     {
57         auto interfaceStrList = getSystemInterfaces();
58         for (const auto& interface : interfaceStrList)
59         {
60             // if the interface has '.' in the name, it means that this is a
61             // VLAN - don't create the network file.
62             if (interface.find(".") != std::string::npos)
63             {
64                 continue;
65             }
66 
67             fs::path filePath = config::pathForIntfConf(confDir, interface);
68 
69             // create the interface specific network file
70             // if not existing.
71             if (!fs::is_regular_file(filePath))
72             {
73                 bmc::writeDHCPDefault(filePath, interface);
74                 log<level::INFO>("Created the default network file.",
75                                  entry("INTERFACE=%s", interface.c_str()));
76                 isCreated = true;
77             }
78         }
79     }
80     catch (const std::exception& e)
81     {
82         log<level::ERR>("Unable to create the default network file");
83     }
84 
85     return isCreated;
86 }
87 
88 void Manager::setConfDir(const fs::path& dir)
89 {
90     confDir = dir;
91 
92     if (!fs::exists(confDir))
93     {
94         if (!fs::create_directories(confDir))
95         {
96             log<level::ERR>("Unable to create the network conf dir",
97                             entry("DIR=%s", confDir.c_str()));
98             elog<InternalFailure>();
99         }
100     }
101 }
102 
103 void Manager::createInterfaces()
104 {
105     // clear all the interfaces first
106     interfaces.clear();
107 
108     auto interfaceStrList = getSystemInterfaces();
109 
110     for (auto& interface : interfaceStrList)
111     {
112         fs::path objPath = objectPath;
113         auto index = interface.find(".");
114 
115         // interface can be of vlan type or normal ethernet interface.
116         // vlan interface looks like "interface.vlanid",so here by looking
117         // at the interface name we decide that we need
118         // to create the vlaninterface or normal physical interface.
119         if (index != std::string::npos)
120         {
121             // it is vlan interface
122             auto sv = std::string_view(interface);
123             auto interfaceName = sv.substr(0, index);
124             auto vlanStr = sv.substr(index + 1);
125             uint16_t vlanId;
126             auto res = std::from_chars(vlanStr.begin(), vlanStr.end(), vlanId);
127             if (res.ec != std::errc() || res.ptr != vlanStr.end())
128             {
129                 auto msg = fmt::format("Invalid VLAN: {}", vlanStr);
130                 log<level::ERR>(msg.c_str());
131                 continue;
132             }
133             auto it = interfaces.find(interfaceName);
134             if (it == interfaces.end())
135             {
136                 auto msg = fmt::format("Missing interface({}) for VLAN({}): {}",
137                                        interfaceName, vlanId, interface);
138                 log<level::ERR>(msg.c_str());
139                 continue;
140             }
141             it->second->loadVLAN(vlanId);
142             continue;
143         }
144         // normal ethernet interface
145         objPath /= interface;
146         config::Parser config(config::pathForIntfConf(confDir, interface));
147 
148         auto intf = std::make_unique<phosphor::network::EthernetInterface>(
149             bus, objPath.string(), config, *this);
150 
151         intf->createIPAddressObjects();
152         intf->createStaticNeighborObjects();
153         intf->loadNameServers(config);
154         intf->loadNTPServers(config);
155 
156         this->interfaces.emplace(std::move(interface), std::move(intf));
157     }
158 }
159 
160 void Manager::createChildObjects()
161 {
162     routeTable.refresh();
163 
164     // creates the ethernet interface dbus object.
165     createInterfaces();
166 
167     systemConf.reset(nullptr);
168     dhcpConf.reset(nullptr);
169 
170     fs::path objPath = objectPath;
171     objPath /= "config";
172 
173     // create the system conf object.
174     systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
175         bus, objPath.string());
176     // create the dhcp conf object.
177     objPath /= "dhcp";
178     dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
179         bus, objPath.string(), *this);
180 }
181 
182 ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
183 {
184     if (id == 0 || id >= 4095)
185     {
186         log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
187         elog<InvalidArgument>(
188             Argument::ARGUMENT_NAME("VLANId"),
189             Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
190     }
191 
192     auto it = interfaces.find(interfaceName);
193     if (it == interfaces.end())
194     {
195         using ResourceErr =
196             phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
197         elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
198     }
199     return it->second->createVLAN(id);
200 }
201 
202 void Manager::reset()
203 {
204     if (fs::is_directory(confDir))
205     {
206         for (const auto& file : fs::directory_iterator(confDir))
207         {
208             fs::remove(file.path());
209         }
210     }
211     createDefaultNetworkFiles();
212     log<level::INFO>("Network Factory Reset queued.");
213 }
214 
215 // Need to merge the below function with the code which writes the
216 // config file during factory reset.
217 // TODO openbmc/openbmc#1751
218 void Manager::writeToConfigurationFile()
219 {
220     // write all the static ip address in the systemd-network conf file
221     for (const auto& intf : interfaces)
222     {
223         intf.second->writeConfigurationFile();
224     }
225 }
226 
227 #ifdef SYNC_MAC_FROM_INVENTORY
228 void Manager::setFistBootMACOnInterface(
229     const std::pair<std::string, std::string>& inventoryEthPair)
230 {
231     for (const auto& interface : interfaces)
232     {
233         if (interface.first == inventoryEthPair.first)
234         {
235             auto returnMAC =
236                 interface.second->macAddress(inventoryEthPair.second);
237             if (returnMAC == inventoryEthPair.second)
238             {
239                 log<level::INFO>("Set the MAC on "),
240                     entry("interface : ", interface.first.c_str()),
241                     entry("MAC : ", inventoryEthPair.second.c_str());
242                 std::error_code ec;
243                 if (std::filesystem::is_directory("/var/lib/network", ec))
244                 {
245                     std::ofstream persistentFile(FirstBootFile +
246                                                  interface.first);
247                 }
248                 break;
249             }
250             else
251             {
252                 log<level::INFO>("MAC is Not Set on ethernet Interface");
253             }
254         }
255     }
256 }
257 
258 #endif
259 
260 void Manager::reloadConfigs()
261 {
262     reloadTimer->restartOnce(reloadTimeout);
263     // Ensure that the next refresh happens after reconfiguration
264     refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
265 }
266 
267 void Manager::doReloadConfigs()
268 {
269     for (auto& hook : reloadPreHooks)
270     {
271         try
272         {
273             hook();
274         }
275         catch (const std::exception& ex)
276         {
277             log<level::ERR>("Failed executing reload hook, ignoring",
278                             entry("ERR=%s", ex.what()));
279         }
280     }
281     reloadPreHooks.clear();
282     try
283     {
284         auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
285                                           NETWORKD_INTERFACE, "Reload");
286         bus.call_noreply(method);
287     }
288     catch (const sdbusplus::exception_t& ex)
289     {
290         log<level::ERR>("Failed to reload configuration",
291                         entry("ERR=%s", ex.what()));
292         elog<InternalFailure>();
293     }
294     // Ensure reconfiguration has enough time
295     refreshObjectTimer->setRemaining(refreshTimeout);
296 }
297 
298 } // namespace network
299 } // namespace phosphor
300