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