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