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