blob: 14ccc6cd7d8d149080f0dd295bb20b242a4d488c [file] [log] [blame]
Ratan Gupta82549cc2017-04-21 08:45:23 +05301#include "config.h"
2#include "ipaddress.hpp"
Ratan Gupta91a99cc2017-04-14 16:32:09 +05303#include "ethernet_interface.hpp"
Ratan Gupta4f1c18b2017-05-25 12:59:35 +05304#include "network_manager.hpp"
Ratan Guptafc2c7242017-05-29 08:46:06 +05305#include "routing_table.hpp"
Ratan Gupta91a99cc2017-04-14 16:32:09 +05306
7#include <phosphor-logging/log.hpp>
8
Ratan Gupta82549cc2017-04-21 08:45:23 +05309#include <arpa/inet.h>
Ratan Gupta91a99cc2017-04-14 16:32:09 +053010#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 Gupta82549cc2017-04-21 08:45:23 +053018#include <string>
19#include <algorithm>
Ratan Gupta65e5abe2017-05-23 13:20:44 +053020#include <sstream>
Ratan Gupta82549cc2017-04-21 08:45:23 +053021#include <experimental/filesystem>
22
Ratan Gupta91a99cc2017-04-14 16:32:09 +053023namespace phosphor
24{
25namespace network
26{
27
28using namespace phosphor::logging;
29constexpr auto MAC_ADDRESS_FORMAT = "%02X:%02X:%02X:%02X:%02X:%02X";
30constexpr size_t SIZE_MAC = 18;
31constexpr size_t SIZE_BUFF = 512;
32
33EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus,
34 const std::string& objPath,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053035 bool dhcpEnabled,
36 Manager& parent) :
Ratan Gupta82549cc2017-04-21 08:45:23 +053037 Ifaces(bus, objPath.c_str(), true),
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053038 bus(bus),
Ratan Gupta47722dc2017-05-26 18:32:23 +053039 manager(parent),
40 objPath(objPath)
Ratan Gupta91a99cc2017-04-14 16:32:09 +053041{
42 auto intfName = objPath.substr(objPath.rfind("/") + 1);
43 interfaceName(intfName);
44 dHCPEnabled(dhcpEnabled);
45 mACAddress(getMACAddress());
Ratan Gupta87c13982017-06-15 09:27:27 +053046 createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +053047 // Emit deferred signal.
48 this->emit_object_added();
49}
50
Ratan Gupta87c13982017-06-15 09:27:27 +053051void EthernetInterface::createIPAddressObjects()
Ratan Gupta29b0e432017-05-25 12:51:40 +053052{
Ratan Gupta82549cc2017-04-21 08:45:23 +053053 std::string gateway;
Ratan Gupta87c13982017-06-15 09:27:27 +053054 addrs.clear();
Ratan Gupta82549cc2017-04-21 08:45:23 +053055
Ratan Gupta87c13982017-06-15 09:27:27 +053056 auto addrs = getInterfaceAddrs()[interfaceName()];
Ratan Gupta82549cc2017-04-21 08:45:23 +053057 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta29b0e432017-05-25 12:51:40 +053058 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Guptafc2c7242017-05-29 08:46:06 +053059 route::Table routingTable;
Ratan Gupta6a387c12017-08-03 13:26:19 +053060 for (auto& addr : addrs)
Ratan Gupta82549cc2017-04-21 08:45:23 +053061 {
62 if (addr.addrType == AF_INET6)
63 {
64 addressType = IP::Protocol::IPv6;
65 }
Ratan Guptafc2c7242017-05-29 08:46:06 +053066 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 Gupta82549cc2017-04-21 08:45:23 +053075
Ratan Gupta65e5abe2017-05-23 13:20:44 +053076 std::string ipAddressObjectPath = generateObjectPath(addressType,
77 addr.ipaddress,
78 addr.prefix,
79 gateway);
Ratan Guptafc2c7242017-05-29 08:46:06 +053080
Ratan Gupta82549cc2017-04-21 08:45:23 +053081 this->addrs.emplace(
Ratan Guptae578d562017-08-02 07:04:16 +053082 std::move(addr.ipaddress),
83 std::make_shared<phosphor::network::IPAddress>(
Ratan Gupta719f83a2017-06-02 11:54:53 +053084 bus,
85 ipAddressObjectPath.c_str(),
86 *this,
87 addressType,
Ratan Gupta82549cc2017-04-21 08:45:23 +053088 addr.ipaddress,
Ratan Gupta29b0e432017-05-25 12:51:40 +053089 origin,
Ratan Gupta719f83a2017-06-02 11:54:53 +053090 addr.prefix,
Ratan Guptae578d562017-08-02 07:04:16 +053091 gateway));
Ratan Gupta82549cc2017-04-21 08:45:23 +053092 }
Ratan Guptafc2c7242017-05-29 08:46:06 +053093
Ratan Gupta91a99cc2017-04-14 16:32:09 +053094}
95
Ratan Gupta82549cc2017-04-21 08:45:23 +053096void EthernetInterface::iP(IP::Protocol protType,
97 std::string ipaddress,
98 uint8_t prefixLength,
99 std::string gateway)
100{
Ratan Guptafc2c7242017-05-29 08:46:06 +0530101
102 if (dHCPEnabled())
103 {
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530104 log<level::INFO>("DHCP enabled on the interface"),
105 entry("INTERFACE=%s",interfaceName());
Ratan Guptafc2c7242017-05-29 08:46:06 +0530106 return;
107 }
108
Ratan Gupta29b0e432017-05-25 12:51:40 +0530109 IP::AddressOrigin origin = IP::AddressOrigin::Static;
110
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530111 std::string objectPath = generateObjectPath(protType,
112 ipaddress,
113 prefixLength,
114 gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530115
116 this->addrs.emplace(
Ratan Guptae578d562017-08-02 07:04:16 +0530117 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 Gupta4f1c18b2017-05-25 12:59:35 +0530127
128 manager.writeToConfigurationFile();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530129}
130
131
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530132/*
133Note: We don't have support for ethtool now
134will enable this code once we bring the ethtool
135in the image.
136TODO: https://github.com/openbmc/openbmc/issues/1484
137*/
Ratan Gupta82549cc2017-04-21 08:45:23 +0530138
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530139InterfaceInfo 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
186std::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 Gupta2eff84f2017-04-20 19:19:15 +0530237
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530238std::string EthernetInterface::generateId(const std::string& ipaddress,
239 uint8_t prefixLength,
240 const std::string& gateway)
Ratan Gupta82549cc2017-04-21 08:45:23 +0530241{
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530242 std::stringstream hexId;
243 std::string hashString = ipaddress;
244 hashString += std::to_string(prefixLength);
245 hashString += gateway;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530246
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530247 // Only want 8 hex digits.
248 hexId << std::hex << ((std::hash<std::string> {}(
Ratan Guptafc2c7242017-05-29 08:46:06 +0530249 hashString)) & 0xFFFFFFFF);
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530250 return hexId.str();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530251}
252
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530253void EthernetInterface::deleteObject(const std::string& ipaddress)
254{
Ratan Gupta29b0e432017-05-25 12:51:40 +0530255 auto it = addrs.find(ipaddress);
Ratan Guptafc2c7242017-05-29 08:46:06 +0530256 if (it == addrs.end())
Ratan Gupta29b0e432017-05-25 12:51:40 +0530257 {
Ratan Guptafc2c7242017-05-29 08:46:06 +0530258 log<level::ERR>("DeleteObject:Unable to find the object.");
259 return;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530260 }
261 this->addrs.erase(it);
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530262 manager.writeToConfigurationFile();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530263}
264
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530265std::string EthernetInterface::generateObjectPath(IP::Protocol addressType,
266 const std::string& ipaddress,
267 uint8_t prefixLength,
268 const std::string& gateway) const
Ratan Gupta82549cc2017-04-21 08:45:23 +0530269{
Ratan Gupta82549cc2017-04-21 08:45:23 +0530270 std::string type = convertForMessage(addressType);
Ratan Gupta29b0e432017-05-25 12:51:40 +0530271 type = type.substr(type.rfind('.') + 1);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530272 std::transform(type.begin(), type.end(), type.begin(), ::tolower);
273
274 std::experimental::filesystem::path objectPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530275 objectPath /= objPath;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530276 objectPath /= type;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530277 objectPath /= generateId(ipaddress, prefixLength, gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530278 return objectPath.string();
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530279}
280
Ratan Gupta87c13982017-06-15 09:27:27 +0530281bool 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 Gupta91a99cc2017-04-14 16:32:09 +0530292}//namespace network
293}//namespace phosphor