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 std::string& path) :
45     details::VLANCreateIface(bus, objPath,
46                              details::VLANCreateIface::action::defer_emit),
47     bus(bus), objectPath(objPath)
48 {
49     fs::path confDir(path);
50     setConfDir(confDir);
51 }
52 
53 bool Manager::createDefaultNetworkFiles()
54 {
55     auto isCreated = false;
56     try
57     {
58         auto interfaceStrList = getSystemInterfaces();
59         for (const auto& interface : interfaceStrList)
60         {
61             // if the interface has '.' in the name, it means that this is a
62             // VLAN - don't create the network file.
63             if (interface.find(".") != std::string::npos)
64             {
65                 continue;
66             }
67 
68             fs::path filePath = config::pathForIntfConf(confDir, interface);
69 
70             // create the interface specific network file
71             // if not existing.
72             if (!fs::is_regular_file(filePath))
73             {
74                 bmc::writeDHCPDefault(filePath, interface);
75                 log<level::INFO>("Created the default network file.",
76                                  entry("INTERFACE=%s", interface.c_str()));
77                 isCreated = true;
78             }
79         }
80     }
81     catch (const std::exception& e)
82     {
83         log<level::ERR>("Unable to create the default network file");
84     }
85 
86     return isCreated;
87 }
88 
89 void Manager::setConfDir(const fs::path& dir)
90 {
91     confDir = dir;
92 
93     if (!fs::exists(confDir))
94     {
95         if (!fs::create_directories(confDir))
96         {
97             log<level::ERR>("Unable to create the network conf dir",
98                             entry("DIR=%s", confDir.c_str()));
99             elog<InternalFailure>();
100         }
101     }
102 }
103 
104 void Manager::createInterfaces()
105 {
106     // clear all the interfaces first
107     interfaces.clear();
108 
109     auto interfaceStrList = getSystemInterfaces();
110 
111     for (auto& interface : interfaceStrList)
112     {
113         fs::path objPath = objectPath;
114         auto index = interface.find(".");
115 
116         // interface can be of vlan type or normal ethernet interface.
117         // vlan interface looks like "interface.vlanid",so here by looking
118         // at the interface name we decide that we need
119         // to create the vlaninterface or normal physical interface.
120         if (index != std::string::npos)
121         {
122             // it is vlan interface
123             auto sv = std::string_view(interface);
124             auto interfaceName = sv.substr(0, index);
125             auto vlanStr = sv.substr(index + 1);
126             uint16_t vlanId;
127             auto res = std::from_chars(vlanStr.begin(), vlanStr.end(), vlanId);
128             if (res.ec != std::errc() || res.ptr != vlanStr.end())
129             {
130                 auto msg = fmt::format("Invalid VLAN: {}", vlanStr);
131                 log<level::ERR>(msg.c_str());
132                 continue;
133             }
134             auto it = interfaces.find(interfaceName);
135             if (it == interfaces.end())
136             {
137                 auto msg = fmt::format("Missing interface({}) for VLAN({}): {}",
138                                        interfaceName, vlanId, interface);
139                 log<level::ERR>(msg.c_str());
140                 continue;
141             }
142             it->second->loadVLAN(vlanId);
143             continue;
144         }
145         // normal ethernet interface
146         objPath /= interface;
147         config::Parser config(config::pathForIntfConf(confDir, interface));
148 
149         auto intf = std::make_unique<phosphor::network::EthernetInterface>(
150             bus, objPath.string(), config, *this);
151 
152         intf->createIPAddressObjects();
153         intf->createStaticNeighborObjects();
154         intf->loadNameServers(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