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