blob: 25b9c05d31c2ff66fa9208c2d464533d7bccb169 [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>
William A. Kennington III085bbdc2022-10-05 02:45:37 -070015#include <charconv>
Manojkiran Edacc099a82020-05-11 14:25:16 +053016#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -070018#include <phosphor-logging/elog-errors.hpp>
19#include <phosphor-logging/log.hpp>
Michael Tritz29f2fd62017-05-22 15:27:26 -050020#include <string>
Patrick Venture189d44e2018-07-09 12:30:59 -070021#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053022
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080023constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
24constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
25constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
Manojkiran Edacc099a82020-05-11 14:25:16 +053026constexpr auto FirstBootFile = "/var/lib/network/firstBoot_";
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080027
William A. Kennington III56ecc782021-10-07 18:44:50 -070028constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1";
29constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1";
30constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager";
31
Ratan Gupta6811f822017-04-14 16:34:56 +053032namespace phosphor
33{
34namespace network
35{
Ratan Gupta82549cc2017-04-21 08:45:23 +053036
William A. Kennington IIId41db382021-11-09 20:42:29 -080037extern std::unique_ptr<Timer> refreshObjectTimer;
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080038extern std::unique_ptr<Timer> reloadTimer;
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;
Jiaqing Zhaob685cb62022-04-12 22:57:34 +080041using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta6811f822017-04-14 16:34:56 +053042
Patrick Williamsc38b0712022-07-22 19:26:54 -050043Manager::Manager(sdbusplus::bus_t& bus, const char* objPath,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070044 const fs::path& confDir) :
Patrick Williams166b9592022-03-30 16:09:16 -050045 details::VLANCreateIface(bus, objPath,
46 details::VLANCreateIface::action::defer_emit),
Gunnar Mills57d9c502018-09-14 14:42:34 -050047 bus(bus), objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053048{
Ratan Gupta255d5142017-08-10 09:02:08 +053049 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053050}
51
William A. Kennington III9a1d9af2021-11-09 17:51:05 -080052bool Manager::createDefaultNetworkFiles()
Ratan Guptab610caf2017-09-19 09:33:51 +053053{
54 auto isCreated = false;
55 try
56 {
William A. Kennington III96444792022-10-05 15:16:22 -070057 auto interfaceStrList = getSystemInterfaces();
Ratan Guptab610caf2017-09-19 09:33:51 +053058 for (const auto& interface : interfaceStrList)
59 {
Michael Tritz08c34f42017-10-16 14:59:09 -050060 // 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
William A. Kennington IIIa520a392022-08-08 12:17:34 -070067 fs::path filePath = config::pathForIntfConf(confDir, interface);
Ratan Guptab610caf2017-09-19 09:33:51 +053068
69 // create the interface specific network file
William A. Kennington III9a1d9af2021-11-09 17:51:05 -080070 // if not existing.
William A. Kennington III87f68132022-08-21 02:20:43 -070071 if (!fs::is_regular_file(filePath))
Ratan Guptab610caf2017-09-19 09:33:51 +053072 {
William A. Kennington III87f68132022-08-21 02:20:43 -070073 bmc::writeDHCPDefault(filePath, interface);
Ratan Guptab610caf2017-09-19 09:33:51 +053074 log<level::INFO>("Created the default network file.",
Gunnar Mills57d9c502018-09-14 14:42:34 -050075 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptab610caf2017-09-19 09:33:51 +053076 isCreated = true;
77 }
78 }
79 }
Patrick Williams5758db32021-10-06 12:29:22 -050080 catch (const std::exception& e)
Ratan Guptab610caf2017-09-19 09:33:51 +053081 {
82 log<level::ERR>("Unable to create the default network file");
83 }
Alexander Filippov1ea35992021-03-26 13:10:05 +030084
Ratan Guptab610caf2017-09-19 09:33:51 +053085 return isCreated;
86}
87
Ratan Guptaef85eb92017-06-15 08:57:54 +053088void Manager::setConfDir(const fs::path& dir)
89{
90 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +053091
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 }
Ratan Gupta29b0e432017-05-25 12:51:40 +0530101}
102
103void Manager::createInterfaces()
104{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500105 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530106 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530107
William A. Kennington III96444792022-10-05 15:16:22 -0700108 auto interfaceStrList = getSystemInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530109
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530110 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530111 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530112 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530113 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530114
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 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500121 // it is vlan interface
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700122 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 }
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -0700133 auto it = interfaces.find(interfaceName);
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700134 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);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530142 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530143 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530144 // normal ethernet interface
145 objPath /= interface;
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700146 config::Parser config(config::pathForIntfConf(confDir, interface));
Ratan Gupta34f96d62017-06-15 09:16:22 +0530147
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -0700148 auto intf = std::make_unique<phosphor::network::EthernetInterface>(
William A. Kennington III0caf2212022-08-18 18:15:51 -0700149 bus, objPath.string(), config, *this);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530150
151 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -0800152 intf->createStaticNeighborObjects();
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700153 intf->loadNameServers(config);
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600154 intf->loadNTPServers(config);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530155
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700156 this->interfaces.emplace(std::move(interface), std::move(intf));
Ratan Gupta6811f822017-04-14 16:34:56 +0530157 }
158}
159
Ratan Guptaef85eb92017-06-15 08:57:54 +0530160void Manager::createChildObjects()
161{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700162 routeTable.refresh();
163
Ratan Guptaef85eb92017-06-15 08:57:54 +0530164 // creates the ethernet interface dbus object.
165 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530166
167 systemConf.reset(nullptr);
168 dhcpConf.reset(nullptr);
169
Ratan Guptaef85eb92017-06-15 08:57:54 +0530170 fs::path objPath = objectPath;
171 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530172
173 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530174 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800175 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530176 // create the dhcp conf object.
177 objPath /= "dhcp";
178 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500179 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530180}
181
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700182ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530183{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800184 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
William A. Kennington III96444792022-10-05 15:16:22 -0700192 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);
Ratan Gupta6811f822017-04-14 16:34:56 +0530200}
201
Michael Tritz29f2fd62017-05-22 15:27:26 -0500202void Manager::reset()
203{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800204 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500205 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800206 for (const auto& file : fs::directory_iterator(confDir))
207 {
208 fs::remove(file.path());
209 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500210 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800211 createDefaultNetworkFiles();
212 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500213}
214
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530215// Need to merge the below function with the code which writes the
216// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500217// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530218void Manager::writeToConfigurationFile()
219{
220 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530221 for (const auto& intf : interfaces)
222 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530223 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530224 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530225}
226
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700227#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530228void 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 =
Patrick Williams6aef7692021-05-01 06:39:41 -0500236 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530237 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
William A. Kennington III56ecc782021-10-07 18:44:50 -0700260void Manager::reloadConfigs()
261{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800262 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington IIId41db382021-11-09 20:42:29 -0800263 // Ensure that the next refresh happens after reconfiguration
264 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800265}
266
267void Manager::doReloadConfigs()
268{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800269 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();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700282 try
283 {
284 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
285 NETWORKD_INTERFACE, "Reload");
286 bus.call_noreply(method);
287 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500288 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700289 {
290 log<level::ERR>("Failed to reload configuration",
291 entry("ERR=%s", ex.what()));
292 elog<InternalFailure>();
293 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800294 // Ensure reconfiguration has enough time
295 refreshObjectTimer->setRemaining(refreshTimeout);
William A. Kennington III56ecc782021-10-07 18:44:50 -0700296}
297
Gunnar Mills57d9c502018-09-14 14:42:34 -0500298} // namespace network
299} // namespace phosphor