blob: fe59f0b9e2b68f4c100685bd6d8d8ac39ca92ba9 [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
2
Ratan Gupta6811f822017-04-14 16:34:56 +05303#include "network_manager.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07004
Ratan Gupta5978dd12017-07-25 13:47:13 +05305#include "ipaddress.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include "network_config.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07007#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include "util.hpp"
Ratan Gupta738a67f2017-04-21 10:38:05 +05309
Ratan Gupta6811f822017-04-14 16:34:56 +053010#include <arpa/inet.h>
11#include <dirent.h>
12#include <net/if.h>
13
Patrick Venture189d44e2018-07-09 12:30:59 -070014#include <algorithm>
15#include <bitset>
Manojkiran Edacc099a82020-05-11 14:25:16 +053016#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <fstream>
18#include <map>
19#include <phosphor-logging/elog-errors.hpp>
20#include <phosphor-logging/log.hpp>
Michael Tritz29f2fd62017-05-22 15:27:26 -050021#include <string>
Patrick Venture189d44e2018-07-09 12:30:59 -070022#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053023
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080024constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
25constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
26constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
Manojkiran Edacc099a82020-05-11 14:25:16 +053027constexpr auto FirstBootFile = "/var/lib/network/firstBoot_";
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080028
William A. Kennington III56ecc782021-10-07 18:44:50 -070029constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1";
30constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1";
31constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager";
32
Ratan Gupta6811f822017-04-14 16:34:56 +053033namespace phosphor
34{
35namespace network
36{
Ratan Gupta82549cc2017-04-21 08:45:23 +053037
William A. Kennington III3a70fa22018-09-20 18:48:20 -070038extern std::unique_ptr<Timer> refreshObjectTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053039using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053040using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053041
Ratan Gupta255d5142017-08-10 09:02:08 +053042Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath,
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 const std::string& path) :
Ratan Gupta29b0e432017-05-25 12:51:40 +053044 details::VLANCreateIface(bus, objPath, true),
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 bus(bus), objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053046{
Ratan Gupta255d5142017-08-10 09:02:08 +053047 fs::path confDir(path);
48 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053049}
50
Ratan Guptab610caf2017-09-19 09:33:51 +053051bool Manager::createDefaultNetworkFiles(bool force)
52{
53 auto isCreated = false;
54 try
55 {
56 // Directory would have created before with
57 // setConfDir function.
58 if (force)
59 {
60 // Factory Reset case
61 // we need to forcefully write the files
62 // so delete the existing ones.
63 if (fs::is_directory(confDir))
64 {
65 for (const auto& file : fs::directory_iterator(confDir))
66 {
67 fs::remove(file.path());
68 }
69 }
70 }
71
72 auto interfaceStrList = getInterfaces();
73 for (const auto& interface : interfaceStrList)
74 {
Michael Tritz08c34f42017-10-16 14:59:09 -050075 // if the interface has '.' in the name, it means that this is a
76 // VLAN - don't create the network file.
77 if (interface.find(".") != std::string::npos)
78 {
79 continue;
80 }
81
Ratan Guptab610caf2017-09-19 09:33:51 +053082 auto fileName = systemd::config::networkFilePrefix + interface +
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 systemd::config::networkFileSuffix;
Ratan Guptab610caf2017-09-19 09:33:51 +053084
85 fs::path filePath = confDir;
86 filePath /= fileName;
87
88 // create the interface specific network file
89 // if not exist or we forcefully wants to write
90 // the network file.
91
92 if (force || !fs::is_regular_file(filePath.string()))
93 {
94 bmc::writeDHCPDefault(filePath.string(), interface);
95 log<level::INFO>("Created the default network file.",
Gunnar Mills57d9c502018-09-14 14:42:34 -050096 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptab610caf2017-09-19 09:33:51 +053097 isCreated = true;
98 }
99 }
100 }
Patrick Williams5758db32021-10-06 12:29:22 -0500101 catch (const std::exception& e)
Ratan Guptab610caf2017-09-19 09:33:51 +0530102 {
103 log<level::ERR>("Unable to create the default network file");
104 }
Alexander Filippov1ea35992021-03-26 13:10:05 +0300105
Ratan Guptab610caf2017-09-19 09:33:51 +0530106 return isCreated;
107}
108
Ratan Guptaef85eb92017-06-15 08:57:54 +0530109void Manager::setConfDir(const fs::path& dir)
110{
111 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +0530112
113 if (!fs::exists(confDir))
114 {
115 if (!fs::create_directories(confDir))
116 {
117 log<level::ERR>("Unable to create the network conf dir",
118 entry("DIR=%s", confDir.c_str()));
119 elog<InternalFailure>();
120 }
121 }
Ratan Gupta29b0e432017-05-25 12:51:40 +0530122}
123
124void Manager::createInterfaces()
125{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500126 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530127 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530128
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530129 auto interfaceStrList = getInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530130
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530131 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530132 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530133 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530134 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530135
136 // interface can be of vlan type or normal ethernet interface.
137 // vlan interface looks like "interface.vlanid",so here by looking
138 // at the interface name we decide that we need
139 // to create the vlaninterface or normal physical interface.
140 if (index != std::string::npos)
141 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500142 // it is vlan interface
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530143 auto interfaceName = interface.substr(0, index);
144 auto vlanid = interface.substr(index + 1);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530145 uint32_t vlanInt = std::stoul(vlanid);
146
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530147 interfaces[interfaceName]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530148 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530149 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530150 // normal ethernet interface
151 objPath /= interface;
Ratan Gupta6811f822017-04-14 16:34:56 +0530152
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530153 auto dhcp = getDHCPValue(confDir, interface);
Ratan Gupta34f96d62017-06-15 09:16:22 +0530154
Gunnar Mills57d9c502018-09-14 14:42:34 -0500155 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
156 bus, objPath.string(), dhcp, *this);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530157
158 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -0800159 intf->createStaticNeighborObjects();
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530160 intf->loadNameServers();
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530161
Gunnar Mills57d9c502018-09-14 14:42:34 -0500162 this->interfaces.emplace(
163 std::make_pair(std::move(interface), std::move(intf)));
Ratan Gupta6811f822017-04-14 16:34:56 +0530164 }
165}
166
Ratan Guptaef85eb92017-06-15 08:57:54 +0530167void Manager::createChildObjects()
168{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700169 routeTable.refresh();
170
Ratan Guptaef85eb92017-06-15 08:57:54 +0530171 // creates the ethernet interface dbus object.
172 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530173
174 systemConf.reset(nullptr);
175 dhcpConf.reset(nullptr);
176
Ratan Guptaef85eb92017-06-15 08:57:54 +0530177 fs::path objPath = objectPath;
178 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530179
180 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530181 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182 bus, objPath.string(), *this);
Ratan Guptad16f88c2017-07-11 17:47:57 +0530183 // create the dhcp conf object.
184 objPath /= "dhcp";
185 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500186 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530187}
188
Patrick Williams8173e022021-05-01 06:39:41 -0500189ObjectPath Manager::vlan(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530190{
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700191 return interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530192}
193
Michael Tritz29f2fd62017-05-22 15:27:26 -0500194void Manager::reset()
195{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500196 if (!createDefaultNetworkFiles(true))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500197 {
Ratan Guptab610caf2017-09-19 09:33:51 +0530198 log<level::ERR>("Network Factory Reset failed.");
199 return;
200 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
Michael Tritz29f2fd62017-05-22 15:27:26 -0500201 }
202
Ratan Guptab610caf2017-09-19 09:33:51 +0530203 log<level::INFO>("Network Factory Reset done.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500204}
205
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530206// Need to merge the below function with the code which writes the
207// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500208// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530209void Manager::writeToConfigurationFile()
210{
211 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530212 for (const auto& intf : interfaces)
213 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530214 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530215 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530216}
217
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700218#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530219void Manager::setFistBootMACOnInterface(
220 const std::pair<std::string, std::string>& inventoryEthPair)
221{
222 for (const auto& interface : interfaces)
223 {
224 if (interface.first == inventoryEthPair.first)
225 {
226 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500227 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530228 if (returnMAC == inventoryEthPair.second)
229 {
230 log<level::INFO>("Set the MAC on "),
231 entry("interface : ", interface.first.c_str()),
232 entry("MAC : ", inventoryEthPair.second.c_str());
233 std::error_code ec;
234 if (std::filesystem::is_directory("/var/lib/network", ec))
235 {
236 std::ofstream persistentFile(FirstBootFile +
237 interface.first);
238 }
239 break;
240 }
241 else
242 {
243 log<level::INFO>("MAC is Not Set on ethernet Interface");
244 }
245 }
246 }
247}
248
249#endif
250
William A. Kennington III56ecc782021-10-07 18:44:50 -0700251void Manager::reloadConfigs()
252{
253 try
254 {
255 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
256 NETWORKD_INTERFACE, "Reload");
257 bus.call_noreply(method);
258 }
259 catch (const sdbusplus::exception::exception& ex)
260 {
261 log<level::ERR>("Failed to reload configuration",
262 entry("ERR=%s", ex.what()));
263 elog<InternalFailure>();
264 }
265}
266
Gunnar Mills57d9c502018-09-14 14:42:34 -0500267} // namespace network
268} // namespace phosphor