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