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 <bitset> 16 #include <filesystem> 17 #include <fstream> 18 #include <map> 19 #include <phosphor-logging/elog-errors.hpp> 20 #include <phosphor-logging/log.hpp> 21 #include <string> 22 #include <xyz/openbmc_project/Common/error.hpp> 23 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"; 27 constexpr auto FirstBootFile = "/var/lib/network/firstBoot_"; 28 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 33 namespace phosphor 34 { 35 namespace network 36 { 37 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(bool force) 54 { 55 auto isCreated = false; 56 try 57 { 58 // Directory would have created before with 59 // setConfDir function. 60 if (force) 61 { 62 // Factory Reset case 63 // we need to forcefully write the files 64 // so delete the existing ones. 65 if (fs::is_directory(confDir)) 66 { 67 for (const auto& file : fs::directory_iterator(confDir)) 68 { 69 fs::remove(file.path()); 70 } 71 } 72 } 73 74 auto interfaceStrList = getInterfaces(); 75 for (const auto& interface : interfaceStrList) 76 { 77 // if the interface has '.' in the name, it means that this is a 78 // VLAN - don't create the network file. 79 if (interface.find(".") != std::string::npos) 80 { 81 continue; 82 } 83 84 auto fileName = systemd::config::networkFilePrefix + interface + 85 systemd::config::networkFileSuffix; 86 87 fs::path filePath = confDir; 88 filePath /= fileName; 89 90 // create the interface specific network file 91 // if not exist or we forcefully wants to write 92 // the network file. 93 94 if (force || !fs::is_regular_file(filePath.string())) 95 { 96 bmc::writeDHCPDefault(filePath.string(), interface); 97 log<level::INFO>("Created the default network file.", 98 entry("INTERFACE=%s", interface.c_str())); 99 isCreated = true; 100 } 101 } 102 } 103 catch (const std::exception& e) 104 { 105 log<level::ERR>("Unable to create the default network file"); 106 } 107 108 return isCreated; 109 } 110 111 void Manager::setConfDir(const fs::path& dir) 112 { 113 confDir = dir; 114 115 if (!fs::exists(confDir)) 116 { 117 if (!fs::create_directories(confDir)) 118 { 119 log<level::ERR>("Unable to create the network conf dir", 120 entry("DIR=%s", confDir.c_str())); 121 elog<InternalFailure>(); 122 } 123 } 124 } 125 126 void Manager::createInterfaces() 127 { 128 // clear all the interfaces first 129 interfaces.clear(); 130 131 auto interfaceStrList = getInterfaces(); 132 133 for (auto& interface : interfaceStrList) 134 { 135 fs::path objPath = objectPath; 136 auto index = interface.find("."); 137 138 // interface can be of vlan type or normal ethernet interface. 139 // vlan interface looks like "interface.vlanid",so here by looking 140 // at the interface name we decide that we need 141 // to create the vlaninterface or normal physical interface. 142 if (index != std::string::npos) 143 { 144 // it is vlan interface 145 auto interfaceName = interface.substr(0, index); 146 auto vlanid = interface.substr(index + 1); 147 uint32_t vlanInt = std::stoul(vlanid); 148 149 interfaces[interfaceName]->loadVLAN(vlanInt); 150 continue; 151 } 152 // normal ethernet interface 153 objPath /= interface; 154 155 auto dhcp = getDHCPValue(confDir, interface); 156 157 auto intf = std::make_shared<phosphor::network::EthernetInterface>( 158 bus, objPath.string(), dhcp, *this); 159 160 intf->createIPAddressObjects(); 161 intf->createStaticNeighborObjects(); 162 intf->loadNameServers(); 163 164 this->interfaces.emplace( 165 std::make_pair(std::move(interface), std::move(intf))); 166 } 167 } 168 169 void Manager::createChildObjects() 170 { 171 routeTable.refresh(); 172 173 // creates the ethernet interface dbus object. 174 createInterfaces(); 175 176 systemConf.reset(nullptr); 177 dhcpConf.reset(nullptr); 178 179 fs::path objPath = objectPath; 180 objPath /= "config"; 181 182 // create the system conf object. 183 systemConf = std::make_unique<phosphor::network::SystemConfiguration>( 184 bus, objPath.string()); 185 // create the dhcp conf object. 186 objPath /= "dhcp"; 187 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( 188 bus, objPath.string(), *this); 189 } 190 191 ObjectPath Manager::vlan(IntfName interfaceName, uint32_t id) 192 { 193 if (!hasInterface(interfaceName)) 194 { 195 using ResourceErr = 196 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound; 197 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str())); 198 } 199 200 if (id == 0 || id >= 4095) 201 { 202 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id)); 203 elog<InvalidArgument>( 204 Argument::ARGUMENT_NAME("VLANId"), 205 Argument::ARGUMENT_VALUE(std::to_string(id).c_str())); 206 } 207 208 return interfaces[interfaceName]->createVLAN(id); 209 } 210 211 void Manager::reset() 212 { 213 if (!createDefaultNetworkFiles(true)) 214 { 215 log<level::ERR>("Network Factory Reset failed."); 216 return; 217 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. 218 } 219 220 log<level::INFO>("Network Factory Reset done."); 221 } 222 223 // Need to merge the below function with the code which writes the 224 // config file during factory reset. 225 // TODO openbmc/openbmc#1751 226 void Manager::writeToConfigurationFile() 227 { 228 // write all the static ip address in the systemd-network conf file 229 for (const auto& intf : interfaces) 230 { 231 intf.second->writeConfigurationFile(); 232 } 233 } 234 235 #ifdef SYNC_MAC_FROM_INVENTORY 236 void Manager::setFistBootMACOnInterface( 237 const std::pair<std::string, std::string>& inventoryEthPair) 238 { 239 for (const auto& interface : interfaces) 240 { 241 if (interface.first == inventoryEthPair.first) 242 { 243 auto returnMAC = 244 interface.second->macAddress(inventoryEthPair.second); 245 if (returnMAC == inventoryEthPair.second) 246 { 247 log<level::INFO>("Set the MAC on "), 248 entry("interface : ", interface.first.c_str()), 249 entry("MAC : ", inventoryEthPair.second.c_str()); 250 std::error_code ec; 251 if (std::filesystem::is_directory("/var/lib/network", ec)) 252 { 253 std::ofstream persistentFile(FirstBootFile + 254 interface.first); 255 } 256 break; 257 } 258 else 259 { 260 log<level::INFO>("MAC is Not Set on ethernet Interface"); 261 } 262 } 263 } 264 } 265 266 #endif 267 268 void Manager::reloadConfigs() 269 { 270 reloadTimer->restartOnce(reloadTimeout); 271 } 272 273 void Manager::doReloadConfigs() 274 { 275 for (auto& hook : reloadPreHooks) 276 { 277 try 278 { 279 hook(); 280 } 281 catch (const std::exception& ex) 282 { 283 log<level::ERR>("Failed executing reload hook, ignoring", 284 entry("ERR=%s", ex.what())); 285 } 286 } 287 reloadPreHooks.clear(); 288 try 289 { 290 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH, 291 NETWORKD_INTERFACE, "Reload"); 292 bus.call_noreply(method); 293 } 294 catch (const sdbusplus::exception_t& ex) 295 { 296 log<level::ERR>("Failed to reload configuration", 297 entry("ERR=%s", ex.what())); 298 elog<InternalFailure>(); 299 } 300 } 301 302 } // namespace network 303 } // namespace phosphor 304