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