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" |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 6 | #include "ipaddress.hpp" |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 7 | #include "neighbor.hpp" |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 8 | #include "network_manager.hpp" |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 9 | #include "util.hpp" |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 10 | #include "vlan_interface.hpp" |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 11 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 12 | #include <arpa/inet.h> |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 13 | #include <fmt/compile.h> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 14 | #include <fmt/format.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 15 | #include <linux/ethtool.h> |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 16 | #include <linux/rtnetlink.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 17 | #include <linux/sockios.h> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 18 | #include <net/if.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 19 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 20 | #include <algorithm> |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 21 | #include <filesystem> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 22 | #include <phosphor-logging/elog-errors.hpp> |
| 23 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 24 | #include <sdbusplus/bus/match.hpp> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 25 | #include <sstream> |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 26 | #include <stdplus/fd/create.hpp> |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 27 | #include <stdplus/raw.hpp> |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 28 | #include <stdplus/zstring.hpp> |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 29 | #include <string> |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 30 | #include <unordered_map> |
| 31 | #include <variant> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 32 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 33 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 34 | namespace phosphor |
| 35 | { |
| 36 | namespace network |
| 37 | { |
| 38 | |
| 39 | using namespace phosphor::logging; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 40 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 41 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 42 | using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 43 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 44 | constexpr auto RESOLVED_SERVICE = "org.freedesktop.resolve1"; |
| 45 | constexpr auto RESOLVED_INTERFACE = "org.freedesktop.resolve1.Link"; |
| 46 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 47 | constexpr auto RESOLVED_SERVICE_PATH = "/org/freedesktop/resolve1/link/"; |
| 48 | constexpr auto METHOD_GET = "Get"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 49 | |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 50 | static stdplus::Fd& getIFSock() |
| 51 | { |
| 52 | using namespace stdplus::fd; |
| 53 | static auto fd = |
| 54 | socket(SocketDomain::INet, SocketType::Datagram, SocketProto::IP); |
| 55 | return fd; |
| 56 | } |
| 57 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 58 | EthernetInterface::EthernetInterface(sdbusplus::bus_t& bus, |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 59 | const std::string& objPath, |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 60 | const config::Parser& config, |
William A. Kennington III | 0caf221 | 2022-08-18 18:15:51 -0700 | [diff] [blame] | 61 | Manager& parent, bool emitSignal, |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 62 | std::optional<bool> enabled) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 63 | Ifaces(bus, objPath.c_str(), |
| 64 | emitSignal ? Ifaces::action::defer_emit |
| 65 | : Ifaces::action::emit_no_signals), |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 66 | bus(bus), manager(parent), objPath(objPath) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 67 | { |
| 68 | auto intfName = objPath.substr(objPath.rfind("/") + 1); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 69 | std::replace(intfName.begin(), intfName.end(), '_', '.'); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 70 | interfaceName(intfName); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 71 | auto dhcpVal = getDHCPValue(config); |
| 72 | EthernetInterfaceIntf::dhcp4(dhcpVal.v4); |
| 73 | EthernetInterfaceIntf::dhcp6(dhcpVal.v6); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 74 | EthernetInterfaceIntf::ipv6AcceptRA(getIPv6AcceptRA(config)); |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 75 | EthernetInterfaceIntf::nicEnabled(enabled ? *enabled : queryNicEnabled()); |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 76 | const auto& gatewayList = manager.getRouteTable().getDefaultGateway(); |
| 77 | const auto& gateway6List = manager.getRouteTable().getDefaultGateway6(); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 78 | std::string defaultGateway; |
| 79 | std::string defaultGateway6; |
| 80 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 81 | for (const auto& gateway : gatewayList) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 82 | { |
| 83 | if (gateway.first == intfName) |
| 84 | { |
| 85 | defaultGateway = gateway.second; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 90 | for (const auto& gateway6 : gateway6List) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 91 | { |
| 92 | if (gateway6.first == intfName) |
| 93 | { |
| 94 | defaultGateway6 = gateway6.second; |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | EthernetInterfaceIntf::defaultGateway(defaultGateway); |
| 100 | EthernetInterfaceIntf::defaultGateway6(defaultGateway6); |
Ratan Gupta | 99801ce | 2020-01-09 18:37:16 +0530 | [diff] [blame] | 101 | // Don't get the mac address from the system as the mac address |
| 102 | // would be same as parent interface. |
| 103 | if (intfName.find(".") == std::string::npos) |
| 104 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 105 | MacAddressIntf::macAddress(getMACAddress(intfName)); |
Ratan Gupta | 99801ce | 2020-01-09 18:37:16 +0530 | [diff] [blame] | 106 | } |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 107 | EthernetInterfaceIntf::ntpServers( |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 108 | config.map.getValueStrings("Network", "NTP")); |
Ratan Gupta | 613a012 | 2020-04-24 15:18:53 +0530 | [diff] [blame] | 109 | |
| 110 | EthernetInterfaceIntf::linkUp(linkUp()); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 111 | EthernetInterfaceIntf::mtu(mtu()); |
Ratan Gupta | 613a012 | 2020-04-24 15:18:53 +0530 | [diff] [blame] | 112 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 113 | #ifdef NIC_SUPPORTS_ETHTOOL |
Johnathan Mantey | cb42fe2 | 2019-08-01 13:35:29 -0700 | [diff] [blame] | 114 | InterfaceInfo ifInfo = EthernetInterface::getInterfaceInfo(); |
| 115 | |
| 116 | EthernetInterfaceIntf::autoNeg(std::get<2>(ifInfo)); |
| 117 | EthernetInterfaceIntf::speed(std::get<0>(ifInfo)); |
Johnathan Mantey | ad4bf5c | 2020-01-24 13:30:39 -0800 | [diff] [blame] | 118 | #endif |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 119 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 120 | // Emit deferred signal. |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 121 | if (emitSignal) |
| 122 | { |
| 123 | this->emit_object_added(); |
| 124 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 125 | } |
| 126 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 127 | static IP::Protocol getProtocol(const InAddrAny& addr) |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 128 | { |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 129 | if (std::holds_alternative<in_addr>(addr)) |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 130 | { |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 131 | return IP::Protocol::IPv4; |
| 132 | } |
| 133 | else if (std::holds_alternative<in6_addr>(addr)) |
| 134 | { |
| 135 | return IP::Protocol::IPv6; |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 136 | } |
| 137 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 138 | throw std::runtime_error("Invalid addr type"); |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 139 | } |
| 140 | |
William A. Kennington III | 24957b9 | 2021-12-03 13:59:19 -0800 | [diff] [blame] | 141 | bool EthernetInterface::dhcpIsEnabled(IP::Protocol family) |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 142 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 143 | switch (family) |
| 144 | { |
| 145 | case IP::Protocol::IPv6: |
| 146 | return dhcp6(); |
| 147 | case IP::Protocol::IPv4: |
| 148 | return dhcp4(); |
| 149 | } |
| 150 | throw std::logic_error("Unreachable"); |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 153 | bool EthernetInterface::originIsManuallyAssigned(IP::AddressOrigin origin) |
| 154 | { |
| 155 | return ( |
| 156 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
| 157 | (origin == IP::AddressOrigin::Static) |
| 158 | #else |
| 159 | (origin == IP::AddressOrigin::Static || |
| 160 | origin == IP::AddressOrigin::LinkLocal) |
| 161 | #endif |
| 162 | |
| 163 | ); |
| 164 | } |
| 165 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 166 | void EthernetInterface::createIPAddressObjects() |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 167 | { |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 168 | addrs.clear(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 169 | |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 170 | AddressFilter filter; |
| 171 | filter.interface = ifIndex(); |
| 172 | auto currentAddrs = getCurrentAddresses(filter); |
| 173 | for (const auto& addr : currentAddrs) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 174 | { |
William A. Kennington III | 13d1708 | 2021-11-04 21:36:54 -0700 | [diff] [blame] | 175 | if (addr.flags & IFA_F_DEPRECATED) |
| 176 | { |
| 177 | continue; |
| 178 | } |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 179 | auto address = toString(addr.address); |
| 180 | IP::Protocol addressType = getProtocol(addr.address); |
William A. Kennington III | fbafa25 | 2018-11-30 16:53:52 -0800 | [diff] [blame] | 181 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 182 | if (dhcpIsEnabled(addressType)) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 183 | { |
| 184 | origin = IP::AddressOrigin::DHCP; |
| 185 | } |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 186 | if (addr.scope == RT_SCOPE_LINK) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 187 | { |
| 188 | origin = IP::AddressOrigin::LinkLocal; |
| 189 | } |
William A. Kennington III | f8c78f2 | 2019-04-20 20:32:59 -0700 | [diff] [blame] | 190 | // Obsolete parameter |
| 191 | std::string gateway = ""; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 192 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 193 | std::string ipAddressObjectPath = generateObjectPath( |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 194 | addressType, address, addr.prefix, gateway, origin); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 195 | |
Lei YU | 7233c58 | 2021-04-08 14:39:43 +0800 | [diff] [blame] | 196 | this->addrs.insert_or_assign( |
William A. Kennington III | c2e5e0e | 2019-04-22 01:26:06 -0700 | [diff] [blame] | 197 | address, std::make_shared<phosphor::network::IPAddress>( |
| 198 | bus, ipAddressObjectPath.c_str(), *this, addressType, |
| 199 | address, origin, addr.prefix, gateway)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 200 | } |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 201 | } |
| 202 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 203 | void EthernetInterface::createStaticNeighborObjects() |
| 204 | { |
| 205 | staticNeighbors.clear(); |
| 206 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 207 | NeighborFilter filter; |
| 208 | filter.interface = ifIndex(); |
| 209 | filter.state = NUD_PERMANENT; |
| 210 | auto neighbors = getCurrentNeighbors(filter); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 211 | for (const auto& neighbor : neighbors) |
| 212 | { |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 213 | if (!neighbor.mac) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 214 | { |
| 215 | continue; |
| 216 | } |
| 217 | std::string ip = toString(neighbor.address); |
| 218 | std::string mac = mac_address::toString(*neighbor.mac); |
| 219 | std::string objectPath = generateStaticNeighborObjectPath(ip, mac); |
| 220 | staticNeighbors.emplace(ip, |
| 221 | std::make_shared<phosphor::network::Neighbor>( |
| 222 | bus, objectPath.c_str(), *this, ip, mac, |
| 223 | Neighbor::State::Permanent)); |
| 224 | } |
| 225 | } |
| 226 | |
William A. Kennington III | d7946a7 | 2019-04-19 14:24:09 -0700 | [diff] [blame] | 227 | unsigned EthernetInterface::ifIndex() const |
| 228 | { |
| 229 | unsigned idx = if_nametoindex(interfaceName().c_str()); |
| 230 | if (idx == 0) |
| 231 | { |
| 232 | throw std::system_error(errno, std::generic_category(), |
| 233 | "if_nametoindex"); |
| 234 | } |
| 235 | return idx; |
| 236 | } |
| 237 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 238 | ObjectPath EthernetInterface::ip(IP::Protocol protType, std::string ipaddress, |
raviteja-b | ce37956 | 2019-03-28 05:59:36 -0500 | [diff] [blame] | 239 | uint8_t prefixLength, std::string gateway) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 240 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 241 | if (dhcpIsEnabled(protType)) |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 242 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 243 | log<level::INFO>("DHCP enabled on the interface, disabling"), |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 244 | entry("INTERFACE=%s", interfaceName().c_str()); |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 245 | switch (protType) |
| 246 | { |
| 247 | case IP::Protocol::IPv4: |
| 248 | dhcp4(false); |
| 249 | break; |
| 250 | case IP::Protocol::IPv6: |
| 251 | dhcp6(false); |
| 252 | break; |
| 253 | } |
Ravi Teja | 0745044 | 2022-07-07 04:30:57 -0500 | [diff] [blame] | 254 | // Delete the IP address object and that reloads the networkd |
| 255 | // to allow the same IP address to be set as Static IP |
| 256 | deleteObject(ipaddress); |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 257 | } |
| 258 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 259 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
| 260 | |
| 261 | int addressFamily = (protType == IP::Protocol::IPv4) ? AF_INET : AF_INET6; |
| 262 | |
| 263 | if (!isValidIP(addressFamily, ipaddress)) |
| 264 | { |
| 265 | log<level::ERR>("Not a valid IP address"), |
| 266 | entry("ADDRESS=%s", ipaddress.c_str()); |
| 267 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"), |
| 268 | Argument::ARGUMENT_VALUE(ipaddress.c_str())); |
| 269 | } |
| 270 | |
William A. Kennington III | f8c78f2 | 2019-04-20 20:32:59 -0700 | [diff] [blame] | 271 | // Gateway is an obsolete parameter |
| 272 | gateway = ""; |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 273 | |
| 274 | if (!isValidPrefix(addressFamily, prefixLength)) |
| 275 | { |
| 276 | log<level::ERR>("PrefixLength is not correct "), |
William A. Kennington III | f8c78f2 | 2019-04-20 20:32:59 -0700 | [diff] [blame] | 277 | entry("PREFIXLENGTH=%" PRIu8, prefixLength); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 278 | elog<InvalidArgument>( |
| 279 | Argument::ARGUMENT_NAME("prefixLength"), |
| 280 | Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str())); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 281 | } |
| 282 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 283 | std::string objectPath = |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 284 | generateObjectPath(protType, ipaddress, prefixLength, gateway, origin); |
Lei YU | 7233c58 | 2021-04-08 14:39:43 +0800 | [diff] [blame] | 285 | this->addrs.insert_or_assign(ipaddress, |
| 286 | std::make_shared<phosphor::network::IPAddress>( |
| 287 | bus, objectPath.c_str(), *this, protType, |
| 288 | ipaddress, origin, prefixLength, gateway)); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 289 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 290 | writeConfigurationFile(); |
| 291 | manager.reloadConfigs(); |
| 292 | |
raviteja-b | ce37956 | 2019-03-28 05:59:36 -0500 | [diff] [blame] | 293 | return objectPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 294 | } |
| 295 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 296 | ObjectPath EthernetInterface::neighbor(std::string ipAddress, |
| 297 | std::string macAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 298 | { |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 299 | if (!isValidIP(ipAddress)) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 300 | { |
| 301 | log<level::ERR>("Not a valid IP address", |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 302 | entry("ADDRESS=%s", ipAddress.c_str())); |
| 303 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipAddress"), |
| 304 | Argument::ARGUMENT_VALUE(ipAddress.c_str())); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 305 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 306 | if (!mac_address::isUnicast(mac_address::fromString(macAddress))) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 307 | { |
| 308 | log<level::ERR>("Not a valid MAC address", |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 309 | entry("MACADDRESS=%s", ipAddress.c_str())); |
| 310 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("macAddress"), |
| 311 | Argument::ARGUMENT_VALUE(macAddress.c_str())); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | std::string objectPath = |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 315 | generateStaticNeighborObjectPath(ipAddress, macAddress); |
| 316 | staticNeighbors.emplace(ipAddress, |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 317 | std::make_shared<phosphor::network::Neighbor>( |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 318 | bus, objectPath.c_str(), *this, ipAddress, |
| 319 | macAddress, Neighbor::State::Permanent)); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 320 | |
| 321 | writeConfigurationFile(); |
| 322 | manager.reloadConfigs(); |
| 323 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 324 | return objectPath; |
| 325 | } |
| 326 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 327 | #ifdef NIC_SUPPORTS_ETHTOOL |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 328 | /* |
Johnathan Mantey | ad4bf5c | 2020-01-24 13:30:39 -0800 | [diff] [blame] | 329 | Enable this code if your NIC driver supports the ETHTOOL features. |
| 330 | Do this by adding the following to your phosphor-network*.bbappend file. |
| 331 | EXTRA_OECONF_append = " --enable-nic-ethtool=yes" |
| 332 | The default compile mode is to omit getInterfaceInfo() |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 333 | */ |
| 334 | InterfaceInfo EthernetInterface::getInterfaceInfo() const |
| 335 | { |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 336 | ifreq ifr = {}; |
| 337 | ethtool_cmd edata = {}; |
| 338 | LinkSpeed speed = {}; |
| 339 | Autoneg autoneg = {}; |
| 340 | DuplexMode duplex = {}; |
| 341 | LinkUp linkState = {}; |
| 342 | NICEnabled enabled = {}; |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 343 | MTU mtuSize = {}; |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 344 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 345 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IFNAMSIZ - 1); |
| 346 | ifr.ifr_data = reinterpret_cast<char*>(&edata); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 347 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 348 | edata.cmd = ETHTOOL_GSET; |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 349 | try |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 350 | { |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 351 | getIFSock().ioctl(SIOCETHTOOL, &ifr); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 352 | speed = edata.speed; |
| 353 | duplex = edata.duplex; |
| 354 | autoneg = edata.autoneg; |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 355 | } |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 356 | catch (const std::exception& e) |
| 357 | { |
| 358 | } |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 359 | |
William A. Kennington III | 9620331 | 2021-05-07 12:50:41 -0700 | [diff] [blame] | 360 | enabled = nicEnabled(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 361 | linkState = linkUp(); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 362 | mtuSize = mtu(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 363 | |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 364 | return std::make_tuple(speed, duplex, autoneg, linkState, enabled, mtuSize); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 365 | } |
Johnathan Mantey | ad4bf5c | 2020-01-24 13:30:39 -0800 | [diff] [blame] | 366 | #endif |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 367 | |
| 368 | /** @brief get the mac address of the interface. |
| 369 | * @return macaddress on success |
| 370 | */ |
| 371 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 372 | std::string |
| 373 | EthernetInterface::getMACAddress(const std::string& interfaceName) const |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 374 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 375 | std::string activeMACAddr = MacAddressIntf::macAddress(); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 376 | |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 377 | ifreq ifr = {}; |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 378 | std::strncpy(ifr.ifr_name, interfaceName.c_str(), IFNAMSIZ - 1); |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 379 | try |
| 380 | { |
| 381 | getIFSock().ioctl(SIOCGIFHWADDR, &ifr); |
| 382 | } |
| 383 | catch (const std::exception& e) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 384 | { |
Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame] | 385 | log<level::ERR>("ioctl failed for SIOCGIFHWADDR:", |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 386 | entry("ERROR=%s", e.what())); |
William A. Kennington III | 7ed1b28 | 2019-04-21 23:38:42 -0700 | [diff] [blame] | 387 | elog<InternalFailure>(); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 388 | } |
William A. Kennington III | 7c4c9a9 | 2022-10-07 19:08:09 -0700 | [diff] [blame^] | 389 | return mac_address::toString( |
| 390 | stdplus::raw::refFrom<ether_addr>(ifr.ifr_hwaddr.sa_data)); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 391 | } |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 392 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 393 | std::string EthernetInterface::generateId(const std::string& ipaddress, |
| 394 | uint8_t prefixLength, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 395 | const std::string& gateway, |
| 396 | const std::string& origin) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 397 | { |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 398 | std::stringstream hexId; |
| 399 | std::string hashString = ipaddress; |
| 400 | hashString += std::to_string(prefixLength); |
| 401 | hashString += gateway; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 402 | hashString += origin; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 403 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 404 | // Only want 8 hex digits. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 405 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 406 | return hexId.str(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 407 | } |
| 408 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 409 | std::string EthernetInterface::generateNeighborId(const std::string& ipAddress, |
| 410 | const std::string& macAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 411 | { |
| 412 | std::stringstream hexId; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 413 | std::string hashString = ipAddress + macAddress; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 414 | |
| 415 | // Only want 8 hex digits. |
| 416 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
| 417 | return hexId.str(); |
| 418 | } |
| 419 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 420 | void EthernetInterface::deleteObject(const std::string& ipaddress) |
| 421 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 422 | auto it = addrs.find(ipaddress); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 423 | if (it == addrs.end()) |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 424 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 425 | log<level::ERR>("DeleteObject:Unable to find the object."); |
| 426 | return; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 427 | } |
| 428 | this->addrs.erase(it); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 429 | |
| 430 | writeConfigurationFile(); |
| 431 | manager.reloadConfigs(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 432 | } |
| 433 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 434 | void EthernetInterface::deleteStaticNeighborObject(const std::string& ipAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 435 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 436 | auto it = staticNeighbors.find(ipAddress); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 437 | if (it == staticNeighbors.end()) |
| 438 | { |
| 439 | log<level::ERR>( |
| 440 | "DeleteStaticNeighborObject:Unable to find the object."); |
| 441 | return; |
| 442 | } |
| 443 | staticNeighbors.erase(it); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 444 | |
| 445 | writeConfigurationFile(); |
| 446 | manager.reloadConfigs(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 449 | void EthernetInterface::deleteVLANFromSystem(const std::string& interface) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 450 | { |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 451 | const auto& confDir = manager.getConfDir(); |
| 452 | auto networkFile = config::pathForIntfConf(confDir, interface); |
| 453 | auto deviceFile = config::pathForIntfDev(confDir, interface); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 454 | |
| 455 | // delete the vlan network file |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 456 | std::error_code ec; |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 457 | std::filesystem::remove(networkFile, ec); |
| 458 | std::filesystem::remove(deviceFile, ec); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 459 | |
| 460 | // TODO systemd doesn't delete the virtual network interface |
| 461 | // even after deleting all the related configuartion. |
| 462 | // https://github.com/systemd/systemd/issues/6600 |
| 463 | try |
| 464 | { |
| 465 | deleteInterface(interface); |
| 466 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 467 | catch (const InternalFailure& e) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 468 | { |
| 469 | commit<InternalFailure>(); |
| 470 | } |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void EthernetInterface::deleteVLANObject(const std::string& interface) |
| 474 | { |
| 475 | auto it = vlanInterfaces.find(interface); |
| 476 | if (it == vlanInterfaces.end()) |
| 477 | { |
| 478 | log<level::ERR>("DeleteVLANObject:Unable to find the object", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 479 | entry("INTERFACE=%s", interface.c_str())); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
| 483 | deleteVLANFromSystem(interface); |
| 484 | // delete the interface |
| 485 | vlanInterfaces.erase(it); |
| 486 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 487 | writeConfigurationFile(); |
| 488 | manager.reloadConfigs(); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 489 | } |
| 490 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 491 | std::string EthernetInterface::generateObjectPath( |
| 492 | IP::Protocol addressType, const std::string& ipaddress, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 493 | uint8_t prefixLength, const std::string& gateway, |
| 494 | IP::AddressOrigin origin) const |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 495 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 496 | std::string type = convertForMessage(addressType); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 497 | type = type.substr(type.rfind('.') + 1); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 498 | std::transform(type.begin(), type.end(), type.begin(), ::tolower); |
| 499 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 500 | std::filesystem::path objectPath; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 501 | objectPath /= objPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 502 | objectPath /= type; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 503 | objectPath /= |
| 504 | generateId(ipaddress, prefixLength, gateway, convertForMessage(origin)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 505 | return objectPath.string(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 506 | } |
| 507 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 508 | std::string EthernetInterface::generateStaticNeighborObjectPath( |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 509 | const std::string& ipAddress, const std::string& macAddress) const |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 510 | { |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 511 | std::filesystem::path objectPath; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 512 | objectPath /= objPath; |
| 513 | objectPath /= "static_neighbor"; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 514 | objectPath /= generateNeighborId(ipAddress, macAddress); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 515 | return objectPath.string(); |
| 516 | } |
| 517 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 518 | bool EthernetInterface::ipv6AcceptRA(bool value) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 519 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 520 | if (ipv6AcceptRA() != EthernetInterfaceIntf::ipv6AcceptRA(value)) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 521 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 522 | writeConfigurationFile(); |
| 523 | manager.reloadConfigs(); |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 524 | } |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 525 | return value; |
| 526 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 527 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 528 | bool EthernetInterface::dhcp4(bool value) |
| 529 | { |
| 530 | if (dhcp4() != EthernetInterfaceIntf::dhcp4(value)) |
| 531 | { |
| 532 | writeConfigurationFile(); |
| 533 | manager.reloadConfigs(); |
| 534 | } |
| 535 | return value; |
| 536 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 537 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 538 | bool EthernetInterface::dhcp6(bool value) |
| 539 | { |
| 540 | if (dhcp6() != EthernetInterfaceIntf::dhcp6(value)) |
| 541 | { |
| 542 | writeConfigurationFile(); |
| 543 | manager.reloadConfigs(); |
| 544 | } |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 545 | return value; |
| 546 | } |
| 547 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 548 | EthernetInterface::DHCPConf EthernetInterface::dhcpEnabled(DHCPConf value) |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 549 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 550 | auto old4 = EthernetInterfaceIntf::dhcp4(); |
| 551 | auto new4 = EthernetInterfaceIntf::dhcp4(value == DHCPConf::v4 || |
| 552 | value == DHCPConf::v4v6stateless || |
| 553 | value == DHCPConf::both); |
| 554 | auto old6 = EthernetInterfaceIntf::dhcp6(); |
| 555 | auto new6 = EthernetInterfaceIntf::dhcp6(value == DHCPConf::v6 || |
| 556 | value == DHCPConf::both); |
| 557 | auto oldra = EthernetInterfaceIntf::ipv6AcceptRA(); |
| 558 | auto newra = EthernetInterfaceIntf::ipv6AcceptRA( |
| 559 | value == DHCPConf::v6stateless || value == DHCPConf::v4v6stateless || |
| 560 | value == DHCPConf::v6 || value == DHCPConf::both); |
| 561 | |
| 562 | if (old4 != new4 || old6 != new6 || oldra != newra) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 563 | { |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 564 | writeConfigurationFile(); |
| 565 | manager.reloadConfigs(); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 566 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 567 | return value; |
| 568 | } |
| 569 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 570 | EthernetInterface::DHCPConf EthernetInterface::dhcpEnabled() const |
| 571 | { |
| 572 | if (dhcp6()) |
| 573 | { |
| 574 | return dhcp4() ? DHCPConf::both : DHCPConf::v6; |
| 575 | } |
| 576 | else if (dhcp4()) |
| 577 | { |
| 578 | return ipv6AcceptRA() ? DHCPConf::v4v6stateless : DHCPConf::v4; |
| 579 | } |
| 580 | return ipv6AcceptRA() ? DHCPConf::v6stateless : DHCPConf::none; |
| 581 | } |
| 582 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 583 | bool EthernetInterface::linkUp() const |
| 584 | { |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 585 | bool value = EthernetInterfaceIntf::linkUp(); |
| 586 | |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 587 | ifreq ifr = {}; |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 588 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1); |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 589 | try |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 590 | { |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 591 | getIFSock().ioctl(SIOCGIFFLAGS, &ifr); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 592 | value = static_cast<bool>(ifr.ifr_flags & IFF_RUNNING); |
| 593 | } |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 594 | catch (const std::exception& e) |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 595 | { |
| 596 | log<level::ERR>("ioctl failed for SIOCGIFFLAGS:", |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 597 | entry("ERROR=%s", e.what())); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 598 | } |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 599 | return value; |
| 600 | } |
| 601 | |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 602 | size_t EthernetInterface::mtu() const |
| 603 | { |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 604 | size_t value = EthernetInterfaceIntf::mtu(); |
| 605 | |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 606 | ifreq ifr = {}; |
| 607 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1); |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 608 | try |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 609 | { |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 610 | getIFSock().ioctl(SIOCGIFMTU, &ifr); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 611 | value = ifr.ifr_mtu; |
| 612 | } |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 613 | catch (const std::exception& e) |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 614 | { |
| 615 | log<level::ERR>("ioctl failed for SIOCGIFMTU:", |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 616 | entry("ERROR=%s", e.what())); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 617 | } |
| 618 | return value; |
| 619 | } |
| 620 | |
| 621 | size_t EthernetInterface::mtu(size_t value) |
| 622 | { |
| 623 | if (value == EthernetInterfaceIntf::mtu()) |
| 624 | { |
| 625 | return value; |
| 626 | } |
| 627 | else if (value == 0) |
| 628 | { |
| 629 | return EthernetInterfaceIntf::mtu(); |
| 630 | } |
| 631 | |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 632 | ifreq ifr = {}; |
| 633 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1); |
| 634 | ifr.ifr_mtu = value; |
| 635 | |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 636 | try |
| 637 | { |
| 638 | getIFSock().ioctl(SIOCSIFMTU, &ifr); |
| 639 | } |
| 640 | catch (const std::exception& e) |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 641 | { |
| 642 | log<level::ERR>("ioctl failed for SIOCSIFMTU:", |
| 643 | entry("ERROR=%s", strerror(errno))); |
| 644 | return EthernetInterfaceIntf::mtu(); |
| 645 | } |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 646 | |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 647 | EthernetInterfaceIntf::mtu(value); |
Tejas Patil | 2c0fc56 | 2021-08-03 19:13:46 +0530 | [diff] [blame] | 648 | return value; |
| 649 | } |
| 650 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 651 | bool EthernetInterface::queryNicEnabled() const |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 652 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 653 | constexpr auto svc = "org.freedesktop.network1"; |
| 654 | constexpr auto intf = "org.freedesktop.network1.Link"; |
| 655 | constexpr auto prop = "AdministrativeState"; |
| 656 | char* rpath; |
| 657 | sd_bus_path_encode("/org/freedesktop/network1/link", |
| 658 | std::to_string(ifIndex()).c_str(), &rpath); |
| 659 | std::string path(rpath); |
| 660 | free(rpath); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 661 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 662 | // Store / Parser for the AdministrativeState return value |
| 663 | std::optional<bool> ret; |
| 664 | auto cb = [&](const std::string& state) { |
| 665 | if (state != "initialized") |
| 666 | { |
| 667 | ret = state != "unmanaged"; |
| 668 | } |
| 669 | }; |
| 670 | |
| 671 | // Build a matcher before making the property call to ensure we |
| 672 | // can eventually get the value. |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 673 | sdbusplus::bus::match_t match( |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 674 | bus, |
| 675 | fmt::format("type='signal',sender='{}',path='{}',interface='{}',member=" |
| 676 | "'PropertiesChanged',arg0='{}',", |
| 677 | svc, path, PROPERTY_INTERFACE, intf) |
| 678 | .c_str(), |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 679 | [&](sdbusplus::message_t& m) { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 680 | std::string intf; |
| 681 | std::unordered_map<std::string, std::variant<std::string>> values; |
| 682 | try |
| 683 | { |
| 684 | m.read(intf, values); |
| 685 | auto it = values.find(prop); |
| 686 | // Ignore properties that aren't AdministrativeState |
| 687 | if (it != values.end()) |
| 688 | { |
| 689 | cb(std::get<std::string>(it->second)); |
| 690 | } |
| 691 | } |
| 692 | catch (const std::exception& e) |
| 693 | { |
| 694 | log<level::ERR>( |
| 695 | fmt::format( |
| 696 | "AdministrativeState match parsing failed on {}: {}", |
| 697 | interfaceName(), e.what()) |
| 698 | .c_str(), |
| 699 | entry("INTERFACE=%s", interfaceName().c_str()), |
| 700 | entry("ERROR=%s", e.what())); |
| 701 | } |
| 702 | }); |
| 703 | |
| 704 | // Actively call for the value in case the interface is already configured |
| 705 | auto method = |
| 706 | bus.new_method_call(svc, path.c_str(), PROPERTY_INTERFACE, METHOD_GET); |
| 707 | method.append(intf, prop); |
| 708 | try |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 709 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 710 | auto reply = bus.call(method); |
| 711 | std::variant<std::string> state; |
| 712 | reply.read(state); |
| 713 | cb(std::get<std::string>(state)); |
| 714 | } |
| 715 | catch (const std::exception& e) |
| 716 | { |
| 717 | log<level::ERR>( |
| 718 | fmt::format("Failed to get AdministrativeState on {}: {}", |
| 719 | interfaceName(), e.what()) |
| 720 | .c_str(), |
| 721 | entry("INTERFACE=%s", interfaceName().c_str()), |
| 722 | entry("ERROR=%s", e.what())); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 723 | } |
| 724 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 725 | // The interface is not yet configured by systemd-networkd, wait until it |
| 726 | // signals us a valid state. |
| 727 | while (!ret) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 728 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 729 | bus.wait(); |
| 730 | bus.process_discard(); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 731 | } |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 732 | |
| 733 | return *ret; |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 734 | } |
| 735 | |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 736 | static void setNICAdminState(stdplus::const_zstring intf, bool up) |
William A. Kennington III | 4209cee | 2021-10-23 18:14:21 -0700 | [diff] [blame] | 737 | { |
| 738 | ifreq ifr = {}; |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 739 | std::strncpy(ifr.ifr_name, intf.data(), IF_NAMESIZE - 1); |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 740 | getIFSock().ioctl(SIOCGIFFLAGS, &ifr); |
William A. Kennington III | 4209cee | 2021-10-23 18:14:21 -0700 | [diff] [blame] | 741 | |
| 742 | ifr.ifr_flags &= ~IFF_UP; |
| 743 | ifr.ifr_flags |= up ? IFF_UP : 0; |
William A. Kennington III | 5dad2aa | 2022-01-21 16:00:17 -0800 | [diff] [blame] | 744 | getIFSock().ioctl(SIOCSIFFLAGS, &ifr); |
William A. Kennington III | 4209cee | 2021-10-23 18:14:21 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 747 | bool EthernetInterface::nicEnabled(bool value) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 748 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 749 | if (value == EthernetInterfaceIntf::nicEnabled()) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 750 | { |
| 751 | return value; |
| 752 | } |
| 753 | |
William A. Kennington III | c922d5e | 2022-01-21 01:08:31 -0800 | [diff] [blame] | 754 | EthernetInterfaceIntf::nicEnabled(value); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 755 | writeConfigurationFile(); |
William A. Kennington III | 329b5fb | 2021-11-09 17:19:30 -0800 | [diff] [blame] | 756 | if (!value) |
| 757 | { |
| 758 | // We only need to bring down the interface, networkd will always bring |
| 759 | // up managed interfaces |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 760 | manager.addReloadPreHook( |
| 761 | [ifname = interfaceName()]() { setNICAdminState(ifname, false); }); |
William A. Kennington III | 329b5fb | 2021-11-09 17:19:30 -0800 | [diff] [blame] | 762 | } |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 763 | manager.reloadConfigs(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 764 | |
| 765 | return value; |
| 766 | } |
| 767 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 768 | ServerList EthernetInterface::staticNameServers(ServerList value) |
| 769 | { |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 770 | for (const auto& nameserverip : value) |
| 771 | { |
William A. Kennington III | ff12acb | 2022-10-07 19:06:56 -0700 | [diff] [blame] | 772 | if (!isValidIP(nameserverip)) |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 773 | { |
| 774 | log<level::ERR>("Not a valid IP address"), |
| 775 | entry("ADDRESS=%s", nameserverip.c_str()); |
| 776 | elog<InvalidArgument>( |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 777 | Argument::ARGUMENT_NAME("StaticNameserver"), |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 778 | Argument::ARGUMENT_VALUE(nameserverip.c_str())); |
| 779 | } |
| 780 | } |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 781 | try |
| 782 | { |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 783 | EthernetInterfaceIntf::staticNameServers(value); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 784 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 785 | writeConfigurationFile(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 786 | manager.reloadConfigs(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 787 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 788 | catch (const InternalFailure& e) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 789 | { |
| 790 | log<level::ERR>("Exception processing DNS entries"); |
| 791 | } |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 792 | return EthernetInterfaceIntf::staticNameServers(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 793 | } |
| 794 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 795 | void EthernetInterface::loadNameServers(const config::Parser& config) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 796 | { |
| 797 | EthernetInterfaceIntf::nameservers(getNameServerFromResolvd()); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 798 | EthernetInterfaceIntf::staticNameServers( |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 799 | config.map.getValueStrings("Network", "DNS")); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 800 | } |
| 801 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 802 | ServerList EthernetInterface::getNameServerFromResolvd() |
| 803 | { |
| 804 | ServerList servers; |
| 805 | std::string OBJ_PATH = RESOLVED_SERVICE_PATH + std::to_string(ifIndex()); |
| 806 | |
| 807 | /* |
| 808 | The DNS property under org.freedesktop.resolve1.Link interface contains |
| 809 | an array containing all DNS servers currently used by resolved. It |
| 810 | contains similar information as the DNS server data written to |
| 811 | /run/systemd/resolve/resolv.conf. |
| 812 | |
| 813 | Each structure in the array consists of a numeric network interface index, |
| 814 | an address family, and a byte array containing the DNS server address |
| 815 | (either 4 bytes in length for IPv4 or 16 bytes in lengths for IPv6). |
| 816 | The array contains DNS servers configured system-wide, including those |
| 817 | possibly read from a foreign /etc/resolv.conf or the DNS= setting in |
| 818 | /etc/systemd/resolved.conf, as well as per-interface DNS server |
| 819 | information either retrieved from systemd-networkd or configured by |
| 820 | external software via SetLinkDNS(). |
| 821 | */ |
| 822 | |
| 823 | using type = std::vector<std::tuple<int32_t, std::vector<uint8_t>>>; |
| 824 | std::variant<type> name; // Variable to capture the DNS property |
| 825 | auto method = bus.new_method_call(RESOLVED_SERVICE, OBJ_PATH.c_str(), |
| 826 | PROPERTY_INTERFACE, METHOD_GET); |
| 827 | |
| 828 | method.append(RESOLVED_INTERFACE, "DNS"); |
| 829 | auto reply = bus.call(method); |
| 830 | |
| 831 | try |
| 832 | { |
| 833 | reply.read(name); |
| 834 | } |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 835 | catch (const sdbusplus::exception_t& e) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 836 | { |
| 837 | log<level::ERR>("Failed to get DNS information from Systemd-Resolved"); |
| 838 | } |
| 839 | auto tupleVector = std::get_if<type>(&name); |
| 840 | for (auto i = tupleVector->begin(); i != tupleVector->end(); ++i) |
| 841 | { |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 842 | int addressFamily = std::get<0>(*i); |
| 843 | std::vector<uint8_t>& ipaddress = std::get<1>(*i); |
| 844 | |
| 845 | switch (addressFamily) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 846 | { |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 847 | case AF_INET: |
| 848 | if (ipaddress.size() == sizeof(struct in_addr)) |
| 849 | { |
| 850 | servers.push_back(toString( |
| 851 | *reinterpret_cast<struct in_addr*>(ipaddress.data()))); |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | log<level::ERR>( |
| 856 | "Invalid data recived from Systemd-Resolved"); |
| 857 | } |
| 858 | break; |
| 859 | |
| 860 | case AF_INET6: |
| 861 | if (ipaddress.size() == sizeof(struct in6_addr)) |
| 862 | { |
| 863 | servers.push_back(toString( |
| 864 | *reinterpret_cast<struct in6_addr*>(ipaddress.data()))); |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | log<level::ERR>( |
| 869 | "Invalid data recived from Systemd-Resolved"); |
| 870 | } |
| 871 | break; |
| 872 | |
| 873 | default: |
| 874 | log<level::ERR>( |
| 875 | "Unsupported address family in DNS from Systemd-Resolved"); |
| 876 | break; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 877 | } |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 878 | } |
| 879 | return servers; |
| 880 | } |
| 881 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 882 | std::string EthernetInterface::vlanIntfName(VlanId id) const |
| 883 | { |
| 884 | return fmt::format(FMT_COMPILE("{}.{}"), interfaceName(), id); |
| 885 | } |
| 886 | |
| 887 | std::string EthernetInterface::vlanObjPath(VlanId id) const |
| 888 | { |
| 889 | return fmt::format(FMT_COMPILE("{}_{}"), objPath, id); |
| 890 | } |
| 891 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 892 | void EthernetInterface::loadVLAN(VlanId id) |
| 893 | { |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 894 | auto vlanInterfaceName = vlanIntfName(id); |
| 895 | auto path = vlanObjPath(id); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 896 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 897 | config::Parser config( |
| 898 | config::pathForIntfConf(manager.getConfDir(), vlanInterfaceName)); |
| 899 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 900 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
William A. Kennington III | 0caf221 | 2022-08-18 18:15:51 -0700 | [diff] [blame] | 901 | bus, path.c_str(), config, EthernetInterfaceIntf::nicEnabled(), id, |
| 902 | *this, manager); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 903 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 904 | // Fetch the ip address from the system |
| 905 | // and create the dbus object. |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 906 | vlanIntf->createIPAddressObjects(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 907 | vlanIntf->createStaticNeighborObjects(); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 908 | vlanIntf->loadNameServers(config); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 909 | |
| 910 | this->vlanInterfaces.emplace(std::move(vlanInterfaceName), |
| 911 | std::move(vlanIntf)); |
| 912 | } |
| 913 | |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 914 | ObjectPath EthernetInterface::createVLAN(VlanId id) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 915 | { |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 916 | auto vlanInterfaceName = vlanIntfName(id); |
| 917 | if (this->vlanInterfaces.find(vlanInterfaceName) != |
| 918 | this->vlanInterfaces.end()) |
Jiaqing Zhao | 33b4eaa | 2022-04-12 23:11:40 +0800 | [diff] [blame] | 919 | { |
| 920 | log<level::ERR>("VLAN already exists", entry("VLANID=%u", id)); |
| 921 | elog<InvalidArgument>( |
| 922 | Argument::ARGUMENT_NAME("VLANId"), |
| 923 | Argument::ARGUMENT_VALUE(std::to_string(id).c_str())); |
| 924 | } |
| 925 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 926 | auto path = vlanObjPath(id); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 927 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 928 | // Pass the parents nicEnabled property, so that the child |
Manojkiran Eda | ca8b91b | 2020-05-28 09:28:42 +0530 | [diff] [blame] | 929 | // VLAN interface can inherit. |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 930 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
William A. Kennington III | 0caf221 | 2022-08-18 18:15:51 -0700 | [diff] [blame] | 931 | bus, path.c_str(), config::Parser(), |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 932 | EthernetInterfaceIntf::nicEnabled(), id, *this, manager); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 933 | |
| 934 | // write the device file for the vlan interface. |
| 935 | vlanIntf->writeDeviceFile(); |
| 936 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 937 | this->vlanInterfaces.emplace(vlanInterfaceName, std::move(vlanIntf)); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 938 | |
| 939 | writeConfigurationFile(); |
| 940 | manager.reloadConfigs(); |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 941 | |
| 942 | return path; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 943 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 944 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 945 | ServerList EthernetInterface::ntpServers(ServerList servers) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 946 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 947 | auto ntpServers = EthernetInterfaceIntf::ntpServers(servers); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 948 | |
| 949 | writeConfigurationFile(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 950 | manager.reloadConfigs(); |
| 951 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 952 | return ntpServers; |
| 953 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 954 | // Need to merge the below function with the code which writes the |
| 955 | // config file during factory reset. |
| 956 | // TODO openbmc/openbmc#1751 |
| 957 | |
| 958 | void EthernetInterface::writeConfigurationFile() |
| 959 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 960 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 961 | { |
| 962 | intf.second->writeConfigurationFile(); |
| 963 | } |
| 964 | |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 965 | config::Parser config; |
| 966 | config.map["Match"].emplace_back()["Name"].emplace_back(interfaceName()); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 967 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 968 | auto& link = config.map["Link"].emplace_back(); |
Johnathan Mantey | 609c12d | 2022-02-03 09:23:09 -0800 | [diff] [blame] | 969 | #ifdef PERSIST_MAC |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 970 | auto mac = MacAddressIntf::macAddress(); |
| 971 | if (!mac.empty()) |
| 972 | { |
| 973 | link["MACAddress"].emplace_back(mac); |
| 974 | } |
Johnathan Mantey | 609c12d | 2022-02-03 09:23:09 -0800 | [diff] [blame] | 975 | #endif |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 976 | if (!EthernetInterfaceIntf::nicEnabled()) |
| 977 | { |
| 978 | link["Unmanaged"].emplace_back("yes"); |
| 979 | } |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 980 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 981 | { |
| 982 | auto& network = config.map["Network"].emplace_back(); |
| 983 | auto& lla = network["LinkLocalAddressing"]; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 984 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 985 | lla.emplace_back("yes"); |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 986 | #else |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 987 | lla.emplace_back("no"); |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 988 | #endif |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 989 | network["IPv6AcceptRA"].emplace_back(ipv6AcceptRA() ? "true" : "false"); |
| 990 | network["DHCP"].emplace_back(dhcp4() ? (dhcp6() ? "true" : "ipv4") |
| 991 | : (dhcp6() ? "ipv6" : "false")); |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 992 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 993 | auto& vlans = network["VLAN"]; |
| 994 | for (const auto& intf : vlanInterfaces) |
| 995 | { |
| 996 | vlans.emplace_back( |
| 997 | intf.second->EthernetInterface::interfaceName()); |
| 998 | } |
| 999 | } |
| 1000 | { |
| 1001 | auto& ntps = network["NTP"]; |
| 1002 | for (const auto& ntp : EthernetInterfaceIntf::ntpServers()) |
| 1003 | { |
| 1004 | ntps.emplace_back(ntp); |
| 1005 | } |
| 1006 | } |
| 1007 | { |
| 1008 | auto& dnss = network["DNS"]; |
| 1009 | for (const auto& dns : EthernetInterfaceIntf::staticNameServers()) |
| 1010 | { |
| 1011 | dnss.emplace_back(dns); |
| 1012 | } |
| 1013 | } |
| 1014 | { |
| 1015 | auto& address = network["Address"]; |
| 1016 | for (const auto& addr : getAddresses()) |
| 1017 | { |
| 1018 | if (originIsManuallyAssigned(addr.second->origin()) && |
| 1019 | !dhcpIsEnabled(addr.second->type())) |
| 1020 | { |
| 1021 | address.emplace_back( |
| 1022 | fmt::format("{}/{}", addr.second->address(), |
| 1023 | addr.second->prefixLength())); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | { |
| 1028 | auto& gateways = network["Gateway"]; |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 1029 | if (!dhcp4()) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 1030 | { |
| 1031 | auto gateway = EthernetInterfaceIntf::defaultGateway(); |
| 1032 | if (!gateway.empty()) |
| 1033 | { |
| 1034 | gateways.emplace_back(gateway); |
| 1035 | } |
| 1036 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1037 | |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 1038 | if (!dhcp6()) |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 1039 | { |
| 1040 | auto gateway6 = EthernetInterfaceIntf::defaultGateway6(); |
| 1041 | if (!gateway6.empty()) |
| 1042 | { |
| 1043 | gateways.emplace_back(gateway6); |
| 1044 | } |
| 1045 | } |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1046 | } |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1047 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 1048 | config.map["IPv6AcceptRA"].emplace_back()["DHCPv6Client"].emplace_back( |
William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 1049 | dhcp6() ? "true" : "false"); |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1050 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 1051 | auto& neighbors = config.map["Neighbor"]; |
| 1052 | for (const auto& sneighbor : staticNeighbors) |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1053 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 1054 | auto& neighbor = neighbors.emplace_back(); |
| 1055 | neighbor["Address"].emplace_back(sneighbor.second->ipAddress()); |
| 1056 | neighbor["MACAddress"].emplace_back(sneighbor.second->macAddress()); |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1057 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1058 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1059 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 1060 | auto& dhcp = config.map["DHCP"].emplace_back(); |
| 1061 | dhcp["ClientIdentifier"].emplace_back("mac"); |
| 1062 | if (manager.getDHCPConf()) |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1063 | { |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 1064 | const auto& conf = *manager.getDHCPConf(); |
| 1065 | auto dns_enabled = conf.dnsEnabled() ? "true" : "false"; |
| 1066 | dhcp["UseDNS"].emplace_back(dns_enabled); |
| 1067 | dhcp["UseDomains"].emplace_back(dns_enabled); |
| 1068 | dhcp["UseNTP"].emplace_back(conf.ntpEnabled() ? "true" : "false"); |
| 1069 | dhcp["UseHostname"].emplace_back(conf.hostNameEnabled() ? "true" |
| 1070 | : "false"); |
| 1071 | dhcp["SendHostname"].emplace_back( |
| 1072 | conf.sendHostNameEnabled() ? "true" : "false"); |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1073 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1074 | } |
William A. Kennington III | 95a49a2 | 2022-08-18 17:50:05 -0700 | [diff] [blame] | 1075 | auto path = config::pathForIntfConf(manager.getConfDir(), interfaceName()); |
| 1076 | config.writeFile(path); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 1077 | auto msg = fmt::format("Wrote networkd file: {}", path.native()); |
| 1078 | log<level::INFO>(msg.c_str(), entry("FILE=%s", path.c_str())); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1079 | } |
| 1080 | |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 1081 | std::string EthernetInterface::macAddress([[maybe_unused]] std::string value) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1082 | { |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 1083 | #ifdef PERSIST_MAC |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 1084 | ether_addr newMAC; |
| 1085 | try |
| 1086 | { |
| 1087 | newMAC = mac_address::fromString(value); |
| 1088 | } |
Patrick Williams | 5758db3 | 2021-10-06 12:29:22 -0500 | [diff] [blame] | 1089 | catch (const std::invalid_argument&) |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 1090 | { |
| 1091 | log<level::ERR>("MACAddress is not valid.", |
| 1092 | entry("MAC=%s", value.c_str())); |
| 1093 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 1094 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 1095 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1096 | if (!mac_address::isUnicast(newMAC)) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1097 | { |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 1098 | log<level::ERR>("MACAddress is not valid.", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1099 | entry("MAC=%s", value.c_str())); |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 1100 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 1101 | Argument::ARGUMENT_VALUE(value.c_str())); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1102 | } |
| 1103 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1104 | auto interface = interfaceName(); |
Asmitha Karunanithi | 33bc9a9 | 2020-08-13 08:48:33 -0500 | [diff] [blame] | 1105 | std::string validMAC = mac_address::toString(newMAC); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1106 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1107 | // We don't need to update the system if the address is unchanged |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1108 | ether_addr oldMAC = mac_address::fromString(MacAddressIntf::macAddress()); |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 1109 | if (!stdplus::raw::equal(newMAC, oldMAC)) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1110 | { |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1111 | // Update everything that depends on the MAC value |
| 1112 | for (const auto& [name, intf] : vlanInterfaces) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1113 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1114 | intf->MacAddressIntf::macAddress(validMAC); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1115 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1116 | MacAddressIntf::macAddress(validMAC); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1117 | |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1118 | writeConfigurationFile(); |
William A. Kennington III | 6bfdf3e | 2021-11-09 17:15:12 -0800 | [diff] [blame] | 1119 | manager.addReloadPreHook([interface]() { |
| 1120 | // The MAC and LLADDRs will only update if the NIC is already down |
William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 1121 | setNICAdminState(interface, false); |
William A. Kennington III | 6bfdf3e | 2021-11-09 17:15:12 -0800 | [diff] [blame] | 1122 | }); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1123 | manager.reloadConfigs(); |
Ratan Gupta | 677ae12 | 2017-09-18 16:28:50 +0530 | [diff] [blame] | 1124 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1125 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1126 | #ifdef HAVE_UBOOT_ENV |
| 1127 | // 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] | 1128 | auto envVar = interfaceToUbootEthAddr(interface); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1129 | if (envVar) |
| 1130 | { |
Asmitha Karunanithi | 33bc9a9 | 2020-08-13 08:48:33 -0500 | [diff] [blame] | 1131 | // Trimming MAC addresses that are out of range. eg: AA:FF:FF:FF:FF:100; |
| 1132 | // and those having more than 6 bytes. eg: AA:AA:AA:AA:AA:AA:BB |
| 1133 | execute("/sbin/fw_setenv", "fw_setenv", envVar->c_str(), |
| 1134 | validMAC.c_str()); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1135 | } |
| 1136 | #endif // HAVE_UBOOT_ENV |
| 1137 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1138 | return value; |
Jiaqing Zhao | 69cfa31 | 2022-02-18 16:52:55 +0800 | [diff] [blame] | 1139 | #else |
| 1140 | elog<NotAllowed>( |
| 1141 | NotAllowedArgument::REASON("Writing MAC address is not allowed")); |
| 1142 | #endif // PERSIST_MAC |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1143 | } |
| 1144 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1145 | void EthernetInterface::deleteAll() |
| 1146 | { |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1147 | // clear all the ip on the interface |
| 1148 | addrs.clear(); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1149 | |
| 1150 | writeConfigurationFile(); |
| 1151 | manager.reloadConfigs(); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1152 | } |
| 1153 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1154 | std::string EthernetInterface::defaultGateway(std::string gateway) |
| 1155 | { |
| 1156 | auto gw = EthernetInterfaceIntf::defaultGateway(); |
| 1157 | if (gw == gateway) |
| 1158 | { |
| 1159 | return gw; |
| 1160 | } |
| 1161 | |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 1162 | if (!isValidIP(AF_INET, gateway) && !gateway.empty()) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1163 | { |
| 1164 | log<level::ERR>("Not a valid v4 Gateway", |
| 1165 | entry("GATEWAY=%s", gateway.c_str())); |
| 1166 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 1167 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 1168 | } |
| 1169 | gw = EthernetInterfaceIntf::defaultGateway(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1170 | |
| 1171 | writeConfigurationFile(); |
| 1172 | manager.reloadConfigs(); |
| 1173 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1174 | return gw; |
| 1175 | } |
| 1176 | |
| 1177 | std::string EthernetInterface::defaultGateway6(std::string gateway) |
| 1178 | { |
| 1179 | auto gw = EthernetInterfaceIntf::defaultGateway6(); |
| 1180 | if (gw == gateway) |
| 1181 | { |
| 1182 | return gw; |
| 1183 | } |
| 1184 | |
Jiaqing Zhao | c2e061f | 2022-04-07 21:55:48 +0800 | [diff] [blame] | 1185 | if (!isValidIP(AF_INET6, gateway) && !gateway.empty()) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1186 | { |
| 1187 | log<level::ERR>("Not a valid v6 Gateway", |
| 1188 | entry("GATEWAY=%s", gateway.c_str())); |
| 1189 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 1190 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 1191 | } |
| 1192 | gw = EthernetInterfaceIntf::defaultGateway6(gateway); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 1193 | |
| 1194 | writeConfigurationFile(); |
| 1195 | manager.reloadConfigs(); |
| 1196 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1197 | return gw; |
| 1198 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1199 | } // namespace network |
| 1200 | } // namespace phosphor |