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