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