Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "ipaddress.hpp" |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 3 | #include "ethernet_interface.hpp" |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 4 | #include "network_manager.hpp" |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 5 | #include "routing_table.hpp" |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 6 | |
| 7 | #include <phosphor-logging/log.hpp> |
| 8 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 9 | #include <arpa/inet.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 10 | #include <linux/ethtool.h> |
| 11 | #include <net/if.h> |
| 12 | #include <linux/sockios.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <sys/ioctl.h> |
| 15 | #include <sys/socket.h> |
| 16 | #include <unistd.h> |
| 17 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <algorithm> |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 20 | #include <sstream> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 21 | #include <experimental/filesystem> |
| 22 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace network |
| 26 | { |
| 27 | |
| 28 | using namespace phosphor::logging; |
| 29 | constexpr auto MAC_ADDRESS_FORMAT = "%02X:%02X:%02X:%02X:%02X:%02X"; |
| 30 | constexpr size_t SIZE_MAC = 18; |
| 31 | constexpr size_t SIZE_BUFF = 512; |
| 32 | |
| 33 | EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus, |
| 34 | const std::string& objPath, |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 35 | bool dhcpEnabled, |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 36 | Manager& parent, |
| 37 | bool emitSignal) : |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 38 | Ifaces(bus, objPath.c_str(), true), |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 39 | bus(bus), |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 40 | manager(parent), |
| 41 | objPath(objPath) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 42 | { |
| 43 | auto intfName = objPath.substr(objPath.rfind("/") + 1); |
| 44 | interfaceName(intfName); |
| 45 | dHCPEnabled(dhcpEnabled); |
| 46 | mACAddress(getMACAddress()); |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 47 | createIPAddressObjects(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 48 | // Emit deferred signal. |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 49 | if (emitSignal) |
| 50 | { |
| 51 | this->emit_object_added(); |
| 52 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 53 | } |
| 54 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 55 | void EthernetInterface::createIPAddressObjects() |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 56 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 57 | std::string gateway; |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 58 | addrs.clear(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 59 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 60 | auto addrs = getInterfaceAddrs()[interfaceName()]; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 61 | IP::Protocol addressType = IP::Protocol::IPv4; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 62 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 63 | route::Table routingTable; |
Ratan Gupta | 6a387c1 | 2017-08-03 13:26:19 +0530 | [diff] [blame] | 64 | for (auto& addr : addrs) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 65 | { |
| 66 | if (addr.addrType == AF_INET6) |
| 67 | { |
| 68 | addressType = IP::Protocol::IPv6; |
| 69 | } |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 70 | if (dHCPEnabled()) |
| 71 | { |
| 72 | origin = IP::AddressOrigin::DHCP; |
| 73 | } |
| 74 | else if (isLinkLocal(addr.ipaddress)) |
| 75 | { |
| 76 | origin = IP::AddressOrigin::LinkLocal; |
| 77 | } |
| 78 | gateway = routingTable.getGateway(addr.addrType, addr.ipaddress, addr.prefix); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 79 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 80 | std::string ipAddressObjectPath = generateObjectPath(addressType, |
| 81 | addr.ipaddress, |
| 82 | addr.prefix, |
| 83 | gateway); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 84 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 85 | this->addrs.emplace( |
Ratan Gupta | e578d56 | 2017-08-02 07:04:16 +0530 | [diff] [blame] | 86 | std::move(addr.ipaddress), |
| 87 | std::make_shared<phosphor::network::IPAddress>( |
Ratan Gupta | 719f83a | 2017-06-02 11:54:53 +0530 | [diff] [blame] | 88 | bus, |
| 89 | ipAddressObjectPath.c_str(), |
| 90 | *this, |
| 91 | addressType, |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 92 | addr.ipaddress, |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 93 | origin, |
Ratan Gupta | 719f83a | 2017-06-02 11:54:53 +0530 | [diff] [blame] | 94 | addr.prefix, |
Ratan Gupta | e578d56 | 2017-08-02 07:04:16 +0530 | [diff] [blame] | 95 | gateway)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 96 | } |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 97 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 98 | } |
| 99 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 100 | void EthernetInterface::iP(IP::Protocol protType, |
| 101 | std::string ipaddress, |
| 102 | uint8_t prefixLength, |
| 103 | std::string gateway) |
| 104 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 105 | |
| 106 | if (dHCPEnabled()) |
| 107 | { |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 108 | log<level::INFO>("DHCP enabled on the interface"), |
| 109 | entry("INTERFACE=%s",interfaceName()); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 113 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
| 114 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 115 | std::string objectPath = generateObjectPath(protType, |
| 116 | ipaddress, |
| 117 | prefixLength, |
| 118 | gateway); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 119 | |
| 120 | this->addrs.emplace( |
Ratan Gupta | e578d56 | 2017-08-02 07:04:16 +0530 | [diff] [blame] | 121 | std::move(ipaddress), |
| 122 | std::make_shared<phosphor::network::IPAddress>( |
| 123 | bus, |
| 124 | objectPath.c_str(), |
| 125 | *this, |
| 126 | protType, |
| 127 | ipaddress, |
| 128 | origin, |
| 129 | prefixLength, |
| 130 | gateway)); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 131 | |
| 132 | manager.writeToConfigurationFile(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 136 | /* |
| 137 | Note: We don't have support for ethtool now |
| 138 | will enable this code once we bring the ethtool |
| 139 | in the image. |
| 140 | TODO: https://github.com/openbmc/openbmc/issues/1484 |
| 141 | */ |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 142 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 143 | InterfaceInfo EthernetInterface::getInterfaceInfo() const |
| 144 | { |
| 145 | int sock{-1}; |
| 146 | struct ifreq ifr{0}; |
| 147 | struct ethtool_cmd edata{0}; |
| 148 | LinkSpeed speed {0}; |
| 149 | Autoneg autoneg {0}; |
| 150 | DuplexMode duplex {0}; |
| 151 | do |
| 152 | { |
| 153 | sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 154 | if (sock < 0) |
| 155 | { |
| 156 | log<level::ERR>("socket creation failed:", |
| 157 | entry("ERROR=%s", strerror(errno))); |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | strncpy(ifr.ifr_name, interfaceName().c_str(), sizeof(ifr.ifr_name)); |
| 162 | ifr.ifr_data = reinterpret_cast<char*>(&edata); |
| 163 | |
| 164 | edata.cmd = ETHTOOL_GSET; |
| 165 | |
| 166 | if (ioctl(sock, SIOCETHTOOL, &ifr) < 0) |
| 167 | { |
| 168 | log<level::ERR>("ioctl failed for SIOCETHTOOL:", |
| 169 | entry("ERROR=%s", strerror(errno))); |
| 170 | break; |
| 171 | |
| 172 | } |
| 173 | speed = edata.speed; |
| 174 | duplex = edata.duplex; |
| 175 | autoneg = edata.autoneg; |
| 176 | } |
| 177 | while (0); |
| 178 | |
| 179 | if (sock) |
| 180 | { |
| 181 | close(sock); |
| 182 | } |
| 183 | return std::make_tuple(speed, duplex, autoneg); |
| 184 | } |
| 185 | |
| 186 | /** @brief get the mac address of the interface. |
| 187 | * @return macaddress on success |
| 188 | */ |
| 189 | |
| 190 | std::string EthernetInterface::getMACAddress() const |
| 191 | { |
| 192 | struct ifreq ifr; |
| 193 | struct ifconf ifc; |
| 194 | char buf[SIZE_BUFF]; |
| 195 | char macAddress[SIZE_MAC] = ""; |
| 196 | |
| 197 | int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 198 | if (sock < 0) |
| 199 | { |
| 200 | log<level::ERR>("socket creation failed:", |
| 201 | entry("ERROR=%s", strerror(errno))); |
| 202 | return macAddress; |
| 203 | } |
| 204 | |
| 205 | ifc.ifc_len = sizeof(buf); |
| 206 | ifc.ifc_buf = buf; |
| 207 | if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) |
| 208 | { |
| 209 | log<level::ERR>("ioctl failed for SIOCGIFCONF:", |
| 210 | entry("ERROR=%s", strerror(errno))); |
| 211 | return macAddress; |
| 212 | } |
| 213 | |
| 214 | struct ifreq* it = ifc.ifc_req; |
| 215 | const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq)); |
| 216 | |
| 217 | for (; it != end; ++it) |
| 218 | { |
| 219 | if (interfaceName() == it->ifr_name) |
| 220 | { |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | if (interfaceName() == it->ifr_name) |
| 225 | { |
| 226 | strcpy(ifr.ifr_name, it->ifr_name); |
| 227 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) |
| 228 | { |
| 229 | log<level::ERR>("ioctl failed for SIOCGIFHWADDR:", |
| 230 | entry("ERROR=%s", strerror(errno))); |
| 231 | return macAddress; |
| 232 | } |
| 233 | |
| 234 | snprintf(macAddress, SIZE_MAC, MAC_ADDRESS_FORMAT, |
| 235 | ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], |
| 236 | ifr.ifr_hwaddr.sa_data[2], ifr.ifr_hwaddr.sa_data[3], |
| 237 | ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); |
| 238 | } |
| 239 | return macAddress; |
| 240 | } |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 241 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 242 | std::string EthernetInterface::generateId(const std::string& ipaddress, |
| 243 | uint8_t prefixLength, |
| 244 | const std::string& gateway) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 245 | { |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 246 | std::stringstream hexId; |
| 247 | std::string hashString = ipaddress; |
| 248 | hashString += std::to_string(prefixLength); |
| 249 | hashString += gateway; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 250 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 251 | // Only want 8 hex digits. |
| 252 | hexId << std::hex << ((std::hash<std::string> {}( |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 253 | hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 254 | return hexId.str(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 255 | } |
| 256 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 257 | void EthernetInterface::deleteObject(const std::string& ipaddress) |
| 258 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 259 | auto it = addrs.find(ipaddress); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 260 | if (it == addrs.end()) |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 261 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 262 | log<level::ERR>("DeleteObject:Unable to find the object."); |
| 263 | return; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 264 | } |
| 265 | this->addrs.erase(it); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 266 | manager.writeToConfigurationFile(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 267 | } |
| 268 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 269 | std::string EthernetInterface::generateObjectPath(IP::Protocol addressType, |
| 270 | const std::string& ipaddress, |
| 271 | uint8_t prefixLength, |
| 272 | const std::string& gateway) const |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 273 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 274 | std::string type = convertForMessage(addressType); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 275 | type = type.substr(type.rfind('.') + 1); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 276 | std::transform(type.begin(), type.end(), type.begin(), ::tolower); |
| 277 | |
| 278 | std::experimental::filesystem::path objectPath; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 279 | objectPath /= objPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 280 | objectPath /= type; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 281 | objectPath /= generateId(ipaddress, prefixLength, gateway); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 282 | return objectPath.string(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 283 | } |
| 284 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 285 | bool EthernetInterface::dHCPEnabled(bool value) |
| 286 | { |
| 287 | EthernetInterfaceIntf::dHCPEnabled(value); |
| 288 | if (value == true) |
| 289 | { |
| 290 | manager.writeToConfigurationFile(); |
| 291 | createIPAddressObjects(); |
| 292 | } |
| 293 | return value; |
| 294 | } |
| 295 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 296 | }//namespace network |
| 297 | }//namespace phosphor |