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