Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 3 | #include "network_manager.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 5 | #include "ipaddress.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | #include "network_config.hpp" |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 7 | #include "types.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include "util.hpp" |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 9 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 10 | #include <arpa/inet.h> |
| 11 | #include <dirent.h> |
| 12 | #include <net/if.h> |
| 13 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <bitset> |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 16 | #include <filesystem> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 17 | #include <fstream> |
| 18 | #include <map> |
| 19 | #include <phosphor-logging/elog-errors.hpp> |
| 20 | #include <phosphor-logging/log.hpp> |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 21 | #include <string> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 22 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 23 | |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 24 | constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1"; |
| 25 | constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1"; |
| 26 | constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager"; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 27 | constexpr auto FirstBootFile = "/var/lib/network/firstBoot_"; |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 28 | |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 29 | constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1"; |
| 30 | constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1"; |
| 31 | constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager"; |
| 32 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 33 | namespace phosphor |
| 34 | { |
| 35 | namespace network |
| 36 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 37 | |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 38 | extern std::unique_ptr<Timer> reloadTimer; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 39 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 40 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 41 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 42 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath, |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | const std::string& path) : |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 44 | details::VLANCreateIface(bus, objPath, true), |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 45 | bus(bus), objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 46 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 47 | fs::path confDir(path); |
| 48 | setConfDir(confDir); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 49 | } |
| 50 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 51 | bool 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 Tritz | 08c34f4 | 2017-10-16 14:59:09 -0500 | [diff] [blame] | 75 | // 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 Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 82 | auto fileName = systemd::config::networkFilePrefix + interface + |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 83 | systemd::config::networkFileSuffix; |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 84 | |
| 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 96 | entry("INTERFACE=%s", interface.c_str())); |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 97 | isCreated = true; |
| 98 | } |
| 99 | } |
| 100 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 101 | catch (const std::exception& e) |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 102 | { |
| 103 | log<level::ERR>("Unable to create the default network file"); |
| 104 | } |
Alexander Filippov | 1ea3599 | 2021-03-26 13:10:05 +0300 | [diff] [blame] | 105 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 106 | return isCreated; |
| 107 | } |
| 108 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 109 | void Manager::setConfDir(const fs::path& dir) |
| 110 | { |
| 111 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 112 | |
| 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 Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void Manager::createInterfaces() |
| 125 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 126 | // clear all the interfaces first |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 127 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 128 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 129 | auto interfaceStrList = getInterfaces(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 130 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 131 | for (auto& interface : interfaceStrList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 132 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 133 | fs::path objPath = objectPath; |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 134 | auto index = interface.find("."); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 135 | |
| 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 142 | // it is vlan interface |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 143 | auto interfaceName = interface.substr(0, index); |
| 144 | auto vlanid = interface.substr(index + 1); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 145 | uint32_t vlanInt = std::stoul(vlanid); |
| 146 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 147 | interfaces[interfaceName]->loadVLAN(vlanInt); |
Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 148 | continue; |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 149 | } |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 150 | // normal ethernet interface |
| 151 | objPath /= interface; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 152 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 153 | auto dhcp = getDHCPValue(confDir, interface); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 154 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 155 | auto intf = std::make_shared<phosphor::network::EthernetInterface>( |
| 156 | bus, objPath.string(), dhcp, *this); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 157 | |
| 158 | intf->createIPAddressObjects(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 159 | intf->createStaticNeighborObjects(); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 160 | intf->loadNameServers(); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 161 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 162 | this->interfaces.emplace( |
| 163 | std::make_pair(std::move(interface), std::move(intf))); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 167 | void Manager::createChildObjects() |
| 168 | { |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 169 | routeTable.refresh(); |
| 170 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 171 | // creates the ethernet interface dbus object. |
| 172 | createInterfaces(); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 173 | |
| 174 | systemConf.reset(nullptr); |
| 175 | dhcpConf.reset(nullptr); |
| 176 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 177 | fs::path objPath = objectPath; |
| 178 | objPath /= "config"; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 179 | |
| 180 | // create the system conf object. |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 181 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 182 | bus, objPath.string(), *this); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 183 | // create the dhcp conf object. |
| 184 | objPath /= "dhcp"; |
| 185 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 186 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 187 | } |
| 188 | |
Patrick Williams | 8173e02 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 189 | ObjectPath Manager::vlan(IntfName interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 190 | { |
sureshvijayv1 | ee2cba8 | 2021-11-18 15:28:50 +0530 | [diff] [blame] | 191 | if (!hasInterface(interfaceName)) |
| 192 | { |
Patrick Williams | 9739984 | 2022-03-16 14:14:02 -0500 | [diff] [blame] | 193 | using ResourceErr = |
| 194 | phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound; |
| 195 | elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str())); |
sureshvijayv1 | ee2cba8 | 2021-11-18 15:28:50 +0530 | [diff] [blame] | 196 | } |
| 197 | |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 198 | return interfaces[interfaceName]->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 199 | } |
| 200 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 201 | void Manager::reset() |
| 202 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 203 | if (!createDefaultNetworkFiles(true)) |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 204 | { |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 205 | log<level::ERR>("Network Factory Reset failed."); |
| 206 | return; |
| 207 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 210 | log<level::INFO>("Network Factory Reset done."); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 213 | // Need to merge the below function with the code which writes the |
| 214 | // config file during factory reset. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 215 | // TODO openbmc/openbmc#1751 |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 216 | void Manager::writeToConfigurationFile() |
| 217 | { |
| 218 | // write all the static ip address in the systemd-network conf file |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 219 | for (const auto& intf : interfaces) |
| 220 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 221 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 222 | } |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 223 | } |
| 224 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 225 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 226 | void Manager::setFistBootMACOnInterface( |
| 227 | const std::pair<std::string, std::string>& inventoryEthPair) |
| 228 | { |
| 229 | for (const auto& interface : interfaces) |
| 230 | { |
| 231 | if (interface.first == inventoryEthPair.first) |
| 232 | { |
| 233 | auto returnMAC = |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 234 | interface.second->macAddress(inventoryEthPair.second); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 235 | if (returnMAC == inventoryEthPair.second) |
| 236 | { |
| 237 | log<level::INFO>("Set the MAC on "), |
| 238 | entry("interface : ", interface.first.c_str()), |
| 239 | entry("MAC : ", inventoryEthPair.second.c_str()); |
| 240 | std::error_code ec; |
| 241 | if (std::filesystem::is_directory("/var/lib/network", ec)) |
| 242 | { |
| 243 | std::ofstream persistentFile(FirstBootFile + |
| 244 | interface.first); |
| 245 | } |
| 246 | break; |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | log<level::INFO>("MAC is Not Set on ethernet Interface"); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | #endif |
| 257 | |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 258 | void Manager::reloadConfigs() |
| 259 | { |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 260 | reloadTimer->restartOnce(reloadTimeout); |
| 261 | } |
| 262 | |
| 263 | void Manager::doReloadConfigs() |
| 264 | { |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 265 | for (auto& hook : reloadPreHooks) |
| 266 | { |
| 267 | try |
| 268 | { |
| 269 | hook(); |
| 270 | } |
| 271 | catch (const std::exception& ex) |
| 272 | { |
| 273 | log<level::ERR>("Failed executing reload hook, ignoring", |
| 274 | entry("ERR=%s", ex.what())); |
| 275 | } |
| 276 | } |
| 277 | reloadPreHooks.clear(); |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 278 | try |
| 279 | { |
| 280 | auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH, |
| 281 | NETWORKD_INTERFACE, "Reload"); |
| 282 | bus.call_noreply(method); |
| 283 | } |
| 284 | catch (const sdbusplus::exception::exception& ex) |
| 285 | { |
| 286 | log<level::ERR>("Failed to reload configuration", |
| 287 | entry("ERR=%s", ex.what())); |
| 288 | elog<InternalFailure>(); |
| 289 | } |
| 290 | } |
| 291 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 292 | } // namespace network |
| 293 | } // namespace phosphor |