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