Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | #include "ethernet_interface.hpp" |
| 4 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 5 | #include "config_parser.hpp" |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 6 | #include "network_manager.hpp" |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 7 | #include "system_queries.hpp" |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 8 | #include "util.hpp" |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 9 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 10 | #include <fmt/compile.h> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 11 | #include <fmt/format.h> |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 12 | #include <linux/rtnetlink.h> |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 13 | #include <net/if.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 14 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 15 | #include <algorithm> |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 16 | #include <filesystem> |
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 | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 19 | #include <stdplus/raw.hpp> |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 20 | #include <stdplus/zstring.hpp> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 21 | #include <string> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 22 | #include <unordered_map> |
| 23 | #include <variant> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 24 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 25 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 26 | namespace phosphor |
| 27 | { |
| 28 | namespace network |
| 29 | { |
| 30 | |
| 31 | using namespace phosphor::logging; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 32 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 33 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 34 | using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 35 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
William A. Kennington III | 991a8e8 | 2022-10-11 15:02:47 -0700 | [diff] [blame] | 36 | using std::literals::string_view_literals::operator""sv; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 37 | constexpr auto RESOLVED_SERVICE = "org.freedesktop.resolve1"; |
| 38 | constexpr auto RESOLVED_INTERFACE = "org.freedesktop.resolve1.Link"; |
| 39 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 40 | constexpr auto RESOLVED_SERVICE_PATH = "/org/freedesktop/resolve1/link/"; |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 41 | |
| 42 | constexpr auto TIMESYNCD_SERVICE = "org.freedesktop.timesync1"; |
| 43 | constexpr auto TIMESYNCD_INTERFACE = "org.freedesktop.timesync1.Manager"; |
| 44 | constexpr auto TIMESYNCD_SERVICE_PATH = "/org/freedesktop/timesync1"; |
| 45 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 46 | constexpr auto METHOD_GET = "Get"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 47 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 48 | template <typename Func> |
| 49 | inline decltype(std::declval<Func>()()) |
| 50 | ignoreError(std::string_view msg, stdplus::zstring_view intf, |
| 51 | decltype(std::declval<Func>()()) fallback, Func&& func) noexcept |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 52 | { |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 53 | try |
| 54 | { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 55 | return func(); |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 56 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 57 | catch (const std::exception& e) |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 58 | { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 59 | auto err = fmt::format("{} failed on {}: {}", msg, intf, e.what()); |
| 60 | log<level::ERR>(err.c_str(), entry("INTERFACE=%s", intf.c_str())); |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 61 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 62 | return fallback; |
William A. Kennington III | 4ee7a7e | 2022-10-11 16:37:22 -0700 | [diff] [blame] | 63 | } |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 64 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 65 | static std::string makeObjPath(std::string_view root, std::string_view intf) |
| 66 | { |
| 67 | auto ret = fmt::format(FMT_COMPILE("{}/{}"), root, intf); |
| 68 | std::replace(ret.begin() + ret.size() - intf.size(), ret.end(), '.', '_'); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | EthernetInterface::EthernetInterface(sdbusplus::bus_t& bus, Manager& manager, |
William A. Kennington III | 454a0de | 2022-11-12 01:01:04 -0800 | [diff] [blame] | 73 | const InterfaceInfo& info, |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 74 | std::string_view objRoot, |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 75 | const config::Parser& config, |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 76 | bool emitSignal, bool enabled) : |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 77 | EthernetInterface(bus, manager, info, makeObjPath(objRoot, *info.name), |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 78 | config, emitSignal, enabled) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | EthernetInterface::EthernetInterface(sdbusplus::bus_t& bus, Manager& manager, |
William A. Kennington III | 454a0de | 2022-11-12 01:01:04 -0800 | [diff] [blame] | 83 | const InterfaceInfo& info, |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 84 | std::string&& objPath, |
| 85 | const config::Parser& config, |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 86 | bool emitSignal, bool enabled) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 87 | Ifaces(bus, objPath.c_str(), |
| 88 | emitSignal ? Ifaces::action::defer_emit |
| 89 | : Ifaces::action::emit_no_signals), |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 90 | manager(manager), bus(bus), objPath(std::move(objPath)), ifIdx(info.idx) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 91 | { |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 92 | interfaceName(*info.name); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 93 | auto dhcpVal = getDHCPValue(config); |
| 94 | EthernetInterfaceIntf::dhcp4(dhcpVal.v4); |
| 95 | EthernetInterfaceIntf::dhcp6(dhcpVal.v6); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 96 | EthernetInterfaceIntf::ipv6AcceptRA(getIPv6AcceptRA(config)); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 97 | EthernetInterfaceIntf::nicEnabled(enabled); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 98 | |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 99 | EthernetInterfaceIntf::ntpServers( |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 100 | config.map.getValueStrings("Network", "NTP")); |
Ratan Gupta | 613a012 | 2020-04-24 15:18:53 +0530 | [diff] [blame] | 101 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 102 | updateInfo(info); |
| 103 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 104 | if (info.vlan_id) |
| 105 | { |
| 106 | if (!info.parent_idx) |
| 107 | { |
| 108 | std::runtime_error("Missing parent link"); |
| 109 | } |
| 110 | vlan.emplace(bus, this->objPath.c_str(), info, *this, emitSignal); |
| 111 | } |
| 112 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 113 | // Emit deferred signal. |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 114 | if (emitSignal) |
| 115 | { |
| 116 | this->emit_object_added(); |
| 117 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 118 | } |
| 119 | |
William A. Kennington III | 454a0de | 2022-11-12 01:01:04 -0800 | [diff] [blame] | 120 | void EthernetInterface::updateInfo(const InterfaceInfo& info) |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 121 | { |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 122 | EthernetInterfaceIntf::linkUp(info.flags & IFF_RUNNING); |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 123 | if (info.mac) |
| 124 | { |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 125 | MacAddressIntf::macAddress(std::to_string(*info.mac)); |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 126 | } |
| 127 | if (info.mtu) |
| 128 | { |
| 129 | EthernetInterfaceIntf::mtu(*info.mtu); |
| 130 | } |
William A. Kennington III | 2153966 | 2022-11-15 14:53:11 -0800 | [diff] [blame] | 131 | if (ifIdx > 0) |
| 132 | { |
| 133 | auto ethInfo = ignoreError("GetEthInfo", *info.name, {}, [&] { |
| 134 | return system::getEthInfo(*info.name); |
| 135 | }); |
| 136 | EthernetInterfaceIntf::autoNeg(ethInfo.autoneg); |
| 137 | EthernetInterfaceIntf::speed(ethInfo.speed); |
| 138 | } |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 141 | bool EthernetInterface::originIsManuallyAssigned(IP::AddressOrigin origin) |
| 142 | { |
| 143 | return ( |
| 144 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
| 145 | (origin == IP::AddressOrigin::Static) |
| 146 | #else |
| 147 | (origin == IP::AddressOrigin::Static || |
| 148 | origin == IP::AddressOrigin::LinkLocal) |
| 149 | #endif |
| 150 | |
| 151 | ); |
| 152 | } |
| 153 | |
William A. Kennington III | d6f5340 | 2022-11-07 14:48:53 -0800 | [diff] [blame] | 154 | void EthernetInterface::addAddr(const AddressInfo& info) |
| 155 | { |
William A. Kennington III | d6f5340 | 2022-11-07 14:48:53 -0800 | [diff] [blame] | 156 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
| 157 | if (dhcpIsEnabled(info.ifaddr.getAddr())) |
| 158 | { |
| 159 | origin = IP::AddressOrigin::DHCP; |
| 160 | } |
| 161 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
| 162 | if (info.scope == RT_SCOPE_LINK) |
| 163 | { |
| 164 | origin = IP::AddressOrigin::LinkLocal; |
| 165 | } |
| 166 | #endif |
| 167 | |
William A. Kennington III | 77747f6 | 2022-11-07 23:11:15 -0800 | [diff] [blame] | 168 | auto it = addrs.find(info.ifaddr); |
| 169 | if (it == addrs.end()) |
| 170 | { |
| 171 | addrs.emplace(info.ifaddr, std::make_unique<IPAddress>( |
| 172 | bus, std::string_view(objPath), *this, |
| 173 | info.ifaddr, origin)); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | it->second->IPIfaces::origin(origin); |
| 178 | } |
William A. Kennington III | d6f5340 | 2022-11-07 14:48:53 -0800 | [diff] [blame] | 179 | } |
| 180 | |
William A. Kennington III | bc2502c | 2022-11-07 16:31:54 -0800 | [diff] [blame] | 181 | void EthernetInterface::addStaticNeigh(const NeighborInfo& info) |
| 182 | { |
William A. Kennington III | d361514 | 2022-11-12 01:28:33 -0800 | [diff] [blame] | 183 | if (!info.mac || !info.addr) |
William A. Kennington III | bc2502c | 2022-11-07 16:31:54 -0800 | [diff] [blame] | 184 | { |
William A. Kennington III | d361514 | 2022-11-12 01:28:33 -0800 | [diff] [blame] | 185 | auto msg = fmt::format("Missing neighbor mac on {}\n", interfaceName()); |
William A. Kennington III | bc2502c | 2022-11-07 16:31:54 -0800 | [diff] [blame] | 186 | log<level::ERR>(msg.c_str()); |
| 187 | return; |
| 188 | } |
William A. Kennington III | 4e75acc | 2022-11-14 19:13:31 -0800 | [diff] [blame] | 189 | |
| 190 | if (auto it = staticNeighbors.find(*info.addr); it != staticNeighbors.end()) |
| 191 | { |
| 192 | it->second->NeighborObj::macAddress(std::to_string(*info.mac)); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | staticNeighbors.emplace(*info.addr, std::make_unique<Neighbor>( |
| 197 | bus, std::string_view(objPath), |
| 198 | *this, *info.addr, *info.mac, |
| 199 | Neighbor::State::Permanent)); |
| 200 | } |
William A. Kennington III | bc2502c | 2022-11-07 16:31:54 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 203 | ObjectPath EthernetInterface::ip(IP::Protocol protType, std::string ipaddress, |
William A. Kennington III | e25f8b4 | 2022-10-11 14:43:28 -0700 | [diff] [blame] | 204 | uint8_t prefixLength, std::string) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 205 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 206 | InAddrAny addr; |
| 207 | try |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 208 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 209 | switch (protType) |
| 210 | { |
| 211 | case IP::Protocol::IPv4: |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 212 | addr = ToAddr<in_addr>{}(ipaddress); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 213 | break; |
| 214 | case IP::Protocol::IPv6: |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 215 | addr = ToAddr<in6_addr>{}(ipaddress); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 216 | break; |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 217 | default: |
| 218 | throw std::logic_error("Exhausted protocols"); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 219 | } |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 220 | } |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 221 | catch (const std::exception& e) |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 222 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 223 | auto msg = fmt::format("Invalid IP `{}`: {}\n", ipaddress, e.what()); |
| 224 | log<level::ERR>(msg.c_str(), entry("ADDRESS=%s", ipaddress.c_str())); |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 225 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"), |
| 226 | Argument::ARGUMENT_VALUE(ipaddress.c_str())); |
| 227 | } |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 228 | IfAddr ifaddr; |
| 229 | try |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 230 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 231 | ifaddr = {addr, prefixLength}; |
| 232 | } |
| 233 | catch (const std::exception& e) |
| 234 | { |
| 235 | auto msg = fmt::format("Invalid prefix length `{}`: {}\n", prefixLength, |
| 236 | e.what()); |
| 237 | log<level::ERR>(msg.c_str(), |
| 238 | entry("PREFIXLENGTH=%" PRIu8, prefixLength)); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 239 | elog<InvalidArgument>( |
| 240 | Argument::ARGUMENT_NAME("prefixLength"), |
| 241 | Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str())); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 242 | } |
| 243 | |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 244 | auto [it, _] = this->addrs.insert_or_assign( |
| 245 | ifaddr, |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 246 | std::make_unique<IPAddress>(bus, std::string_view(objPath), *this, |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 247 | ifaddr, IP::AddressOrigin::Static)); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 248 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 249 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 250 | manager.reloadConfigs(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 251 | |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 252 | return it->second->getObjPath(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 253 | } |
| 254 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 255 | ObjectPath EthernetInterface::neighbor(std::string ipAddress, |
| 256 | std::string macAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 257 | { |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 258 | InAddrAny addr; |
| 259 | try |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 260 | { |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 261 | addr = ToAddr<InAddrAny>{}(ipAddress); |
| 262 | } |
| 263 | catch (const std::exception& e) |
| 264 | { |
| 265 | auto msg = |
| 266 | fmt::format("Not a valid IP address `{}`: {}", ipAddress, e.what()); |
| 267 | log<level::ERR>(msg.c_str(), entry("ADDRESS=%s", ipAddress.c_str())); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 268 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipAddress"), |
| 269 | Argument::ARGUMENT_VALUE(ipAddress.c_str())); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 270 | } |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 271 | |
| 272 | ether_addr lladdr; |
| 273 | try |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 274 | { |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 275 | lladdr = ToAddr<ether_addr>{}(macAddress); |
| 276 | } |
| 277 | catch (const std::exception& e) |
| 278 | { |
| 279 | auto msg = fmt::format("Not a valid MAC address `{}`: {}", macAddress, |
| 280 | e.what()); |
| 281 | log<level::ERR>(msg.c_str(), |
| 282 | entry("MACADDRESS=%s", macAddress.c_str())); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 283 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("macAddress"), |
| 284 | Argument::ARGUMENT_VALUE(macAddress.c_str())); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 285 | } |
| 286 | |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 287 | auto [it, _] = staticNeighbors.emplace( |
| 288 | addr, |
| 289 | std::make_unique<Neighbor>(bus, std::string_view(objPath), *this, addr, |
| 290 | lladdr, Neighbor::State::Permanent)); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 291 | |
| 292 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 293 | manager.reloadConfigs(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 294 | |
William A. Kennington III | 434a943 | 2022-11-04 18:38:46 -0700 | [diff] [blame] | 295 | return it->second->getObjPath(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 298 | bool EthernetInterface::ipv6AcceptRA(bool value) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 299 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 300 | if (ipv6AcceptRA() != EthernetInterfaceIntf::ipv6AcceptRA(value)) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 301 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 302 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 303 | manager.reloadConfigs(); |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 304 | } |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 305 | return value; |
| 306 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 307 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 308 | bool EthernetInterface::dhcp4(bool value) |
| 309 | { |
| 310 | if (dhcp4() != EthernetInterfaceIntf::dhcp4(value)) |
| 311 | { |
| 312 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 313 | manager.reloadConfigs(); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 314 | } |
| 315 | return value; |
| 316 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 317 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 318 | bool EthernetInterface::dhcp6(bool value) |
| 319 | { |
| 320 | if (dhcp6() != EthernetInterfaceIntf::dhcp6(value)) |
| 321 | { |
| 322 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 323 | manager.reloadConfigs(); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 324 | } |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 325 | return value; |
| 326 | } |
| 327 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 328 | EthernetInterface::DHCPConf EthernetInterface::dhcpEnabled(DHCPConf value) |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 329 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 330 | auto old4 = EthernetInterfaceIntf::dhcp4(); |
| 331 | auto new4 = EthernetInterfaceIntf::dhcp4(value == DHCPConf::v4 || |
| 332 | value == DHCPConf::v4v6stateless || |
| 333 | value == DHCPConf::both); |
| 334 | auto old6 = EthernetInterfaceIntf::dhcp6(); |
| 335 | auto new6 = EthernetInterfaceIntf::dhcp6(value == DHCPConf::v6 || |
| 336 | value == DHCPConf::both); |
| 337 | auto oldra = EthernetInterfaceIntf::ipv6AcceptRA(); |
| 338 | auto newra = EthernetInterfaceIntf::ipv6AcceptRA( |
| 339 | value == DHCPConf::v6stateless || value == DHCPConf::v4v6stateless || |
| 340 | value == DHCPConf::v6 || value == DHCPConf::both); |
| 341 | |
| 342 | if (old4 != new4 || old6 != new6 || oldra != newra) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 343 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 344 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 345 | manager.reloadConfigs(); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 346 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 347 | return value; |
| 348 | } |
| 349 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 350 | EthernetInterface::DHCPConf EthernetInterface::dhcpEnabled() const |
| 351 | { |
| 352 | if (dhcp6()) |
| 353 | { |
| 354 | return dhcp4() ? DHCPConf::both : DHCPConf::v6; |
| 355 | } |
| 356 | else if (dhcp4()) |
| 357 | { |
| 358 | return ipv6AcceptRA() ? DHCPConf::v4v6stateless : DHCPConf::v4; |
| 359 | } |
| 360 | return ipv6AcceptRA() ? DHCPConf::v6stateless : DHCPConf::none; |
| 361 | } |
| 362 | |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 363 | size_t EthernetInterface::mtu(size_t value) |
| 364 | { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 365 | const size_t old = EthernetInterfaceIntf::mtu(); |
| 366 | if (value == old) |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 367 | { |
| 368 | return value; |
| 369 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 370 | const auto ifname = interfaceName(); |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 371 | return EthernetInterfaceIntf::mtu(ignoreError("SetMTU", ifname, old, [&] { |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 372 | system::setMTU(ifname, value); |
| 373 | return value; |
| 374 | })); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 375 | } |
| 376 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 377 | bool EthernetInterface::nicEnabled(bool value) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 378 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 379 | if (value == EthernetInterfaceIntf::nicEnabled()) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 380 | { |
| 381 | return value; |
| 382 | } |
| 383 | |
William A. Kennington III | c922d5e | 2022-01-21 01:08:31 -0800 | [diff] [blame] | 384 | EthernetInterfaceIntf::nicEnabled(value); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 385 | writeConfigurationFile(); |
William A. Kennington III | 329b5fb | 2021-11-09 17:19:30 -0800 | [diff] [blame] | 386 | if (!value) |
| 387 | { |
| 388 | // We only need to bring down the interface, networkd will always bring |
| 389 | // up managed interfaces |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 390 | manager.addReloadPreHook( |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 391 | [ifname = interfaceName()]() { system::setNICUp(ifname, false); }); |
William A. Kennington III | 329b5fb | 2021-11-09 17:19:30 -0800 | [diff] [blame] | 392 | } |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 393 | manager.reloadConfigs(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 394 | |
| 395 | return value; |
| 396 | } |
| 397 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 398 | ServerList EthernetInterface::staticNameServers(ServerList value) |
| 399 | { |
William A. Kennington III | c9f672e | 2022-11-04 14:35:53 -0700 | [diff] [blame] | 400 | for (auto& ip : value) |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 401 | { |
William A. Kennington III | c9f672e | 2022-11-04 14:35:53 -0700 | [diff] [blame] | 402 | try |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 403 | { |
William A. Kennington III | c9f672e | 2022-11-04 14:35:53 -0700 | [diff] [blame] | 404 | ip = std::to_string(ToAddr<InAddrAny>{}(ip)); |
| 405 | } |
| 406 | catch (const std::exception& e) |
| 407 | { |
| 408 | auto msg = |
| 409 | fmt::format("Not a valid IP address `{}`: {}", ip, e.what()); |
| 410 | log<level::ERR>(msg.c_str()), entry("ADDRESS=%s", ip.c_str()); |
| 411 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("StaticNameserver"), |
| 412 | Argument::ARGUMENT_VALUE(ip.c_str())); |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 413 | } |
| 414 | } |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 415 | try |
| 416 | { |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 417 | EthernetInterfaceIntf::staticNameServers(value); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 418 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 419 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 420 | manager.reloadConfigs(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 421 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 422 | catch (const InternalFailure& e) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 423 | { |
| 424 | log<level::ERR>("Exception processing DNS entries"); |
| 425 | } |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 426 | return EthernetInterfaceIntf::staticNameServers(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 427 | } |
| 428 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 429 | void EthernetInterface::loadNTPServers(const config::Parser& config) |
| 430 | { |
| 431 | EthernetInterfaceIntf::ntpServers(getNTPServerFromTimeSyncd()); |
| 432 | EthernetInterfaceIntf::staticNTPServers( |
| 433 | config.map.getValueStrings("Network", "NTP")); |
| 434 | } |
| 435 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 436 | void EthernetInterface::loadNameServers(const config::Parser& config) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 437 | { |
| 438 | EthernetInterfaceIntf::nameservers(getNameServerFromResolvd()); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 439 | EthernetInterfaceIntf::staticNameServers( |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 440 | config.map.getValueStrings("Network", "DNS")); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 441 | } |
| 442 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 443 | ServerList EthernetInterface::getNTPServerFromTimeSyncd() |
| 444 | { |
| 445 | ServerList servers; // Variable to capture the NTP Server IPs |
| 446 | auto method = bus.new_method_call(TIMESYNCD_SERVICE, TIMESYNCD_SERVICE_PATH, |
| 447 | PROPERTY_INTERFACE, METHOD_GET); |
| 448 | |
| 449 | method.append(TIMESYNCD_INTERFACE, "LinkNTPServers"); |
| 450 | |
| 451 | try |
| 452 | { |
| 453 | auto reply = bus.call(method); |
| 454 | std::variant<ServerList> response; |
| 455 | reply.read(response); |
| 456 | servers = std::get<ServerList>(response); |
| 457 | } |
| 458 | catch (const sdbusplus::exception::SdBusError& e) |
| 459 | { |
| 460 | log<level::ERR>( |
| 461 | "Failed to get NTP server information from Systemd-Timesyncd"); |
| 462 | } |
| 463 | |
| 464 | return servers; |
| 465 | } |
| 466 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 467 | ServerList EthernetInterface::getNameServerFromResolvd() |
| 468 | { |
| 469 | ServerList servers; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 470 | auto OBJ_PATH = fmt::format("{}{}", RESOLVED_SERVICE_PATH, ifIdx); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 471 | |
| 472 | /* |
| 473 | The DNS property under org.freedesktop.resolve1.Link interface contains |
| 474 | an array containing all DNS servers currently used by resolved. It |
| 475 | contains similar information as the DNS server data written to |
| 476 | /run/systemd/resolve/resolv.conf. |
| 477 | |
| 478 | Each structure in the array consists of a numeric network interface index, |
| 479 | an address family, and a byte array containing the DNS server address |
| 480 | (either 4 bytes in length for IPv4 or 16 bytes in lengths for IPv6). |
| 481 | The array contains DNS servers configured system-wide, including those |
| 482 | possibly read from a foreign /etc/resolv.conf or the DNS= setting in |
| 483 | /etc/systemd/resolved.conf, as well as per-interface DNS server |
| 484 | information either retrieved from systemd-networkd or configured by |
| 485 | external software via SetLinkDNS(). |
| 486 | */ |
| 487 | |
| 488 | using type = std::vector<std::tuple<int32_t, std::vector<uint8_t>>>; |
| 489 | std::variant<type> name; // Variable to capture the DNS property |
| 490 | auto method = bus.new_method_call(RESOLVED_SERVICE, OBJ_PATH.c_str(), |
| 491 | PROPERTY_INTERFACE, METHOD_GET); |
| 492 | |
| 493 | method.append(RESOLVED_INTERFACE, "DNS"); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 494 | |
| 495 | try |
| 496 | { |
Asmitha Karunanithi | 97ddb8d | 2022-05-05 01:00:18 -0500 | [diff] [blame] | 497 | auto reply = bus.call(method); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 498 | reply.read(name); |
| 499 | } |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 500 | catch (const sdbusplus::exception_t& e) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 501 | { |
| 502 | log<level::ERR>("Failed to get DNS information from Systemd-Resolved"); |
| 503 | } |
| 504 | auto tupleVector = std::get_if<type>(&name); |
| 505 | for (auto i = tupleVector->begin(); i != tupleVector->end(); ++i) |
| 506 | { |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 507 | int addressFamily = std::get<0>(*i); |
| 508 | std::vector<uint8_t>& ipaddress = std::get<1>(*i); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 509 | servers.push_back(std::to_string( |
| 510 | addrFromBuf(addressFamily, stdplus::raw::asView<char>(ipaddress)))); |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 511 | } |
| 512 | return servers; |
| 513 | } |
| 514 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 515 | ObjectPath EthernetInterface::createVLAN(uint16_t id) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 516 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 517 | auto intfName = fmt::format(FMT_COMPILE("{}.{}"), interfaceName(), id); |
| 518 | auto idStr = std::to_string(id); |
| 519 | if (manager.interfaces.find(intfName) != manager.interfaces.end()) |
Jiaqing Zhao | 33b4eaa | 2022-04-12 23:11:40 +0800 | [diff] [blame] | 520 | { |
| 521 | log<level::ERR>("VLAN already exists", entry("VLANID=%u", id)); |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 522 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("VLANId"), |
| 523 | Argument::ARGUMENT_VALUE(idStr.c_str())); |
Jiaqing Zhao | 33b4eaa | 2022-04-12 23:11:40 +0800 | [diff] [blame] | 524 | } |
| 525 | |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 526 | auto objRoot = std::string_view(objPath).substr(0, objPath.rfind('/')); |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 527 | auto macStr = MacAddressIntf::macAddress(); |
| 528 | std::optional<ether_addr> mac; |
| 529 | if (!macStr.empty()) |
| 530 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 531 | mac.emplace(ToAddr<ether_addr>{}(macStr)); |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 532 | } |
William A. Kennington III | 454a0de | 2022-11-12 01:01:04 -0800 | [diff] [blame] | 533 | auto info = InterfaceInfo{ |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 534 | .idx = 0, // TODO: Query the correct value after creation |
| 535 | .flags = 0, |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 536 | .name = intfName, |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 537 | .mac = std::move(mac), |
| 538 | .mtu = mtu(), |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 539 | .parent_idx = ifIdx, |
| 540 | .vlan_id = id, |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 541 | }; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 542 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 543 | // Pass the parents nicEnabled property, so that the child |
Manojkiran Eda | ca8b91b | 2020-05-28 09:28:42 +0530 | [diff] [blame] | 544 | // VLAN interface can inherit. |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 545 | auto vlanIntf = std::make_unique<EthernetInterface>( |
| 546 | bus, manager, info, objRoot, config::Parser(), /*emit=*/true, |
William A. Kennington III | d298f93 | 2022-10-17 14:31:38 -0700 | [diff] [blame] | 547 | nicEnabled()); |
| 548 | ObjectPath ret = vlanIntf->objPath; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 549 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 550 | manager.interfaces.emplace(intfName, std::move(vlanIntf)); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 551 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 552 | // write the device file for the vlan interface. |
| 553 | config::Parser config; |
| 554 | auto& netdev = config.map["NetDev"].emplace_back(); |
| 555 | netdev["Name"].emplace_back(intfName); |
| 556 | netdev["Kind"].emplace_back("vlan"); |
| 557 | config.map["VLAN"].emplace_back()["Id"].emplace_back(std::move(idStr)); |
| 558 | config.writeFile(config::pathForIntfDev(manager.getConfDir(), intfName)); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 559 | |
| 560 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 561 | manager.reloadConfigs(); |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 562 | |
William A. Kennington III | 7b90bc8 | 2022-11-17 14:55:12 -0800 | [diff] [blame] | 563 | return ret; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 564 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 565 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 566 | ServerList EthernetInterface::staticNTPServers(ServerList value) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 567 | { |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 568 | try |
| 569 | { |
| 570 | EthernetInterfaceIntf::staticNTPServers(value); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 571 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 572 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 573 | manager.reloadConfigs(); |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 574 | } |
| 575 | catch (InternalFailure& e) |
| 576 | { |
| 577 | log<level::ERR>("Exception processing NTP entries"); |
| 578 | } |
| 579 | return EthernetInterfaceIntf::staticNTPServers(); |
| 580 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 581 | |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 582 | ServerList EthernetInterface::ntpServers(ServerList /*servers*/) |
| 583 | { |
| 584 | elog<NotAllowed>(NotAllowedArgument::REASON("ReadOnly Property")); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 585 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 586 | // Need to merge the below function with the code which writes the |
| 587 | // config file during factory reset. |
| 588 | // TODO openbmc/openbmc#1751 |
| 589 | |
| 590 | void EthernetInterface::writeConfigurationFile() |
| 591 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 592 | config::Parser config; |
| 593 | config.map["Match"].emplace_back()["Name"].emplace_back(interfaceName()); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 594 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 595 | auto& link = config.map["Link"].emplace_back(); |
Johnathan Mantey | 609c12d | 2022-02-03 09:23:09 -0800 | [diff] [blame] | 596 | #ifdef PERSIST_MAC |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 597 | auto mac = MacAddressIntf::macAddress(); |
| 598 | if (!mac.empty()) |
| 599 | { |
| 600 | link["MACAddress"].emplace_back(mac); |
| 601 | } |
Johnathan Mantey | 609c12d | 2022-02-03 09:23:09 -0800 | [diff] [blame] | 602 | #endif |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 603 | if (!EthernetInterfaceIntf::nicEnabled()) |
| 604 | { |
| 605 | link["Unmanaged"].emplace_back("yes"); |
| 606 | } |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 607 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 608 | { |
| 609 | auto& network = config.map["Network"].emplace_back(); |
| 610 | auto& lla = network["LinkLocalAddressing"]; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 611 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 612 | lla.emplace_back("yes"); |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 613 | #else |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 614 | lla.emplace_back("no"); |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 615 | #endif |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 616 | network["IPv6AcceptRA"].emplace_back(ipv6AcceptRA() ? "true" : "false"); |
| 617 | network["DHCP"].emplace_back(dhcp4() ? (dhcp6() ? "true" : "ipv4") |
| 618 | : (dhcp6() ? "ipv6" : "false")); |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 619 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 620 | auto& vlans = network["VLAN"]; |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 621 | for (const auto& [_, intf] : manager.interfaces) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 622 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 623 | if (intf->vlan && intf->vlan->parentIdx == ifIdx) |
| 624 | { |
| 625 | vlans.emplace_back(intf->interfaceName()); |
| 626 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | { |
| 630 | auto& ntps = network["NTP"]; |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 631 | for (const auto& ntp : EthernetInterfaceIntf::staticNTPServers()) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 632 | { |
| 633 | ntps.emplace_back(ntp); |
| 634 | } |
| 635 | } |
| 636 | { |
| 637 | auto& dnss = network["DNS"]; |
| 638 | for (const auto& dns : EthernetInterfaceIntf::staticNameServers()) |
| 639 | { |
| 640 | dnss.emplace_back(dns); |
| 641 | } |
| 642 | } |
| 643 | { |
| 644 | auto& address = network["Address"]; |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 645 | for (const auto& addr : addrs) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 646 | { |
William A. Kennington III | 59e5b91 | 2022-11-02 02:49:46 -0700 | [diff] [blame] | 647 | if (originIsManuallyAssigned(addr.second->origin())) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 648 | { |
| 649 | address.emplace_back( |
| 650 | fmt::format("{}/{}", addr.second->address(), |
| 651 | addr.second->prefixLength())); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | { |
| 656 | auto& gateways = network["Gateway"]; |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 657 | if (!dhcp4()) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 658 | { |
| 659 | auto gateway = EthernetInterfaceIntf::defaultGateway(); |
| 660 | if (!gateway.empty()) |
| 661 | { |
| 662 | gateways.emplace_back(gateway); |
| 663 | } |
| 664 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 665 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 666 | if (!dhcp6()) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 667 | { |
| 668 | auto gateway6 = EthernetInterfaceIntf::defaultGateway6(); |
| 669 | if (!gateway6.empty()) |
| 670 | { |
| 671 | gateways.emplace_back(gateway6); |
| 672 | } |
| 673 | } |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 674 | } |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 675 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 676 | config.map["IPv6AcceptRA"].emplace_back()["DHCPv6Client"].emplace_back( |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 677 | dhcp6() ? "true" : "false"); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 678 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 679 | auto& neighbors = config.map["Neighbor"]; |
| 680 | for (const auto& sneighbor : staticNeighbors) |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 681 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 682 | auto& neighbor = neighbors.emplace_back(); |
| 683 | neighbor["Address"].emplace_back(sneighbor.second->ipAddress()); |
| 684 | neighbor["MACAddress"].emplace_back(sneighbor.second->macAddress()); |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 685 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 686 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 687 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 688 | auto& dhcp = config.map["DHCP"].emplace_back(); |
| 689 | dhcp["ClientIdentifier"].emplace_back("mac"); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 690 | const auto& conf = manager.getDHCPConf(); |
| 691 | auto dns_enabled = conf.dnsEnabled() ? "true" : "false"; |
| 692 | dhcp["UseDNS"].emplace_back(dns_enabled); |
| 693 | dhcp["UseDomains"].emplace_back(dns_enabled); |
| 694 | dhcp["UseNTP"].emplace_back(conf.ntpEnabled() ? "true" : "false"); |
| 695 | dhcp["UseHostname"].emplace_back(conf.hostNameEnabled() ? "true" |
| 696 | : "false"); |
| 697 | dhcp["SendHostname"].emplace_back(conf.sendHostNameEnabled() ? "true" |
| 698 | : "false"); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 699 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 700 | auto path = config::pathForIntfConf(manager.getConfDir(), interfaceName()); |
| 701 | config.writeFile(path); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 702 | auto msg = fmt::format("Wrote networkd file: {}", path.native()); |
| 703 | log<level::INFO>(msg.c_str(), entry("FILE=%s", path.c_str())); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 704 | } |
| 705 | |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 706 | std::string EthernetInterface::macAddress([[maybe_unused]] std::string value) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 707 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 708 | if (vlan) |
| 709 | { |
| 710 | log<level::ERR>("Tried to set MAC address on VLAN"); |
| 711 | elog<InternalFailure>(); |
| 712 | } |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 713 | #ifdef PERSIST_MAC |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 714 | ether_addr newMAC; |
| 715 | try |
| 716 | { |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 717 | newMAC = ToAddr<ether_addr>{}(value); |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 718 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 719 | catch (const std::invalid_argument&) |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 720 | { |
| 721 | log<level::ERR>("MACAddress is not valid.", |
| 722 | entry("MAC=%s", value.c_str())); |
| 723 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 724 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 725 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 726 | if (!mac_address::isUnicast(newMAC)) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 727 | { |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 728 | log<level::ERR>("MACAddress is not valid.", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 729 | entry("MAC=%s", value.c_str())); |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 730 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 731 | Argument::ARGUMENT_VALUE(value.c_str())); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 732 | } |
| 733 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 734 | auto interface = interfaceName(); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 735 | auto validMAC = std::to_string(newMAC); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 736 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 737 | // We don't need to update the system if the address is unchanged |
William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 738 | ether_addr oldMAC = ToAddr<ether_addr>{}(MacAddressIntf::macAddress()); |
William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 739 | if (newMAC != oldMAC) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 740 | { |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 741 | // Update everything that depends on the MAC value |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 742 | for (const auto& [_, intf] : manager.interfaces) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 743 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 744 | if (intf->vlan && intf->vlan->parentIdx == ifIdx) |
| 745 | { |
| 746 | intf->MacAddressIntf::macAddress(validMAC); |
| 747 | } |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 748 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 749 | MacAddressIntf::macAddress(validMAC); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 750 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 751 | writeConfigurationFile(); |
William A. Kennington III | 6bfdf3e | 2021-11-09 17:15:12 -0800 | [diff] [blame] | 752 | manager.addReloadPreHook([interface]() { |
| 753 | // The MAC and LLADDRs will only update if the NIC is already down |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 754 | system::setNICUp(interface, false); |
William A. Kennington III | 6bfdf3e | 2021-11-09 17:15:12 -0800 | [diff] [blame] | 755 | }); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 756 | manager.reloadConfigs(); |
Ratan Gupta | 677ae12 | 2017-09-18 16:28:50 +0530 | [diff] [blame] | 757 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 758 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 759 | #ifdef HAVE_UBOOT_ENV |
| 760 | // Ensure that the valid address is stored in the u-boot-env |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 761 | auto envVar = interfaceToUbootEthAddr(interface); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 762 | if (envVar) |
| 763 | { |
Asmitha Karunanithi | 33bc9a9 | 2020-08-13 08:48:33 -0500 | [diff] [blame] | 764 | // Trimming MAC addresses that are out of range. eg: AA:FF:FF:FF:FF:100; |
| 765 | // and those having more than 6 bytes. eg: AA:AA:AA:AA:AA:AA:BB |
| 766 | execute("/sbin/fw_setenv", "fw_setenv", envVar->c_str(), |
| 767 | validMAC.c_str()); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 768 | } |
| 769 | #endif // HAVE_UBOOT_ENV |
| 770 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 771 | return value; |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 772 | #else |
| 773 | elog<NotAllowed>( |
| 774 | NotAllowedArgument::REASON("Writing MAC address is not allowed")); |
| 775 | #endif // PERSIST_MAC |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 776 | } |
| 777 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 778 | void EthernetInterface::deleteAll() |
| 779 | { |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 780 | // clear all the ip on the interface |
| 781 | addrs.clear(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 782 | |
| 783 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 784 | manager.reloadConfigs(); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 785 | } |
| 786 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 787 | std::string EthernetInterface::defaultGateway(std::string gateway) |
| 788 | { |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 789 | try |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 790 | { |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 791 | if (!gateway.empty()) |
| 792 | { |
| 793 | gateway = std::to_string(ToAddr<in_addr>{}(gateway)); |
| 794 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 795 | } |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 796 | catch (const std::exception& e) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 797 | { |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 798 | auto msg = fmt::format("Invalid v4 GW `{}`: {}", gateway, e.what()); |
| 799 | log<level::ERR>(msg.c_str(), entry("GATEWAY=%s", gateway.c_str())); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 800 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 801 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 802 | } |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 803 | |
| 804 | if (EthernetInterfaceIntf::defaultGateway() == gateway) |
| 805 | { |
| 806 | return gateway; |
| 807 | } |
| 808 | EthernetInterfaceIntf::defaultGateway(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 809 | |
| 810 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 811 | manager.reloadConfigs(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 812 | |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 813 | return gateway; |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | std::string EthernetInterface::defaultGateway6(std::string gateway) |
| 817 | { |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 818 | try |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 819 | { |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 820 | if (!gateway.empty()) |
| 821 | { |
| 822 | gateway = std::to_string(ToAddr<in6_addr>{}(gateway)); |
| 823 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 824 | } |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 825 | catch (const std::exception& e) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 826 | { |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 827 | auto msg = fmt::format("Invalid v6 GW `{}`: {}", gateway, e.what()); |
| 828 | log<level::ERR>(msg.c_str(), entry("GATEWAY=%s", gateway.c_str())); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 829 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 830 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 831 | } |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 832 | |
| 833 | if (EthernetInterfaceIntf::defaultGateway6() == gateway) |
| 834 | { |
| 835 | return gateway; |
| 836 | } |
| 837 | EthernetInterfaceIntf::defaultGateway6(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 838 | |
| 839 | writeConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 840 | manager.reloadConfigs(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 841 | |
William A. Kennington III | f6f0cf3 | 2022-11-04 14:28:42 -0700 | [diff] [blame] | 842 | return gateway; |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 843 | } |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 844 | |
| 845 | EthernetInterface::VlanProperties::VlanProperties( |
| 846 | sdbusplus::bus_t& bus, stdplus::const_zstring objPath, |
William A. Kennington III | 454a0de | 2022-11-12 01:01:04 -0800 | [diff] [blame] | 847 | const InterfaceInfo& info, EthernetInterface& eth, bool emitSignal) : |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 848 | VlanIfaces(bus, objPath.c_str(), |
| 849 | emitSignal ? VlanIfaces::action::defer_emit |
| 850 | : VlanIfaces::action::emit_no_signals), |
| 851 | parentIdx(*info.parent_idx), eth(eth) |
| 852 | { |
| 853 | VlanIntf::id(*info.vlan_id); |
| 854 | if (emitSignal) |
| 855 | { |
| 856 | this->emit_object_added(); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | void EthernetInterface::VlanProperties::delete_() |
| 861 | { |
| 862 | auto intf = eth.interfaceName(); |
| 863 | |
| 864 | // Remove all configs for the current interface |
| 865 | const auto& confDir = eth.manager.getConfDir(); |
| 866 | std::error_code ec; |
| 867 | std::filesystem::remove(config::pathForIntfConf(confDir, intf), ec); |
| 868 | std::filesystem::remove(config::pathForIntfDev(confDir, intf), ec); |
| 869 | |
| 870 | // Write an updated parent interface since it has a VLAN entry |
| 871 | for (const auto& [_, intf] : eth.manager.interfaces) |
| 872 | { |
| 873 | if (intf->ifIdx == parentIdx) |
| 874 | { |
| 875 | intf->writeConfigurationFile(); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // We need to forcibly delete the interface as systemd does not |
| 880 | deleteInterface(intf); |
| 881 | |
William A. Kennington III | 67b09da | 2022-10-31 14:09:53 -0700 | [diff] [blame] | 882 | if (eth.ifIdx > 0) |
| 883 | { |
| 884 | eth.manager.interfacesByIdx.erase(eth.ifIdx); |
| 885 | } |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 886 | eth.manager.interfaces.erase(intf); |
| 887 | } |
| 888 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 889 | } // namespace network |
| 890 | } // namespace phosphor |