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 | 6dec390f | 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( |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 238 | addressType, addr.ipaddress, addr.prefix, gateway, origin); |
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 = |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 318 | generateObjectPath(protType, ipaddress, prefixLength, gateway, origin); |
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, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 429 | const std::string& gateway, |
| 430 | const std::string& origin) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 431 | { |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 432 | std::stringstream hexId; |
| 433 | std::string hashString = ipaddress; |
| 434 | hashString += std::to_string(prefixLength); |
| 435 | hashString += gateway; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 436 | hashString += origin; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 437 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 438 | // Only want 8 hex digits. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 439 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 440 | return hexId.str(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 441 | } |
| 442 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 443 | std::string EthernetInterface::generateNeighborId(const std::string& ipAddress, |
| 444 | const std::string& macAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 445 | { |
| 446 | std::stringstream hexId; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 447 | std::string hashString = ipAddress + macAddress; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 448 | |
| 449 | // Only want 8 hex digits. |
| 450 | hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); |
| 451 | return hexId.str(); |
| 452 | } |
| 453 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 454 | void EthernetInterface::deleteObject(const std::string& ipaddress) |
| 455 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 456 | auto it = addrs.find(ipaddress); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 457 | if (it == addrs.end()) |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 458 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 459 | log<level::ERR>("DeleteObject:Unable to find the object."); |
| 460 | return; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 461 | } |
| 462 | this->addrs.erase(it); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 463 | manager.writeToConfigurationFile(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 464 | } |
| 465 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 466 | void EthernetInterface::deleteStaticNeighborObject(const std::string& ipAddress) |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 467 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 468 | auto it = staticNeighbors.find(ipAddress); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 469 | if (it == staticNeighbors.end()) |
| 470 | { |
| 471 | log<level::ERR>( |
| 472 | "DeleteStaticNeighborObject:Unable to find the object."); |
| 473 | return; |
| 474 | } |
| 475 | staticNeighbors.erase(it); |
| 476 | manager.writeToConfigurationFile(); |
| 477 | } |
| 478 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 479 | void EthernetInterface::deleteVLANFromSystem(const std::string& interface) |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 480 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 481 | auto confDir = manager.getConfDir(); |
| 482 | fs::path networkFile = confDir; |
| 483 | networkFile /= systemd::config::networkFilePrefix + interface + |
| 484 | systemd::config::networkFileSuffix; |
| 485 | |
| 486 | fs::path deviceFile = confDir; |
| 487 | deviceFile /= interface + systemd::config::deviceFileSuffix; |
| 488 | |
| 489 | // delete the vlan network file |
| 490 | if (fs::is_regular_file(networkFile)) |
| 491 | { |
| 492 | fs::remove(networkFile); |
| 493 | } |
| 494 | |
| 495 | // delete the vlan device file |
| 496 | if (fs::is_regular_file(deviceFile)) |
| 497 | { |
| 498 | fs::remove(deviceFile); |
| 499 | } |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 500 | |
| 501 | // TODO systemd doesn't delete the virtual network interface |
| 502 | // even after deleting all the related configuartion. |
| 503 | // https://github.com/systemd/systemd/issues/6600 |
| 504 | try |
| 505 | { |
| 506 | deleteInterface(interface); |
| 507 | } |
| 508 | catch (InternalFailure& e) |
| 509 | { |
| 510 | commit<InternalFailure>(); |
| 511 | } |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | void EthernetInterface::deleteVLANObject(const std::string& interface) |
| 515 | { |
| 516 | auto it = vlanInterfaces.find(interface); |
| 517 | if (it == vlanInterfaces.end()) |
| 518 | { |
| 519 | log<level::ERR>("DeleteVLANObject:Unable to find the object", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 520 | entry("INTERFACE=%s", interface.c_str())); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
| 524 | deleteVLANFromSystem(interface); |
| 525 | // delete the interface |
| 526 | vlanInterfaces.erase(it); |
| 527 | |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 528 | manager.writeToConfigurationFile(); |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 529 | } |
| 530 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 531 | std::string EthernetInterface::generateObjectPath( |
| 532 | IP::Protocol addressType, const std::string& ipaddress, |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 533 | uint8_t prefixLength, const std::string& gateway, |
| 534 | IP::AddressOrigin origin) const |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 535 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 536 | std::string type = convertForMessage(addressType); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 537 | type = type.substr(type.rfind('.') + 1); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 538 | std::transform(type.begin(), type.end(), type.begin(), ::tolower); |
| 539 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 540 | std::filesystem::path objectPath; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 541 | objectPath /= objPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 542 | objectPath /= type; |
Lei YU | 3402757 | 2021-08-11 15:23:52 +0800 | [diff] [blame] | 543 | objectPath /= |
| 544 | generateId(ipaddress, prefixLength, gateway, convertForMessage(origin)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 545 | return objectPath.string(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 546 | } |
| 547 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 548 | std::string EthernetInterface::generateStaticNeighborObjectPath( |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 549 | const std::string& ipAddress, const std::string& macAddress) const |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 550 | { |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 551 | std::filesystem::path objectPath; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 552 | objectPath /= objPath; |
| 553 | objectPath /= "static_neighbor"; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 554 | objectPath /= generateNeighborId(ipAddress, macAddress); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 555 | return objectPath.string(); |
| 556 | } |
| 557 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 558 | bool EthernetInterface::ipv6AcceptRA(bool value) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 559 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 560 | if (value == EthernetInterfaceIntf::ipv6AcceptRA()) |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 561 | { |
| 562 | return value; |
| 563 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 564 | EthernetInterfaceIntf::ipv6AcceptRA(value); |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 565 | manager.writeToConfigurationFile(); |
| 566 | return value; |
| 567 | } |
| 568 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 569 | EthernetInterface::DHCPConf EthernetInterface::dhcpEnabled(DHCPConf value) |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 570 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 571 | if (value == EthernetInterfaceIntf::dhcpEnabled()) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 572 | { |
| 573 | return value; |
| 574 | } |
| 575 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 576 | EthernetInterfaceIntf::dhcpEnabled(value); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 577 | manager.writeToConfigurationFile(); |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 578 | return value; |
| 579 | } |
| 580 | |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 581 | bool EthernetInterface::linkUp() const |
| 582 | { |
| 583 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 584 | bool value = EthernetInterfaceIntf::linkUp(); |
| 585 | |
| 586 | if (eifSocket.sock < 0) |
| 587 | { |
| 588 | return value; |
| 589 | } |
| 590 | |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 591 | ifreq ifr = {}; |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 592 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1); |
| 593 | if (ioctl(eifSocket.sock, SIOCGIFFLAGS, &ifr) == 0) |
| 594 | { |
| 595 | value = static_cast<bool>(ifr.ifr_flags & IFF_RUNNING); |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | log<level::ERR>("ioctl failed for SIOCGIFFLAGS:", |
| 600 | entry("ERROR=%s", strerror(errno))); |
| 601 | } |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 602 | return value; |
| 603 | } |
| 604 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 605 | bool EthernetInterface::queryNicEnabled() const |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 606 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 607 | constexpr auto svc = "org.freedesktop.network1"; |
| 608 | constexpr auto intf = "org.freedesktop.network1.Link"; |
| 609 | constexpr auto prop = "AdministrativeState"; |
| 610 | char* rpath; |
| 611 | sd_bus_path_encode("/org/freedesktop/network1/link", |
| 612 | std::to_string(ifIndex()).c_str(), &rpath); |
| 613 | std::string path(rpath); |
| 614 | free(rpath); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 615 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 616 | // Store / Parser for the AdministrativeState return value |
| 617 | std::optional<bool> ret; |
| 618 | auto cb = [&](const std::string& state) { |
| 619 | if (state != "initialized") |
| 620 | { |
| 621 | ret = state != "unmanaged"; |
| 622 | } |
| 623 | }; |
| 624 | |
| 625 | // Build a matcher before making the property call to ensure we |
| 626 | // can eventually get the value. |
| 627 | sdbusplus::bus::match::match match( |
| 628 | bus, |
| 629 | fmt::format("type='signal',sender='{}',path='{}',interface='{}',member=" |
| 630 | "'PropertiesChanged',arg0='{}',", |
| 631 | svc, path, PROPERTY_INTERFACE, intf) |
| 632 | .c_str(), |
| 633 | [&](sdbusplus::message::message& m) { |
| 634 | std::string intf; |
| 635 | std::unordered_map<std::string, std::variant<std::string>> values; |
| 636 | try |
| 637 | { |
| 638 | m.read(intf, values); |
| 639 | auto it = values.find(prop); |
| 640 | // Ignore properties that aren't AdministrativeState |
| 641 | if (it != values.end()) |
| 642 | { |
| 643 | cb(std::get<std::string>(it->second)); |
| 644 | } |
| 645 | } |
| 646 | catch (const std::exception& e) |
| 647 | { |
| 648 | log<level::ERR>( |
| 649 | fmt::format( |
| 650 | "AdministrativeState match parsing failed on {}: {}", |
| 651 | interfaceName(), e.what()) |
| 652 | .c_str(), |
| 653 | entry("INTERFACE=%s", interfaceName().c_str()), |
| 654 | entry("ERROR=%s", e.what())); |
| 655 | } |
| 656 | }); |
| 657 | |
| 658 | // Actively call for the value in case the interface is already configured |
| 659 | auto method = |
| 660 | bus.new_method_call(svc, path.c_str(), PROPERTY_INTERFACE, METHOD_GET); |
| 661 | method.append(intf, prop); |
| 662 | try |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 663 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 664 | auto reply = bus.call(method); |
| 665 | std::variant<std::string> state; |
| 666 | reply.read(state); |
| 667 | cb(std::get<std::string>(state)); |
| 668 | } |
| 669 | catch (const std::exception& e) |
| 670 | { |
| 671 | log<level::ERR>( |
| 672 | fmt::format("Failed to get AdministrativeState on {}: {}", |
| 673 | interfaceName(), e.what()) |
| 674 | .c_str(), |
| 675 | entry("INTERFACE=%s", interfaceName().c_str()), |
| 676 | entry("ERROR=%s", e.what())); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 677 | } |
| 678 | |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 679 | // The interface is not yet configured by systemd-networkd, wait until it |
| 680 | // signals us a valid state. |
| 681 | while (!ret) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 682 | { |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 683 | bus.wait(); |
| 684 | bus.process_discard(); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 685 | } |
William A. Kennington III | 26275a3 | 2021-07-13 20:32:42 -0700 | [diff] [blame] | 686 | |
| 687 | return *ret; |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 690 | bool EthernetInterface::nicEnabled(bool value) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 691 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 692 | if (value == EthernetInterfaceIntf::nicEnabled()) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 693 | { |
| 694 | return value; |
| 695 | } |
| 696 | |
| 697 | EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 698 | if (eifSocket.sock < 0) |
| 699 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 700 | return EthernetInterfaceIntf::nicEnabled(); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 701 | } |
| 702 | |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 703 | ifreq ifr = {}; |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 704 | std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1); |
| 705 | if (ioctl(eifSocket.sock, SIOCGIFFLAGS, &ifr) != 0) |
| 706 | { |
| 707 | log<level::ERR>("ioctl failed for SIOCGIFFLAGS:", |
| 708 | entry("ERROR=%s", strerror(errno))); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 709 | return EthernetInterfaceIntf::nicEnabled(); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | ifr.ifr_flags &= ~IFF_UP; |
| 713 | ifr.ifr_flags |= value ? IFF_UP : 0; |
| 714 | |
| 715 | if (ioctl(eifSocket.sock, SIOCSIFFLAGS, &ifr) != 0) |
| 716 | { |
| 717 | log<level::ERR>("ioctl failed for SIOCSIFFLAGS:", |
| 718 | entry("ERROR=%s", strerror(errno))); |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 719 | return EthernetInterfaceIntf::nicEnabled(); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 720 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 721 | EthernetInterfaceIntf::nicEnabled(value); |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 722 | writeConfigurationFile(); |
Johnathan Mantey | faa72e5 | 2020-01-08 10:38:58 -0800 | [diff] [blame] | 723 | |
| 724 | return value; |
| 725 | } |
| 726 | |
Manojkiran Eda | aa57fa5 | 2020-06-13 14:59:53 +0530 | [diff] [blame] | 727 | ServerList EthernetInterface::nameservers(ServerList /*value*/) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 728 | { |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 729 | elog<NotAllowed>(NotAllowedArgument::REASON("ReadOnly Property")); |
| 730 | return EthernetInterfaceIntf::nameservers(); |
| 731 | } |
| 732 | |
| 733 | ServerList EthernetInterface::staticNameServers(ServerList value) |
| 734 | { |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 735 | for (const auto& nameserverip : value) |
| 736 | { |
| 737 | if (!isValidIP(AF_INET, nameserverip) && |
| 738 | !isValidIP(AF_INET6, nameserverip)) |
| 739 | { |
| 740 | log<level::ERR>("Not a valid IP address"), |
| 741 | entry("ADDRESS=%s", nameserverip.c_str()); |
| 742 | elog<InvalidArgument>( |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 743 | Argument::ARGUMENT_NAME("StaticNameserver"), |
Manojkiran Eda | 5fb6c33 | 2019-08-21 16:37:29 +0530 | [diff] [blame] | 744 | Argument::ARGUMENT_VALUE(nameserverip.c_str())); |
| 745 | } |
| 746 | } |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 747 | try |
| 748 | { |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 749 | EthernetInterfaceIntf::staticNameServers(value); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 750 | writeConfigurationFile(); |
Ratan Gupta | b400597 | 2019-09-19 06:19:16 +0530 | [diff] [blame] | 751 | // resolved reads the DNS server configuration from the |
| 752 | // network file. |
| 753 | manager.restartSystemdUnit(networkdService); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 754 | } |
| 755 | catch (InternalFailure& e) |
| 756 | { |
| 757 | log<level::ERR>("Exception processing DNS entries"); |
| 758 | } |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 759 | return EthernetInterfaceIntf::staticNameServers(); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 760 | } |
| 761 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 762 | void EthernetInterface::loadNameServers() |
| 763 | { |
| 764 | EthernetInterfaceIntf::nameservers(getNameServerFromResolvd()); |
| 765 | EthernetInterfaceIntf::staticNameServers(getstaticNameServerFromConf()); |
| 766 | } |
| 767 | |
| 768 | ServerList EthernetInterface::getstaticNameServerFromConf() |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 769 | { |
| 770 | fs::path confPath = manager.getConfDir(); |
| 771 | |
| 772 | std::string fileName = systemd::config::networkFilePrefix + |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 773 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 774 | confPath /= fileName; |
| 775 | ServerList servers; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 776 | config::Parser parser(confPath.string()); |
| 777 | auto rc = config::ReturnCode::SUCCESS; |
| 778 | |
| 779 | std::tie(rc, servers) = parser.getValues("Network", "DNS"); |
| 780 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 781 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 782 | log<level::DEBUG>("Unable to get the value for network[DNS]", |
| 783 | entry("RC=%d", rc)); |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 784 | } |
| 785 | return servers; |
| 786 | } |
| 787 | |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 788 | ServerList EthernetInterface::getNameServerFromResolvd() |
| 789 | { |
| 790 | ServerList servers; |
| 791 | std::string OBJ_PATH = RESOLVED_SERVICE_PATH + std::to_string(ifIndex()); |
| 792 | |
| 793 | /* |
| 794 | The DNS property under org.freedesktop.resolve1.Link interface contains |
| 795 | an array containing all DNS servers currently used by resolved. It |
| 796 | contains similar information as the DNS server data written to |
| 797 | /run/systemd/resolve/resolv.conf. |
| 798 | |
| 799 | Each structure in the array consists of a numeric network interface index, |
| 800 | an address family, and a byte array containing the DNS server address |
| 801 | (either 4 bytes in length for IPv4 or 16 bytes in lengths for IPv6). |
| 802 | The array contains DNS servers configured system-wide, including those |
| 803 | possibly read from a foreign /etc/resolv.conf or the DNS= setting in |
| 804 | /etc/systemd/resolved.conf, as well as per-interface DNS server |
| 805 | information either retrieved from systemd-networkd or configured by |
| 806 | external software via SetLinkDNS(). |
| 807 | */ |
| 808 | |
| 809 | using type = std::vector<std::tuple<int32_t, std::vector<uint8_t>>>; |
| 810 | std::variant<type> name; // Variable to capture the DNS property |
| 811 | auto method = bus.new_method_call(RESOLVED_SERVICE, OBJ_PATH.c_str(), |
| 812 | PROPERTY_INTERFACE, METHOD_GET); |
| 813 | |
| 814 | method.append(RESOLVED_INTERFACE, "DNS"); |
| 815 | auto reply = bus.call(method); |
| 816 | |
| 817 | try |
| 818 | { |
| 819 | reply.read(name); |
| 820 | } |
| 821 | catch (const sdbusplus::exception::SdBusError& e) |
| 822 | { |
| 823 | log<level::ERR>("Failed to get DNS information from Systemd-Resolved"); |
| 824 | } |
| 825 | auto tupleVector = std::get_if<type>(&name); |
| 826 | for (auto i = tupleVector->begin(); i != tupleVector->end(); ++i) |
| 827 | { |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 828 | int addressFamily = std::get<0>(*i); |
| 829 | std::vector<uint8_t>& ipaddress = std::get<1>(*i); |
| 830 | |
| 831 | switch (addressFamily) |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 832 | { |
Alexander Filippov | 983da55 | 2021-02-08 15:26:54 +0300 | [diff] [blame] | 833 | case AF_INET: |
| 834 | if (ipaddress.size() == sizeof(struct in_addr)) |
| 835 | { |
| 836 | servers.push_back(toString( |
| 837 | *reinterpret_cast<struct in_addr*>(ipaddress.data()))); |
| 838 | } |
| 839 | else |
| 840 | { |
| 841 | log<level::ERR>( |
| 842 | "Invalid data recived from Systemd-Resolved"); |
| 843 | } |
| 844 | break; |
| 845 | |
| 846 | case AF_INET6: |
| 847 | if (ipaddress.size() == sizeof(struct in6_addr)) |
| 848 | { |
| 849 | servers.push_back(toString( |
| 850 | *reinterpret_cast<struct in6_addr*>(ipaddress.data()))); |
| 851 | } |
| 852 | else |
| 853 | { |
| 854 | log<level::ERR>( |
| 855 | "Invalid data recived from Systemd-Resolved"); |
| 856 | } |
| 857 | break; |
| 858 | |
| 859 | default: |
| 860 | log<level::ERR>( |
| 861 | "Unsupported address family in DNS from Systemd-Resolved"); |
| 862 | break; |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 863 | } |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 864 | } |
| 865 | return servers; |
| 866 | } |
| 867 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 868 | void EthernetInterface::loadVLAN(VlanId id) |
| 869 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 870 | std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 871 | std::string path = objPath; |
| 872 | path += "_" + std::to_string(id); |
| 873 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 874 | DHCPConf dhcpEnabled = |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 875 | getDHCPValue(manager.getConfDir().string(), vlanInterfaceName); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 876 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 877 | bus, path.c_str(), dhcpEnabled, EthernetInterfaceIntf::nicEnabled(), id, |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 878 | *this, manager); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 879 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 880 | // Fetch the ip address from the system |
| 881 | // and create the dbus object. |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 882 | vlanIntf->createIPAddressObjects(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 883 | vlanIntf->createStaticNeighborObjects(); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 884 | |
| 885 | this->vlanInterfaces.emplace(std::move(vlanInterfaceName), |
| 886 | std::move(vlanIntf)); |
| 887 | } |
| 888 | |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 889 | ObjectPath EthernetInterface::createVLAN(VlanId id) |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 890 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 891 | std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 892 | std::string path = objPath; |
| 893 | path += "_" + std::to_string(id); |
| 894 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 895 | // Pass the parents nicEnabled property, so that the child |
Manojkiran Eda | ca8b91b | 2020-05-28 09:28:42 +0530 | [diff] [blame] | 896 | // VLAN interface can inherit. |
| 897 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 898 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 899 | bus, path.c_str(), EthernetInterface::DHCPConf::none, |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 900 | EthernetInterfaceIntf::nicEnabled(), id, *this, manager); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 901 | |
| 902 | // write the device file for the vlan interface. |
| 903 | vlanIntf->writeDeviceFile(); |
| 904 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 905 | this->vlanInterfaces.emplace(vlanInterfaceName, std::move(vlanIntf)); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 906 | // write the new vlan device entry to the configuration(network) file. |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 907 | manager.writeToConfigurationFile(); |
William A. Kennington III | f4b4ff8 | 2019-04-09 19:06:52 -0700 | [diff] [blame] | 908 | |
| 909 | return path; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 910 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 911 | |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 912 | bool EthernetInterface::getIPv6AcceptRAFromConf() |
| 913 | { |
| 914 | fs::path confPath = manager.getConfDir(); |
| 915 | |
| 916 | std::string fileName = systemd::config::networkFilePrefix + |
| 917 | interfaceName() + systemd::config::networkFileSuffix; |
| 918 | confPath /= fileName; |
| 919 | config::ValueList values; |
| 920 | config::Parser parser(confPath.string()); |
| 921 | auto rc = config::ReturnCode::SUCCESS; |
| 922 | std::tie(rc, values) = parser.getValues("Network", "IPv6AcceptRA"); |
| 923 | if (rc != config::ReturnCode::SUCCESS) |
| 924 | { |
| 925 | log<level::DEBUG>("Unable to get the value for Network[IPv6AcceptRA]", |
| 926 | entry("rc=%d", rc)); |
| 927 | return false; |
| 928 | } |
| 929 | return (values[0] == "true"); |
| 930 | } |
| 931 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 932 | ServerList EthernetInterface::getNTPServersFromConf() |
| 933 | { |
| 934 | fs::path confPath = manager.getConfDir(); |
| 935 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 936 | std::string fileName = systemd::config::networkFilePrefix + |
| 937 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 938 | confPath /= fileName; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 939 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 940 | ServerList servers; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 941 | config::Parser parser(confPath.string()); |
| 942 | auto rc = config::ReturnCode::SUCCESS; |
| 943 | |
| 944 | std::tie(rc, servers) = parser.getValues("Network", "NTP"); |
| 945 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 946 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 947 | log<level::DEBUG>("Unable to get the value for Network[NTP]", |
| 948 | entry("rc=%d", rc)); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 949 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 950 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 951 | return servers; |
| 952 | } |
| 953 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 954 | ServerList EthernetInterface::ntpServers(ServerList servers) |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 955 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 956 | auto ntpServers = EthernetInterfaceIntf::ntpServers(servers); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 957 | |
| 958 | writeConfigurationFile(); |
| 959 | // timesynchd reads the NTP server configuration from the |
| 960 | // network file. |
Ratan Gupta | 895f9e5 | 2018-11-26 20:57:34 +0530 | [diff] [blame] | 961 | manager.restartSystemdUnit(networkdService); |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 962 | return ntpServers; |
| 963 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 964 | // Need to merge the below function with the code which writes the |
| 965 | // config file during factory reset. |
| 966 | // TODO openbmc/openbmc#1751 |
| 967 | |
| 968 | void EthernetInterface::writeConfigurationFile() |
| 969 | { |
| 970 | // write all the static ip address in the systemd-network conf file |
| 971 | |
| 972 | using namespace std::string_literals; |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 973 | namespace fs = std::filesystem; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 974 | |
| 975 | // if there is vlan interafce then write the configuration file |
| 976 | // for vlan also. |
| 977 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 978 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 979 | { |
| 980 | intf.second->writeConfigurationFile(); |
| 981 | } |
| 982 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 983 | fs::path confPath = manager.getConfDir(); |
| 984 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 985 | std::string fileName = systemd::config::networkFilePrefix + |
| 986 | interfaceName() + systemd::config::networkFileSuffix; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 987 | confPath /= fileName; |
| 988 | std::fstream stream; |
| 989 | |
| 990 | stream.open(confPath.c_str(), std::fstream::out); |
| 991 | if (!stream.is_open()) |
| 992 | { |
| 993 | log<level::ERR>("Unable to open the file", |
| 994 | entry("FILE=%s", confPath.c_str())); |
| 995 | elog<InternalFailure>(); |
| 996 | } |
| 997 | |
| 998 | // Write the device |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 999 | stream << "[Match]\n"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1000 | stream << "Name=" << interfaceName() << "\n"; |
| 1001 | |
| 1002 | auto addrs = getAddresses(); |
| 1003 | |
William A. Kennington III | 1578721 | 2019-04-23 19:18:01 -0700 | [diff] [blame] | 1004 | // Write the link section |
| 1005 | stream << "[Link]\n"; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1006 | auto mac = MacAddressIntf::macAddress(); |
William A. Kennington III | 1578721 | 2019-04-23 19:18:01 -0700 | [diff] [blame] | 1007 | if (!mac.empty()) |
| 1008 | { |
| 1009 | stream << "MACAddress=" << mac << "\n"; |
| 1010 | } |
| 1011 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1012 | if (!EthernetInterfaceIntf::nicEnabled()) |
Johnathan Mantey | d0679f9 | 2019-10-29 16:20:28 -0700 | [diff] [blame] | 1013 | { |
| 1014 | stream << "Unmanaged=yes\n"; |
| 1015 | } |
| 1016 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1017 | // write the network section |
Ratan K Gupta | 1a054ae | 2018-09-15 00:49:51 -0400 | [diff] [blame] | 1018 | stream << "[Network]\n"; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 1019 | #ifdef LINK_LOCAL_AUTOCONFIGURATION |
Nagaraju Goruganti | 24afe36 | 2017-09-21 07:40:26 -0500 | [diff] [blame] | 1020 | stream << "LinkLocalAddressing=yes\n"; |
Oskar Senft | ad21fc2 | 2018-07-26 16:32:23 -0400 | [diff] [blame] | 1021 | #else |
| 1022 | stream << "LinkLocalAddressing=no\n"; |
| 1023 | #endif |
Johnathan Mantey | 5b023f5 | 2019-06-24 16:06:37 -0700 | [diff] [blame] | 1024 | stream << std::boolalpha |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1025 | << "IPv6AcceptRA=" << EthernetInterfaceIntf::ipv6AcceptRA() << "\n"; |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 1026 | |
| 1027 | // Add the VLAN entry |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1028 | for (const auto& intf : vlanInterfaces) |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 1029 | { |
| 1030 | stream << "VLAN=" << intf.second->EthernetInterface::interfaceName() |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1031 | << "\n"; |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 1032 | } |
Ratan Gupta | 046b2a0 | 2019-09-20 15:49:51 +0530 | [diff] [blame] | 1033 | // Add the NTP server |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1034 | for (const auto& ntp : EthernetInterfaceIntf::ntpServers()) |
Ratan Gupta | 046b2a0 | 2019-09-20 15:49:51 +0530 | [diff] [blame] | 1035 | { |
| 1036 | stream << "NTP=" << ntp << "\n"; |
| 1037 | } |
| 1038 | |
| 1039 | // Add the DNS entry |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 1040 | for (const auto& dns : EthernetInterfaceIntf::staticNameServers()) |
Ratan Gupta | 046b2a0 | 2019-09-20 15:49:51 +0530 | [diff] [blame] | 1041 | { |
| 1042 | stream << "DNS=" << dns << "\n"; |
| 1043 | } |
| 1044 | |
Nagaraju Goruganti | e8b83ec | 2018-03-26 05:21:45 -0500 | [diff] [blame] | 1045 | // Add the DHCP entry |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1046 | stream << "DHCP="s + |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1047 | mapDHCPToSystemd[EthernetInterfaceIntf::dhcpEnabled()] + "\n"; |
Nagaraju Goruganti | e8b83ec | 2018-03-26 05:21:45 -0500 | [diff] [blame] | 1048 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1049 | // Static IP addresses |
| 1050 | for (const auto& addr : addrs) |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1051 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1052 | if (originIsManuallyAssigned(addr.second->origin()) && |
| 1053 | !dhcpIsEnabled(addr.second->type())) |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1054 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1055 | // Process all static addresses |
| 1056 | std::string address = addr.second->address() + "/" + |
| 1057 | std::to_string(addr.second->prefixLength()); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1058 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1059 | // build the address entries. Do not use [Network] shortcuts to |
| 1060 | // insert address entries. |
| 1061 | stream << "[Address]\n"; |
| 1062 | stream << "Address=" << address << "\n"; |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1063 | } |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1064 | } |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1065 | |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1066 | if (!dhcpIsEnabled(IP::Protocol::IPv4)) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1067 | { |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1068 | auto gateway = EthernetInterfaceIntf::defaultGateway(); |
| 1069 | if (!gateway.empty()) |
| 1070 | { |
| 1071 | stream << "[Route]\n"; |
| 1072 | stream << "Gateway=" << gateway << "\n"; |
| 1073 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1074 | } |
| 1075 | |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1076 | if (!dhcpIsEnabled(IP::Protocol::IPv6)) |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1077 | { |
Lei YU | cb2d408 | 2021-08-12 15:26:49 +0800 | [diff] [blame] | 1078 | auto gateway6 = EthernetInterfaceIntf::defaultGateway6(); |
| 1079 | if (!gateway6.empty()) |
| 1080 | { |
| 1081 | stream << "[Route]\n"; |
| 1082 | stream << "Gateway=" << gateway6 << "\n"; |
| 1083 | } |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1084 | } |
| 1085 | |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 1086 | // Write the neighbor sections |
| 1087 | for (const auto& neighbor : staticNeighbors) |
| 1088 | { |
| 1089 | stream << "[Neighbor]" |
| 1090 | << "\n"; |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1091 | stream << "Address=" << neighbor.second->ipAddress() << "\n"; |
| 1092 | stream << "MACAddress=" << neighbor.second->macAddress() << "\n"; |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 1095 | // Write the dhcp section irrespective of whether DHCP is enabled or not |
| 1096 | writeDHCPSection(stream); |
| 1097 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1098 | stream.close(); |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | void EthernetInterface::writeDHCPSection(std::fstream& stream) |
| 1102 | { |
| 1103 | using namespace std::string_literals; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1104 | // write the dhcp section |
| 1105 | stream << "[DHCP]\n"; |
| 1106 | |
| 1107 | // Hardcoding the client identifier to mac, to address below issue |
| 1108 | // https://github.com/openbmc/openbmc/issues/1280 |
| 1109 | stream << "ClientIdentifier=mac\n"; |
| 1110 | if (manager.getDHCPConf()) |
| 1111 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1112 | auto value = manager.getDHCPConf()->dnsEnabled() ? "true"s : "false"s; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1113 | stream << "UseDNS="s + value + "\n"; |
| 1114 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1115 | value = manager.getDHCPConf()->ntpEnabled() ? "true"s : "false"s; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1116 | stream << "UseNTP="s + value + "\n"; |
| 1117 | |
| 1118 | value = manager.getDHCPConf()->hostNameEnabled() ? "true"s : "false"s; |
| 1119 | stream << "UseHostname="s + value + "\n"; |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 1120 | |
| 1121 | value = |
| 1122 | manager.getDHCPConf()->sendHostNameEnabled() ? "true"s : "false"s; |
| 1123 | stream << "SendHostname="s + value + "\n"; |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1127 | std::string EthernetInterface::macAddress(std::string value) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1128 | { |
Asmitha Karunanithi | 86f659e | 2021-01-05 00:16:03 -0600 | [diff] [blame] | 1129 | ether_addr newMAC; |
| 1130 | try |
| 1131 | { |
| 1132 | newMAC = mac_address::fromString(value); |
| 1133 | } |
| 1134 | catch (std::invalid_argument&) |
| 1135 | { |
| 1136 | log<level::ERR>("MACAddress is not valid.", |
| 1137 | entry("MAC=%s", value.c_str())); |
| 1138 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 1139 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 1140 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1141 | if (!mac_address::isUnicast(newMAC)) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1142 | { |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 1143 | log<level::ERR>("MACAddress is not valid.", |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1144 | entry("MAC=%s", value.c_str())); |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 1145 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"), |
| 1146 | Argument::ARGUMENT_VALUE(value.c_str())); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1147 | } |
| 1148 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1149 | auto interface = interfaceName(); |
Asmitha Karunanithi | 33bc9a9 | 2020-08-13 08:48:33 -0500 | [diff] [blame] | 1150 | std::string validMAC = mac_address::toString(newMAC); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1151 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1152 | // 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] | 1153 | ether_addr oldMAC = mac_address::fromString(MacAddressIntf::macAddress()); |
William A. Kennington III | 12beaad | 2020-06-13 19:30:41 -0700 | [diff] [blame] | 1154 | if (!stdplus::raw::equal(newMAC, oldMAC)) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1155 | { |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1156 | // Update everything that depends on the MAC value |
| 1157 | for (const auto& [name, intf] : vlanInterfaces) |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1158 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1159 | intf->MacAddressIntf::macAddress(validMAC); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1160 | } |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 1161 | MacAddressIntf::macAddress(validMAC); |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1162 | |
William A. Kennington III | 1578721 | 2019-04-23 19:18:01 -0700 | [diff] [blame] | 1163 | // TODO: would remove the call below and |
| 1164 | // just restart systemd-netwokd |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1165 | // through https://github.com/systemd/systemd/issues/6696 |
| 1166 | execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), |
| 1167 | "down"); |
William A. Kennington III | 1578721 | 2019-04-23 19:18:01 -0700 | [diff] [blame] | 1168 | manager.writeToConfigurationFile(); |
Ratan Gupta | 677ae12 | 2017-09-18 16:28:50 +0530 | [diff] [blame] | 1169 | } |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1170 | |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1171 | #ifdef HAVE_UBOOT_ENV |
| 1172 | // Ensure that the valid address is stored in the u-boot-env |
| 1173 | auto envVar = interfaceToUbootEthAddr(interface.c_str()); |
| 1174 | if (envVar) |
| 1175 | { |
Asmitha Karunanithi | 33bc9a9 | 2020-08-13 08:48:33 -0500 | [diff] [blame] | 1176 | // Trimming MAC addresses that are out of range. eg: AA:FF:FF:FF:FF:100; |
| 1177 | // and those having more than 6 bytes. eg: AA:AA:AA:AA:AA:AA:BB |
| 1178 | execute("/sbin/fw_setenv", "fw_setenv", envVar->c_str(), |
| 1179 | validMAC.c_str()); |
Alexander Filippov | 76b2aa3 | 2020-07-10 13:28:55 +0300 | [diff] [blame] | 1180 | } |
| 1181 | #endif // HAVE_UBOOT_ENV |
| 1182 | |
William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 1183 | return value; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 1184 | } |
| 1185 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1186 | void EthernetInterface::deleteAll() |
| 1187 | { |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 1188 | if (dhcpIsEnabled(IP::Protocol::IPv4, true)) |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1189 | { |
| 1190 | log<level::INFO>("DHCP enabled on the interface"), |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1191 | entry("INTERFACE=%s", interfaceName().c_str()); |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | // clear all the ip on the interface |
| 1195 | addrs.clear(); |
| 1196 | manager.writeToConfigurationFile(); |
| 1197 | } |
| 1198 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 1199 | std::string EthernetInterface::defaultGateway(std::string gateway) |
| 1200 | { |
| 1201 | auto gw = EthernetInterfaceIntf::defaultGateway(); |
| 1202 | if (gw == gateway) |
| 1203 | { |
| 1204 | return gw; |
| 1205 | } |
| 1206 | |
| 1207 | if (!isValidIP(AF_INET, gateway)) |
| 1208 | { |
| 1209 | log<level::ERR>("Not a valid v4 Gateway", |
| 1210 | entry("GATEWAY=%s", gateway.c_str())); |
| 1211 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 1212 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 1213 | } |
| 1214 | gw = EthernetInterfaceIntf::defaultGateway(gateway); |
| 1215 | manager.writeToConfigurationFile(); |
| 1216 | return gw; |
| 1217 | } |
| 1218 | |
| 1219 | std::string EthernetInterface::defaultGateway6(std::string gateway) |
| 1220 | { |
| 1221 | auto gw = EthernetInterfaceIntf::defaultGateway6(); |
| 1222 | if (gw == gateway) |
| 1223 | { |
| 1224 | return gw; |
| 1225 | } |
| 1226 | |
| 1227 | if (!isValidIP(AF_INET6, gateway)) |
| 1228 | { |
| 1229 | log<level::ERR>("Not a valid v6 Gateway", |
| 1230 | entry("GATEWAY=%s", gateway.c_str())); |
| 1231 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"), |
| 1232 | Argument::ARGUMENT_VALUE(gateway.c_str())); |
| 1233 | } |
| 1234 | gw = EthernetInterfaceIntf::defaultGateway6(gateway); |
| 1235 | manager.writeToConfigurationFile(); |
| 1236 | return gw; |
| 1237 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1238 | } // namespace network |
| 1239 | } // namespace phosphor |