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