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 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 5 | #include "config_parser.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 6 | #include "ipaddress.hpp" |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 7 | #include "system_queries.hpp" |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 8 | #include "types.hpp" |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 9 | #include "util.hpp" |
| 10 | |
William A. Kennington III | 57ca961 | 2022-11-14 15:26:47 -0800 | [diff] [blame] | 11 | #include <linux/if_addr.h> |
William A. Kennington III | 7310ac7 | 2022-11-14 15:44:00 -0800 | [diff] [blame] | 12 | #include <linux/neighbour.h> |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 13 | #include <net/if.h> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 14 | |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 15 | #include <filesystem> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 16 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 17 | #include <phosphor-logging/elog-errors.hpp> |
| 18 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 19 | #include <sdbusplus/message.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 20 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 21 | |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 22 | constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1"; |
| 23 | constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1"; |
| 24 | constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager"; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 25 | constexpr auto FirstBootFile = "/var/lib/network/firstBoot_"; |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 26 | |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 27 | constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1"; |
| 28 | constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1"; |
| 29 | constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager"; |
| 30 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 31 | namespace phosphor |
| 32 | { |
| 33 | namespace network |
| 34 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 35 | |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 36 | extern std::unique_ptr<Timer> refreshObjectTimer; |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 37 | extern std::unique_ptr<Timer> reloadTimer; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 38 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 39 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Jiaqing Zhao | b685cb6 | 2022-04-12 22:57:34 +0800 | [diff] [blame] | 40 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 41 | |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 42 | static constexpr const char enabledMatch[] = |
| 43 | "type='signal',sender='org.freedesktop.network1',path_namespace='/org/" |
| 44 | "freedesktop/network1/" |
| 45 | "link',interface='org.freedesktop.DBus.Properties',member='" |
| 46 | "PropertiesChanged',arg0='org.freedesktop.network1.Link',"; |
| 47 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 48 | Manager::Manager(sdbusplus::bus_t& bus, const char* objPath, |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 49 | const fs::path& confDir) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 50 | details::VLANCreateIface(bus, objPath, |
| 51 | details::VLANCreateIface::action::defer_emit), |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 52 | bus(bus), objectPath(objPath), |
| 53 | systemdNetworkdEnabledMatch( |
| 54 | bus, enabledMatch, [&](sdbusplus::message_t& m) { |
| 55 | std::string intf; |
| 56 | std::unordered_map<std::string, std::variant<std::string>> values; |
| 57 | try |
| 58 | { |
| 59 | m.read(intf, values); |
| 60 | auto it = values.find("AdministrativeState"); |
| 61 | if (it == values.end()) |
| 62 | { |
| 63 | return; |
| 64 | } |
| 65 | const std::string_view obj = m.get_path(); |
| 66 | auto sep = obj.rfind('/'); |
| 67 | if (sep == obj.npos || sep + 3 > obj.size()) |
| 68 | { |
| 69 | throw std::invalid_argument("Invalid obj path"); |
| 70 | } |
| 71 | auto ifidx = DecodeInt<unsigned, 10>{}(obj.substr(sep + 3)); |
| 72 | const auto& state = std::get<std::string>(it->second); |
| 73 | handleAdminState(state, ifidx); |
| 74 | } |
| 75 | catch (const std::exception& e) |
| 76 | { |
| 77 | log<level::ERR>( |
| 78 | fmt::format("AdministrativeState match parsing failed: {}", |
| 79 | e.what()) |
| 80 | .c_str(), |
| 81 | entry("ERROR=%s", e.what())); |
| 82 | } |
| 83 | }) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 84 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 85 | setConfDir(confDir); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 86 | std::vector< |
| 87 | std::tuple<int32_t, std::string, sdbusplus::message::object_path>> |
| 88 | links; |
| 89 | try |
| 90 | { |
| 91 | auto rsp = |
| 92 | bus.new_method_call("org.freedesktop.network1", |
| 93 | "/org/freedesktop/network1", |
| 94 | "org.freedesktop.network1.Manager", "ListLinks") |
| 95 | .call(); |
| 96 | rsp.read(links); |
| 97 | } |
| 98 | catch (const sdbusplus::exception::SdBusError& e) |
| 99 | { |
| 100 | // Any failures are systemd-network not being ready |
| 101 | } |
| 102 | for (const auto& link : links) |
| 103 | { |
| 104 | unsigned ifidx = std::get<0>(link); |
| 105 | auto obj = fmt::format("/org/freedesktop/network1/link/_3{}", ifidx); |
| 106 | auto req = |
| 107 | bus.new_method_call("org.freedesktop.network1", obj.c_str(), |
| 108 | "org.freedesktop.DBus.Properties", "Get"); |
| 109 | req.append("org.freedesktop.network1.Link", "AdministrativeState"); |
| 110 | auto rsp = req.call(); |
| 111 | std::variant<std::string> val; |
| 112 | rsp.read(val); |
| 113 | handleAdminState(std::get<std::string>(val), ifidx); |
| 114 | } |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void Manager::setConfDir(const fs::path& dir) |
| 118 | { |
| 119 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 120 | |
| 121 | if (!fs::exists(confDir)) |
| 122 | { |
| 123 | if (!fs::create_directories(confDir)) |
| 124 | { |
| 125 | log<level::ERR>("Unable to create the network conf dir", |
| 126 | entry("DIR=%s", confDir.c_str())); |
| 127 | elog<InternalFailure>(); |
| 128 | } |
| 129 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 130 | } |
| 131 | |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 132 | void Manager::createInterface(const UndiscoveredInfo& info, bool enabled) |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 133 | { |
William A. Kennington III | 3ee5b7e | 2022-11-15 15:04:37 -0800 | [diff] [blame^] | 134 | if (!info.intf.name) |
| 135 | { |
| 136 | auto msg = fmt::format("Can't create interface without name: {}", |
| 137 | info.intf.idx); |
| 138 | log<level::ERR>(msg.c_str(), entry("IFIDX=%u", info.intf.idx)); |
| 139 | return; |
| 140 | } |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 141 | removeInterface(info.intf); |
| 142 | config::Parser config(config::pathForIntfConf(confDir, *info.intf.name)); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 143 | auto intf = std::make_unique<EthernetInterface>( |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 144 | bus, *this, info.intf, objectPath, config, true, enabled); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 145 | intf->createIPAddressObjects(); |
| 146 | intf->createStaticNeighborObjects(); |
| 147 | intf->loadNameServers(config); |
| 148 | intf->loadNTPServers(config); |
| 149 | auto ptr = intf.get(); |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 150 | interfaces.insert_or_assign(*info.intf.name, std::move(intf)); |
| 151 | interfacesByIdx.insert_or_assign(info.intf.idx, ptr); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 152 | } |
| 153 | |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 154 | void Manager::addInterface(const InterfaceInfo& info) |
| 155 | { |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 156 | if (info.flags & IFF_LOOPBACK) |
| 157 | { |
William A. Kennington III | 3ee5b7e | 2022-11-15 15:04:37 -0800 | [diff] [blame^] | 158 | ignoredIntf.emplace(info.idx); |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 159 | return; |
| 160 | } |
William A. Kennington III | 3ee5b7e | 2022-11-15 15:04:37 -0800 | [diff] [blame^] | 161 | if (info.name) |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 162 | { |
William A. Kennington III | 3ee5b7e | 2022-11-15 15:04:37 -0800 | [diff] [blame^] | 163 | const auto& ignored = internal::getIgnoredInterfaces(); |
| 164 | if (ignored.find(*info.name) != ignored.end()) |
| 165 | { |
| 166 | static std::unordered_set<std::string> ignored; |
| 167 | if (!ignored.contains(*info.name)) |
| 168 | { |
| 169 | ignored.emplace(*info.name); |
| 170 | auto msg = fmt::format("Ignoring interface {}\n", *info.name); |
| 171 | log<level::INFO>(msg.c_str()); |
| 172 | } |
| 173 | ignoredIntf.emplace(info.idx); |
| 174 | return; |
| 175 | } |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 176 | } |
| 177 | |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 178 | auto it = systemdNetworkdEnabled.find(info.idx); |
| 179 | if (it != systemdNetworkdEnabled.end()) |
| 180 | { |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 181 | createInterface({info}, it->second); |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 185 | undiscoveredIntfInfo.insert_or_assign( |
| 186 | info.idx, UndiscoveredInfo{std::move(info)}); |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | void Manager::removeInterface(const InterfaceInfo& info) |
| 191 | { |
| 192 | auto iit = interfacesByIdx.find(info.idx); |
| 193 | auto nit = interfaces.end(); |
| 194 | if (info.name) |
| 195 | { |
| 196 | nit = interfaces.find(*info.name); |
| 197 | if (nit != interfaces.end() && iit != interfacesByIdx.end() && |
| 198 | nit->second.get() != iit->second) |
| 199 | { |
| 200 | fmt::print(stderr, "Removed interface desync detected\n"); |
| 201 | fflush(stderr); |
| 202 | std::abort(); |
| 203 | } |
| 204 | } |
| 205 | else if (iit != interfacesByIdx.end()) |
| 206 | { |
| 207 | for (nit = interfaces.begin(); nit != interfaces.end(); ++nit) |
| 208 | { |
| 209 | if (nit->second.get() == iit->second) |
| 210 | { |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (iit != interfacesByIdx.end()) |
| 217 | { |
| 218 | interfacesByIdx.erase(iit); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | undiscoveredIntfInfo.erase(info.idx); |
William A. Kennington III | 3ee5b7e | 2022-11-15 15:04:37 -0800 | [diff] [blame^] | 223 | ignoredIntf.erase(info.idx); |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 224 | } |
| 225 | if (nit != interfaces.end()) |
| 226 | { |
| 227 | interfaces.erase(nit); |
| 228 | } |
| 229 | } |
| 230 | |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 231 | void Manager::addAddress(const AddressInfo& info) |
| 232 | { |
William A. Kennington III | 57ca961 | 2022-11-14 15:26:47 -0800 | [diff] [blame] | 233 | if (info.flags & IFA_F_DEPRECATED) |
| 234 | { |
| 235 | return; |
| 236 | } |
| 237 | if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end()) |
| 238 | { |
| 239 | it->second->addAddr(info); |
| 240 | } |
| 241 | else if (auto it = undiscoveredIntfInfo.find(info.ifidx); |
| 242 | it != undiscoveredIntfInfo.end()) |
| 243 | { |
| 244 | it->second.addrs.insert_or_assign(info.ifaddr, info); |
| 245 | } |
William A. Kennington III | 3ee5b7e | 2022-11-15 15:04:37 -0800 | [diff] [blame^] | 246 | else if (!ignoredIntf.contains(info.ifidx)) |
William A. Kennington III | 57ca961 | 2022-11-14 15:26:47 -0800 | [diff] [blame] | 247 | { |
| 248 | throw std::runtime_error( |
| 249 | fmt::format("Interface `{}` not found for addr", info.ifidx)); |
| 250 | } |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void Manager::removeAddress(const AddressInfo& info) |
| 254 | { |
William A. Kennington III | 57ca961 | 2022-11-14 15:26:47 -0800 | [diff] [blame] | 255 | if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end()) |
| 256 | { |
| 257 | it->second->addrs.erase(info.ifaddr); |
| 258 | } |
| 259 | else if (auto it = undiscoveredIntfInfo.find(info.ifidx); |
| 260 | it != undiscoveredIntfInfo.end()) |
| 261 | { |
| 262 | it->second.addrs.erase(info.ifaddr); |
| 263 | } |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void Manager::addNeighbor(const NeighborInfo& info) |
| 267 | { |
William A. Kennington III | 7310ac7 | 2022-11-14 15:44:00 -0800 | [diff] [blame] | 268 | if (!(info.state & NUD_PERMANENT) || !info.addr) |
| 269 | { |
| 270 | return; |
| 271 | } |
| 272 | if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end()) |
| 273 | { |
| 274 | it->second->addStaticNeigh(info); |
| 275 | } |
| 276 | else if (auto it = undiscoveredIntfInfo.find(info.ifidx); |
| 277 | it != undiscoveredIntfInfo.end()) |
| 278 | { |
| 279 | it->second.staticNeighs.insert_or_assign(*info.addr, info); |
| 280 | } |
William A. Kennington III | 3ee5b7e | 2022-11-15 15:04:37 -0800 | [diff] [blame^] | 281 | else if (!ignoredIntf.contains(info.ifidx)) |
William A. Kennington III | 7310ac7 | 2022-11-14 15:44:00 -0800 | [diff] [blame] | 282 | { |
| 283 | throw std::runtime_error( |
| 284 | fmt::format("Interface `{}` not found for neigh", info.ifidx)); |
| 285 | } |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void Manager::removeNeighbor(const NeighborInfo& info) |
| 289 | { |
William A. Kennington III | 7310ac7 | 2022-11-14 15:44:00 -0800 | [diff] [blame] | 290 | if (!info.addr) |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 291 | { |
William A. Kennington III | 7310ac7 | 2022-11-14 15:44:00 -0800 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end()) |
| 295 | { |
| 296 | it->second->staticNeighbors.erase(*info.addr); |
| 297 | } |
| 298 | else if (auto it = undiscoveredIntfInfo.find(info.ifidx); |
| 299 | it != undiscoveredIntfInfo.end()) |
| 300 | { |
| 301 | it->second.staticNeighs.erase(*info.addr); |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | void Manager::addDefGw(unsigned ifidx, InAddrAny addr) |
| 306 | { |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 307 | if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end()) |
| 308 | { |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 309 | std::visit( |
| 310 | [&](auto addr) { |
| 311 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 312 | { |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 313 | it->second->EthernetInterfaceIntf::defaultGateway( |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 314 | std::to_string(addr)); |
| 315 | } |
| 316 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 317 | { |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 318 | it->second->EthernetInterfaceIntf::defaultGateway6( |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 319 | std::to_string(addr)); |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 324 | } |
| 325 | }, |
| 326 | addr); |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 327 | } |
| 328 | else if (auto it = undiscoveredIntfInfo.find(ifidx); |
| 329 | it != undiscoveredIntfInfo.end()) |
| 330 | { |
| 331 | std::visit( |
| 332 | [&](auto addr) { |
| 333 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 334 | { |
| 335 | it->second.defgw4.emplace(addr); |
| 336 | } |
| 337 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 338 | { |
| 339 | it->second.defgw6.emplace(addr); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 344 | } |
| 345 | }, |
| 346 | addr); |
| 347 | } |
William A. Kennington III | 3ee5b7e | 2022-11-15 15:04:37 -0800 | [diff] [blame^] | 348 | else if (!ignoredIntf.contains(ifidx)) |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 349 | { |
| 350 | auto msg = fmt::format("Interface `{}` not found for gw", ifidx); |
| 351 | log<level::ERR>(msg.c_str(), entry("IFIDX=%u", ifidx)); |
| 352 | } |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void Manager::removeDefGw(unsigned ifidx, InAddrAny addr) |
| 356 | { |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 357 | if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end()) |
| 358 | { |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 359 | std::visit( |
| 360 | [&](auto addr) { |
| 361 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 362 | { |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 363 | if (it->second->defaultGateway() == std::to_string(addr)) |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 364 | { |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 365 | it->second->EthernetInterfaceIntf::defaultGateway(""); |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 369 | { |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 370 | if (it->second->defaultGateway6() == std::to_string(addr)) |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 371 | { |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 372 | it->second->EthernetInterfaceIntf::defaultGateway6(""); |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 378 | } |
| 379 | }, |
| 380 | addr); |
William A. Kennington III | bb9e909 | 2022-11-14 15:58:02 -0800 | [diff] [blame] | 381 | } |
| 382 | else if (auto it = undiscoveredIntfInfo.find(ifidx); |
| 383 | it != undiscoveredIntfInfo.end()) |
| 384 | { |
| 385 | std::visit( |
| 386 | [&](auto addr) { |
| 387 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 388 | { |
| 389 | if (it->second.defgw4 == addr) |
| 390 | { |
| 391 | it->second.defgw4.reset(); |
| 392 | } |
| 393 | } |
| 394 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 395 | { |
| 396 | if (it->second.defgw6 == addr) |
| 397 | { |
| 398 | it->second.defgw6.reset(); |
| 399 | } |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 404 | } |
| 405 | }, |
| 406 | addr); |
| 407 | } |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 410 | void Manager::createInterfaces() |
| 411 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 412 | // clear all the interfaces first |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 413 | interfaces.clear(); |
William A. Kennington III | 67b09da | 2022-10-31 14:09:53 -0700 | [diff] [blame] | 414 | interfacesByIdx.clear(); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 415 | for (auto& info : system::getInterfaces()) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 416 | { |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 417 | addInterface(info); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 421 | void Manager::createChildObjects() |
| 422 | { |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 423 | routeTable.refresh(); |
| 424 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 425 | // creates the ethernet interface dbus object. |
| 426 | createInterfaces(); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 427 | |
| 428 | systemConf.reset(nullptr); |
| 429 | dhcpConf.reset(nullptr); |
| 430 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 431 | fs::path objPath = objectPath; |
| 432 | objPath /= "config"; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 433 | |
| 434 | // create the system conf object. |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 435 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
Jiaqing Zhao | 24b5a61 | 2022-04-11 16:46:16 +0800 | [diff] [blame] | 436 | bus, objPath.string()); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 437 | // create the dhcp conf object. |
| 438 | objPath /= "dhcp"; |
| 439 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 440 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 441 | } |
| 442 | |
William A. Kennington III | 085bbdc | 2022-10-05 02:45:37 -0700 | [diff] [blame] | 443 | ObjectPath Manager::vlan(std::string interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 444 | { |
Jiaqing Zhao | b685cb6 | 2022-04-12 22:57:34 +0800 | [diff] [blame] | 445 | if (id == 0 || id >= 4095) |
| 446 | { |
| 447 | log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id)); |
| 448 | elog<InvalidArgument>( |
| 449 | Argument::ARGUMENT_NAME("VLANId"), |
| 450 | Argument::ARGUMENT_VALUE(std::to_string(id).c_str())); |
| 451 | } |
| 452 | |
William A. Kennington III | 9644479 | 2022-10-05 15:16:22 -0700 | [diff] [blame] | 453 | auto it = interfaces.find(interfaceName); |
| 454 | if (it == interfaces.end()) |
| 455 | { |
| 456 | using ResourceErr = |
| 457 | phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound; |
| 458 | elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str())); |
| 459 | } |
| 460 | return it->second->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 461 | } |
| 462 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 463 | void Manager::reset() |
| 464 | { |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 465 | if (fs::is_directory(confDir)) |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 466 | { |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 467 | for (const auto& file : fs::directory_iterator(confDir)) |
| 468 | { |
| 469 | fs::remove(file.path()); |
| 470 | } |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 471 | } |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 472 | log<level::INFO>("Network Factory Reset queued."); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 473 | } |
| 474 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 475 | // Need to merge the below function with the code which writes the |
| 476 | // config file during factory reset. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 477 | // TODO openbmc/openbmc#1751 |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 478 | void Manager::writeToConfigurationFile() |
| 479 | { |
| 480 | // write all the static ip address in the systemd-network conf file |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 481 | for (const auto& intf : interfaces) |
| 482 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 483 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 484 | } |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 485 | } |
| 486 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 487 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 488 | void Manager::setFistBootMACOnInterface( |
| 489 | const std::pair<std::string, std::string>& inventoryEthPair) |
| 490 | { |
| 491 | for (const auto& interface : interfaces) |
| 492 | { |
| 493 | if (interface.first == inventoryEthPair.first) |
| 494 | { |
| 495 | auto returnMAC = |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 496 | interface.second->macAddress(inventoryEthPair.second); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 497 | if (returnMAC == inventoryEthPair.second) |
| 498 | { |
| 499 | log<level::INFO>("Set the MAC on "), |
| 500 | entry("interface : ", interface.first.c_str()), |
| 501 | entry("MAC : ", inventoryEthPair.second.c_str()); |
| 502 | std::error_code ec; |
| 503 | if (std::filesystem::is_directory("/var/lib/network", ec)) |
| 504 | { |
| 505 | std::ofstream persistentFile(FirstBootFile + |
| 506 | interface.first); |
| 507 | } |
| 508 | break; |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | log<level::INFO>("MAC is Not Set on ethernet Interface"); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | #endif |
| 519 | |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 520 | void Manager::reloadConfigsNoRefresh() |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 521 | { |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 522 | reloadTimer->restartOnce(reloadTimeout); |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void Manager::reloadConfigs() |
| 526 | { |
| 527 | reloadConfigsNoRefresh(); |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 528 | // Ensure that the next refresh happens after reconfiguration |
| 529 | refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout); |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void Manager::doReloadConfigs() |
| 533 | { |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 534 | for (auto& hook : reloadPreHooks) |
| 535 | { |
| 536 | try |
| 537 | { |
| 538 | hook(); |
| 539 | } |
| 540 | catch (const std::exception& ex) |
| 541 | { |
| 542 | log<level::ERR>("Failed executing reload hook, ignoring", |
| 543 | entry("ERR=%s", ex.what())); |
| 544 | } |
| 545 | } |
| 546 | reloadPreHooks.clear(); |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 547 | try |
| 548 | { |
| 549 | auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH, |
| 550 | NETWORKD_INTERFACE, "Reload"); |
| 551 | bus.call_noreply(method); |
| 552 | } |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 553 | catch (const sdbusplus::exception_t& ex) |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 554 | { |
| 555 | log<level::ERR>("Failed to reload configuration", |
| 556 | entry("ERR=%s", ex.what())); |
| 557 | elog<InternalFailure>(); |
| 558 | } |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 559 | // Ensure reconfiguration has enough time |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 560 | if (refreshObjectTimer->isEnabled()) |
| 561 | { |
| 562 | refreshObjectTimer->setRemaining(refreshTimeout); |
| 563 | } |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 564 | } |
| 565 | |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 566 | void Manager::handleAdminState(std::string_view state, unsigned ifidx) |
| 567 | { |
| 568 | if (state == "initialized" || state == "linger") |
| 569 | { |
| 570 | systemdNetworkdEnabled.erase(ifidx); |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | bool managed = state != "unmanaged"; |
| 575 | systemdNetworkdEnabled.insert_or_assign(ifidx, managed); |
| 576 | if (auto it = undiscoveredIntfInfo.find(ifidx); |
| 577 | it != undiscoveredIntfInfo.end()) |
| 578 | { |
| 579 | auto info = std::move(it->second); |
| 580 | undiscoveredIntfInfo.erase(it); |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 581 | createInterface(info, managed); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 582 | } |
| 583 | else if (auto it = interfacesByIdx.find(ifidx); |
| 584 | it != interfacesByIdx.end()) |
| 585 | { |
| 586 | it->second->EthernetInterfaceIntf::nicEnabled(managed); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 591 | } // namespace network |
| 592 | } // namespace phosphor |