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