blob: 94bfecd8e8d4be69707279b27355524cdc38471f [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
2
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include "ethernet_interface.hpp"
4
Ratan Gupta497c0c92017-08-22 19:15:59 +05305#include "config_parser.hpp"
Ratan Gupta2b106532017-07-25 16:05:02 +05306#include "ipaddress.hpp"
Ratan Gupta4f1c18b2017-05-25 12:59:35 +05307#include "network_manager.hpp"
Ratan Guptafc2c7242017-05-29 08:46:06 +05308#include "routing_table.hpp"
Ratan Gupta2b106532017-07-25 16:05:02 +05309#include "vlan_interface.hpp"
Ratan Gupta91a99cc2017-04-14 16:32:09 +053010
Ratan Gupta82549cc2017-04-21 08:45:23 +053011#include <arpa/inet.h>
Ratan Gupta91a99cc2017-04-14 16:32:09 +053012#include <linux/ethtool.h>
Ratan Gupta91a99cc2017-04-14 16:32:09 +053013#include <linux/sockios.h>
Ratan Gupta2b106532017-07-25 16:05:02 +053014#include <net/if.h>
Ratan Gupta91a99cc2017-04-14 16:32:09 +053015#include <netinet/in.h>
16#include <sys/ioctl.h>
17#include <sys/socket.h>
18#include <unistd.h>
19
Ratan Gupta82549cc2017-04-21 08:45:23 +053020#include <algorithm>
21#include <experimental/filesystem>
Ratan Gupta2b106532017-07-25 16:05:02 +053022#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -070023#include <phosphor-logging/elog-errors.hpp>
24#include <phosphor-logging/log.hpp>
Ratan Gupta2b106532017-07-25 16:05:02 +053025#include <sstream>
26#include <string>
Patrick Venture189d44e2018-07-09 12:30:59 -070027#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta82549cc2017-04-21 08:45:23 +053028
Ratan Gupta91a99cc2017-04-14 16:32:09 +053029namespace phosphor
30{
31namespace network
32{
33
34using namespace phosphor::logging;
Ratan Gupta2b106532017-07-25 16:05:02 +053035using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050036using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta2b106532017-07-25 16:05:02 +053037
Ratan Gupta91a99cc2017-04-14 16:32:09 +053038EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus,
39 const std::string& objPath,
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 bool dhcpEnabled, Manager& parent,
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053041 bool emitSignal) :
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 Ifaces(bus, objPath.c_str(), true),
43 bus(bus), manager(parent), objPath(objPath)
Ratan Gupta91a99cc2017-04-14 16:32:09 +053044{
45 auto intfName = objPath.substr(objPath.rfind("/") + 1);
Ratan Gupta5978dd12017-07-25 13:47:13 +053046 std::replace(intfName.begin(), intfName.end(), '_', '.');
Ratan Gupta91a99cc2017-04-14 16:32:09 +053047 interfaceName(intfName);
Ratan Guptac35481d2017-08-18 06:12:26 +053048 EthernetInterfaceIntf::dHCPEnabled(dhcpEnabled);
Ratan Guptabd303b12017-08-18 17:10:07 +053049 MacAddressIntf::mACAddress(getMACAddress(intfName));
Ratan Gupta497c0c92017-08-22 19:15:59 +053050 EthernetInterfaceIntf::nTPServers(getNTPServersFromConf());
Ratan Gupta6dec390f2017-08-20 15:28:12 +053051 EthernetInterfaceIntf::nameservers(getNameServerFromConf());
52
Ratan Gupta29b0e432017-05-25 12:51:40 +053053 // Emit deferred signal.
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053054 if (emitSignal)
55 {
56 this->emit_object_added();
57 }
Ratan Gupta29b0e432017-05-25 12:51:40 +053058}
59
William A. Kennington IIIfbafa252018-11-30 16:53:52 -080060static IP::Protocol convertFamily(int family)
61{
62 switch (family)
63 {
64 case AF_INET:
65 return IP::Protocol::IPv4;
66 case AF_INET6:
67 return IP::Protocol::IPv6;
68 }
69
70 throw std::invalid_argument("Bad address family");
71}
72
Ratan Gupta87c13982017-06-15 09:27:27 +053073void EthernetInterface::createIPAddressObjects()
Ratan Gupta29b0e432017-05-25 12:51:40 +053074{
Ratan Gupta87c13982017-06-15 09:27:27 +053075 addrs.clear();
Ratan Gupta82549cc2017-04-21 08:45:23 +053076
Ratan Gupta87c13982017-06-15 09:27:27 +053077 auto addrs = getInterfaceAddrs()[interfaceName()];
Ratan Gupta5978dd12017-07-25 13:47:13 +053078
Ratan Guptafc2c7242017-05-29 08:46:06 +053079 route::Table routingTable;
Ratan Gupta5978dd12017-07-25 13:47:13 +053080
Ratan Gupta6a387c12017-08-03 13:26:19 +053081 for (auto& addr : addrs)
Ratan Gupta82549cc2017-04-21 08:45:23 +053082 {
William A. Kennington IIIfbafa252018-11-30 16:53:52 -080083 IP::Protocol addressType = convertFamily(addr.addrType);
84 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Guptafc2c7242017-05-29 08:46:06 +053085 if (dHCPEnabled())
86 {
87 origin = IP::AddressOrigin::DHCP;
88 }
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050089 else if (isLinkLocalIP(addr.ipaddress))
Ratan Guptafc2c7242017-05-29 08:46:06 +053090 {
91 origin = IP::AddressOrigin::LinkLocal;
92 }
William A. Kennington IIIfbafa252018-11-30 16:53:52 -080093 std::string gateway =
Gunnar Mills57d9c502018-09-14 14:42:34 -050094 routingTable.getGateway(addr.addrType, addr.ipaddress, addr.prefix);
Ratan Gupta82549cc2017-04-21 08:45:23 +053095
Gunnar Mills57d9c502018-09-14 14:42:34 -050096 std::string ipAddressObjectPath = generateObjectPath(
97 addressType, addr.ipaddress, addr.prefix, gateway);
Ratan Guptafc2c7242017-05-29 08:46:06 +053098
Gunnar Mills57d9c502018-09-14 14:42:34 -050099 this->addrs.emplace(addr.ipaddress,
100 std::make_shared<phosphor::network::IPAddress>(
101 bus, ipAddressObjectPath.c_str(), *this,
102 addressType, addr.ipaddress, origin,
103 addr.prefix, gateway));
Ratan Gupta82549cc2017-04-21 08:45:23 +0530104 }
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530105}
106
Gunnar Mills57d9c502018-09-14 14:42:34 -0500107void EthernetInterface::iP(IP::Protocol protType, std::string ipaddress,
108 uint8_t prefixLength, std::string gateway)
Ratan Gupta82549cc2017-04-21 08:45:23 +0530109{
Ratan Guptafc2c7242017-05-29 08:46:06 +0530110
111 if (dHCPEnabled())
112 {
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530113 log<level::INFO>("DHCP enabled on the interface"),
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500114 entry("INTERFACE=%s", interfaceName().c_str());
115 dHCPEnabled(false);
116 }
117
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500118 IP::AddressOrigin origin = IP::AddressOrigin::Static;
119
120 int addressFamily = (protType == IP::Protocol::IPv4) ? AF_INET : AF_INET6;
121
122 if (!isValidIP(addressFamily, ipaddress))
123 {
124 log<level::ERR>("Not a valid IP address"),
125 entry("ADDRESS=%s", ipaddress.c_str());
126 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"),
127 Argument::ARGUMENT_VALUE(ipaddress.c_str()));
128 }
129
130 if (!gateway.empty() && (!isValidIP(addressFamily, gateway)))
131 {
132 log<level::ERR>("Not a valid Gateway"),
133 entry("GATEWAY=%s", gateway.c_str());
134 elog<InvalidArgument>(Argument::ARGUMENT_NAME("gateway"),
135 Argument::ARGUMENT_VALUE(gateway.c_str()));
136 }
137
138 if (!isValidPrefix(addressFamily, prefixLength))
139 {
140 log<level::ERR>("PrefixLength is not correct "),
141 entry("PREFIXLENGTH=%d", gateway.c_str());
Gunnar Mills57d9c502018-09-14 14:42:34 -0500142 elog<InvalidArgument>(
143 Argument::ARGUMENT_NAME("prefixLength"),
144 Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str()));
Ratan Guptafc2c7242017-05-29 08:46:06 +0530145 }
146
Gunnar Mills57d9c502018-09-14 14:42:34 -0500147 std::string objectPath =
148 generateObjectPath(protType, ipaddress, prefixLength, gateway);
149 this->addrs.emplace(ipaddress,
150 std::make_shared<phosphor::network::IPAddress>(
151 bus, objectPath.c_str(), *this, protType, ipaddress,
152 origin, prefixLength, gateway));
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530153
Ratan Guptae05083a2017-09-16 07:12:11 +0530154 manager.writeToConfigurationFile();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530155}
156
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530157/*
158Note: We don't have support for ethtool now
159will enable this code once we bring the ethtool
160in the image.
161TODO: https://github.com/openbmc/openbmc/issues/1484
162*/
Ratan Gupta82549cc2017-04-21 08:45:23 +0530163
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530164InterfaceInfo EthernetInterface::getInterfaceInfo() const
165{
166 int sock{-1};
Ratan K Gupta1a054ae2018-09-15 00:49:51 -0400167 ifreq ifr{0};
168 ethtool_cmd edata{0};
Gunnar Mills57d9c502018-09-14 14:42:34 -0500169 LinkSpeed speed{0};
170 Autoneg autoneg{0};
171 DuplexMode duplex{0};
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530172 do
173 {
174 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
175 if (sock < 0)
176 {
177 log<level::ERR>("socket creation failed:",
178 entry("ERROR=%s", strerror(errno)));
179 break;
180 }
181
182 strncpy(ifr.ifr_name, interfaceName().c_str(), sizeof(ifr.ifr_name));
183 ifr.ifr_data = reinterpret_cast<char*>(&edata);
184
185 edata.cmd = ETHTOOL_GSET;
186
187 if (ioctl(sock, SIOCETHTOOL, &ifr) < 0)
188 {
189 log<level::ERR>("ioctl failed for SIOCETHTOOL:",
190 entry("ERROR=%s", strerror(errno)));
191 break;
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530192 }
193 speed = edata.speed;
194 duplex = edata.duplex;
195 autoneg = edata.autoneg;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500196 } while (0);
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530197
198 if (sock)
199 {
200 close(sock);
201 }
202 return std::make_tuple(speed, duplex, autoneg);
203}
204
205/** @brief get the mac address of the interface.
206 * @return macaddress on success
207 */
208
Gunnar Mills57d9c502018-09-14 14:42:34 -0500209std::string
210 EthernetInterface::getMACAddress(const std::string& interfaceName) const
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530211{
Ratan K Gupta1a054ae2018-09-15 00:49:51 -0400212 ifreq ifr{};
Gunnar Mills57d9c502018-09-14 14:42:34 -0500213 char macAddress[mac_address::size]{};
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530214
215 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
216 if (sock < 0)
217 {
218 log<level::ERR>("socket creation failed:",
219 entry("ERROR=%s", strerror(errno)));
220 return macAddress;
221 }
222
Patrick Venture836c91d2018-09-11 17:36:03 -0700223 std::strcpy(ifr.ifr_name, interfaceName.c_str());
Ratan Guptada7d3af2017-08-13 17:49:56 +0530224 if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0)
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530225 {
Ratan Guptada7d3af2017-08-13 17:49:56 +0530226 log<level::ERR>("ioctl failed for SIOCGIFHWADDR:",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500227 entry("ERROR=%s", strerror(errno)));
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530228 return macAddress;
229 }
230
Patrick Venture836c91d2018-09-11 17:36:03 -0700231 std::snprintf(macAddress, mac_address::size, 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]);
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530235
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530236 return macAddress;
237}
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530238
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530239std::string EthernetInterface::generateId(const std::string& ipaddress,
240 uint8_t prefixLength,
241 const std::string& gateway)
Ratan Gupta82549cc2017-04-21 08:45:23 +0530242{
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530243 std::stringstream hexId;
244 std::string hashString = ipaddress;
245 hashString += std::to_string(prefixLength);
246 hashString += gateway;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530247
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530248 // Only want 8 hex digits.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500249 hexId << std::hex << ((std::hash<std::string>{}(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 Guptae05083a2017-09-16 07:12:11 +0530262 manager.writeToConfigurationFile();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530263}
264
Ratan Guptae9c9b812017-09-22 17:15:37 +0530265void EthernetInterface::deleteVLANFromSystem(const std::string& interface)
Ratan Guptabc886292017-07-25 18:29:57 +0530266{
Ratan Guptabc886292017-07-25 18:29:57 +0530267 auto confDir = manager.getConfDir();
268 fs::path networkFile = confDir;
269 networkFile /= systemd::config::networkFilePrefix + interface +
270 systemd::config::networkFileSuffix;
271
272 fs::path deviceFile = confDir;
273 deviceFile /= interface + systemd::config::deviceFileSuffix;
274
275 // delete the vlan network file
276 if (fs::is_regular_file(networkFile))
277 {
278 fs::remove(networkFile);
279 }
280
281 // delete the vlan device file
282 if (fs::is_regular_file(deviceFile))
283 {
284 fs::remove(deviceFile);
285 }
Ratan Guptabc886292017-07-25 18:29:57 +0530286
287 // TODO systemd doesn't delete the virtual network interface
288 // even after deleting all the related configuartion.
289 // https://github.com/systemd/systemd/issues/6600
290 try
291 {
292 deleteInterface(interface);
293 }
294 catch (InternalFailure& e)
295 {
296 commit<InternalFailure>();
297 }
Ratan Guptae9c9b812017-09-22 17:15:37 +0530298}
299
300void EthernetInterface::deleteVLANObject(const std::string& interface)
301{
302 auto it = vlanInterfaces.find(interface);
303 if (it == vlanInterfaces.end())
304 {
305 log<level::ERR>("DeleteVLANObject:Unable to find the object",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500306 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptae9c9b812017-09-22 17:15:37 +0530307 return;
308 }
309
310 deleteVLANFromSystem(interface);
311 // delete the interface
312 vlanInterfaces.erase(it);
313
Ratan Guptae05083a2017-09-16 07:12:11 +0530314 manager.writeToConfigurationFile();
Ratan Guptabc886292017-07-25 18:29:57 +0530315}
316
Gunnar Mills57d9c502018-09-14 14:42:34 -0500317std::string EthernetInterface::generateObjectPath(
318 IP::Protocol addressType, const std::string& ipaddress,
319 uint8_t prefixLength, const std::string& gateway) const
Ratan Gupta82549cc2017-04-21 08:45:23 +0530320{
Ratan Gupta82549cc2017-04-21 08:45:23 +0530321 std::string type = convertForMessage(addressType);
Ratan Gupta29b0e432017-05-25 12:51:40 +0530322 type = type.substr(type.rfind('.') + 1);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530323 std::transform(type.begin(), type.end(), type.begin(), ::tolower);
324
325 std::experimental::filesystem::path objectPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530326 objectPath /= objPath;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530327 objectPath /= type;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530328 objectPath /= generateId(ipaddress, prefixLength, gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530329 return objectPath.string();
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530330}
331
Ratan Gupta87c13982017-06-15 09:27:27 +0530332bool EthernetInterface::dHCPEnabled(bool value)
333{
Ratan Gupta5978dd12017-07-25 13:47:13 +0530334 if (value == EthernetInterfaceIntf::dHCPEnabled())
335 {
336 return value;
337 }
338
Ratan Gupta87c13982017-06-15 09:27:27 +0530339 EthernetInterfaceIntf::dHCPEnabled(value);
Ratan Guptae05083a2017-09-16 07:12:11 +0530340 manager.writeToConfigurationFile();
Ratan Gupta87c13982017-06-15 09:27:27 +0530341 return value;
342}
343
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530344ServerList EthernetInterface::nameservers(ServerList value)
345{
346 try
347 {
348 EthernetInterfaceIntf::nameservers(value);
349
350 writeConfigurationFile();
351
352 // Currently we don't have systemd-resolved enabled
353 // in the openbmc. Once we update the network conf file,
354 // it should be read by systemd-resolved.service.
355
356 // The other reason to write the resolv conf is,
357 // we don't want to restart the networkd for nameserver change.
358 // as restarting of systemd-networkd takes more then 2 secs
359 writeDNSEntries(value, resolvConfFile);
360 }
361 catch (InternalFailure& e)
362 {
363 log<level::ERR>("Exception processing DNS entries");
364 }
365 return EthernetInterfaceIntf::nameservers();
366}
367
368ServerList EthernetInterface::getNameServerFromConf()
369{
370 fs::path confPath = manager.getConfDir();
371
372 std::string fileName = systemd::config::networkFilePrefix +
Gunnar Mills57d9c502018-09-14 14:42:34 -0500373 interfaceName() + systemd::config::networkFileSuffix;
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530374 confPath /= fileName;
375 ServerList servers;
Ratan Guptac27170a2017-11-22 15:44:42 +0530376 config::Parser parser(confPath.string());
377 auto rc = config::ReturnCode::SUCCESS;
378
379 std::tie(rc, servers) = parser.getValues("Network", "DNS");
380 if (rc != config::ReturnCode::SUCCESS)
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530381 {
Ratan Guptac27170a2017-11-22 15:44:42 +0530382 log<level::DEBUG>("Unable to get the value for network[DNS]",
383 entry("RC=%d", rc));
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530384 }
385 return servers;
386}
387
388void EthernetInterface::writeDNSEntries(const ServerList& dnsList,
389 const std::string& file)
390{
391 std::fstream outStream(file, std::fstream::out);
392 if (!outStream.is_open())
393 {
394 log<level::ERR>("Unable to open the file",
395 entry("FILE=%s", file.c_str()));
396 elog<InternalFailure>();
397 }
398
Ratan Guptafe116912017-11-10 16:00:59 +0530399 outStream << "### Generated manually via dbus settings ###\n";
Gunnar Mills57d9c502018-09-14 14:42:34 -0500400 for (const auto& server : dnsList)
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530401 {
402 outStream << "nameserver " << server << "\n";
403 }
404}
405
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530406void EthernetInterface::loadVLAN(VlanId id)
407{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500408 std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530409 std::string path = objPath;
410 path += "_" + std::to_string(id);
411
Gunnar Mills57d9c502018-09-14 14:42:34 -0500412 auto dhcpEnabled =
413 getDHCPValue(manager.getConfDir().string(), vlanInterfaceName);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530414
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530415 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500416 bus, path.c_str(), dhcpEnabled, id, *this, manager);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530417
Gunnar Mills57d9c502018-09-14 14:42:34 -0500418 // Fetch the ip address from the system
419 // and create the dbus object.
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530420 vlanIntf->createIPAddressObjects();
421
422 this->vlanInterfaces.emplace(std::move(vlanInterfaceName),
423 std::move(vlanIntf));
424}
425
Ratan Gupta5978dd12017-07-25 13:47:13 +0530426void EthernetInterface::createVLAN(VlanId id)
427{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500428 std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530429 std::string path = objPath;
430 path += "_" + std::to_string(id);
431
Ratan Gupta5978dd12017-07-25 13:47:13 +0530432 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500433 bus, path.c_str(), false, id, *this, manager);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530434
435 // write the device file for the vlan interface.
436 vlanIntf->writeDeviceFile();
437
Gunnar Mills57d9c502018-09-14 14:42:34 -0500438 this->vlanInterfaces.emplace(vlanInterfaceName, std::move(vlanIntf));
Ratan Gupta5978dd12017-07-25 13:47:13 +0530439 // write the new vlan device entry to the configuration(network) file.
Ratan Guptae05083a2017-09-16 07:12:11 +0530440 manager.writeToConfigurationFile();
Ratan Gupta5978dd12017-07-25 13:47:13 +0530441}
Ratan Gupta2b106532017-07-25 16:05:02 +0530442
Ratan Gupta497c0c92017-08-22 19:15:59 +0530443ServerList EthernetInterface::getNTPServersFromConf()
444{
445 fs::path confPath = manager.getConfDir();
446
Gunnar Mills57d9c502018-09-14 14:42:34 -0500447 std::string fileName = systemd::config::networkFilePrefix +
448 interfaceName() + systemd::config::networkFileSuffix;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530449 confPath /= fileName;
Ratan Guptac27170a2017-11-22 15:44:42 +0530450
Ratan Gupta497c0c92017-08-22 19:15:59 +0530451 ServerList servers;
Ratan Guptac27170a2017-11-22 15:44:42 +0530452 config::Parser parser(confPath.string());
453 auto rc = config::ReturnCode::SUCCESS;
454
455 std::tie(rc, servers) = parser.getValues("Network", "NTP");
456 if (rc != config::ReturnCode::SUCCESS)
Ratan Gupta497c0c92017-08-22 19:15:59 +0530457 {
Ratan Guptac27170a2017-11-22 15:44:42 +0530458 log<level::DEBUG>("Unable to get the value for Network[NTP]",
459 entry("rc=%d", rc));
Ratan Gupta497c0c92017-08-22 19:15:59 +0530460 }
Ratan Guptac27170a2017-11-22 15:44:42 +0530461
Ratan Gupta497c0c92017-08-22 19:15:59 +0530462 return servers;
463}
464
465ServerList EthernetInterface::nTPServers(ServerList servers)
466{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500467 auto ntpServers = EthernetInterfaceIntf::nTPServers(servers);
Ratan Gupta497c0c92017-08-22 19:15:59 +0530468
469 writeConfigurationFile();
470 // timesynchd reads the NTP server configuration from the
471 // network file.
Ratan Gupta895f9e52018-11-26 20:57:34 +0530472 manager.restartSystemdUnit(networkdService);
Ratan Gupta497c0c92017-08-22 19:15:59 +0530473 return ntpServers;
474}
Ratan Gupta2b106532017-07-25 16:05:02 +0530475// Need to merge the below function with the code which writes the
476// config file during factory reset.
477// TODO openbmc/openbmc#1751
478
479void EthernetInterface::writeConfigurationFile()
480{
481 // write all the static ip address in the systemd-network conf file
482
483 using namespace std::string_literals;
484 using AddressOrigin =
485 sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin;
486 namespace fs = std::experimental::filesystem;
Ratan Guptae05083a2017-09-16 07:12:11 +0530487
488 // if there is vlan interafce then write the configuration file
489 // for vlan also.
490
Gunnar Mills57d9c502018-09-14 14:42:34 -0500491 for (const auto& intf : vlanInterfaces)
Ratan Guptae05083a2017-09-16 07:12:11 +0530492 {
493 intf.second->writeConfigurationFile();
494 }
495
Ratan Gupta2b106532017-07-25 16:05:02 +0530496 fs::path confPath = manager.getConfDir();
497
Gunnar Mills57d9c502018-09-14 14:42:34 -0500498 std::string fileName = systemd::config::networkFilePrefix +
499 interfaceName() + systemd::config::networkFileSuffix;
Ratan Gupta2b106532017-07-25 16:05:02 +0530500 confPath /= fileName;
501 std::fstream stream;
502
503 stream.open(confPath.c_str(), std::fstream::out);
504 if (!stream.is_open())
505 {
506 log<level::ERR>("Unable to open the file",
507 entry("FILE=%s", confPath.c_str()));
508 elog<InternalFailure>();
509 }
510
511 // Write the device
Ratan K Gupta1a054ae2018-09-15 00:49:51 -0400512 stream << "[Match]\n";
Ratan Gupta2b106532017-07-25 16:05:02 +0530513 stream << "Name=" << interfaceName() << "\n";
514
515 auto addrs = getAddresses();
516
517 // write the network section
Ratan K Gupta1a054ae2018-09-15 00:49:51 -0400518 stream << "[Network]\n";
Oskar Senftad21fc22018-07-26 16:32:23 -0400519#ifdef LINK_LOCAL_AUTOCONFIGURATION
Nagaraju Goruganti24afe362017-09-21 07:40:26 -0500520 stream << "LinkLocalAddressing=yes\n";
Oskar Senftad21fc22018-07-26 16:32:23 -0400521#else
522 stream << "LinkLocalAddressing=no\n";
523#endif
Ratan Guptae9629412017-12-21 08:20:25 +0530524 stream << "IPv6AcceptRA=false\n";
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530525
526 // Add the VLAN entry
Gunnar Mills57d9c502018-09-14 14:42:34 -0500527 for (const auto& intf : vlanInterfaces)
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530528 {
529 stream << "VLAN=" << intf.second->EthernetInterface::interfaceName()
Gunnar Mills57d9c502018-09-14 14:42:34 -0500530 << "\n";
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530531 }
Nagaraju Gorugantie8b83ec2018-03-26 05:21:45 -0500532 // Add the DHCP entry
533 auto value = dHCPEnabled() ? "true"s : "false"s;
534 stream << "DHCP="s + value + "\n";
535
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600536 // When the interface configured as dhcp, we don't need below given entries
537 // in config file.
538 if (dHCPEnabled() == false)
Ratan Gupta2b106532017-07-25 16:05:02 +0530539 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500540 // Add the NTP server
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600541 for (const auto& ntp : EthernetInterfaceIntf::nTPServers())
Ratan Gupta2b106532017-07-25 16:05:02 +0530542 {
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600543 stream << "NTP=" << ntp << "\n";
544 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530545
Gunnar Mills57d9c502018-09-14 14:42:34 -0500546 // Add the DNS entry
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600547 for (const auto& dns : EthernetInterfaceIntf::nameservers())
Ratan Gupta2b106532017-07-25 16:05:02 +0530548 {
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600549 stream << "DNS=" << dns << "\n";
550 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530551
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600552 // Static
553 for (const auto& addr : addrs)
554 {
Oskar Senftad21fc22018-07-26 16:32:23 -0400555 if (addr.second->origin() == AddressOrigin::Static
556#ifndef LINK_LOCAL_AUTOCONFIGURATION
557 || addr.second->origin() == AddressOrigin::LinkLocal
558#endif
559 )
Ratan Gupta2b106532017-07-25 16:05:02 +0530560 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500561 std::string address =
562 addr.second->address() + "/" +
563 std::to_string(addr.second->prefixLength());
Ratan Gupta2b106532017-07-25 16:05:02 +0530564
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600565 stream << "Address=" << address << "\n";
566 }
567 }
568
569 if (manager.getSystemConf())
570 {
571 stream << "Gateway=" << manager.getSystemConf()->defaultGateway()
572 << "\n";
573 }
574
575 // write the route section
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600576 for (const auto& addr : addrs)
577 {
578 if (addr.second->origin() == AddressOrigin::Static)
579 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500580 int addressFamily = addr.second->type() == IP::Protocol::IPv4
581 ? AF_INET
582 : AF_INET6;
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600583
Gunnar Mills57d9c502018-09-14 14:42:34 -0500584 std::string destination =
585 getNetworkID(addressFamily, addr.second->address(),
586 addr.second->prefixLength());
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600587
588 if (addr.second->gateway() != "0.0.0.0" &&
Gunnar Mills57d9c502018-09-14 14:42:34 -0500589 addr.second->gateway() != "" && destination != "0.0.0.0" &&
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600590 destination != "")
591 {
Ratan K Gupta1a054ae2018-09-15 00:49:51 -0400592 stream << "[Route]\n";
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600593 stream << "Gateway=" << addr.second->gateway() << "\n";
594 stream << "Destination=" << destination << "\n";
595 }
596 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530597 }
598 }
599
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600600 // Write the dhcp section irrespective of whether DHCP is enabled or not
601 writeDHCPSection(stream);
602
Ratan Gupta2b106532017-07-25 16:05:02 +0530603 stream.close();
Ratan Gupta2b106532017-07-25 16:05:02 +0530604}
605
606void EthernetInterface::writeDHCPSection(std::fstream& stream)
607{
608 using namespace std::string_literals;
Ratan Gupta2b106532017-07-25 16:05:02 +0530609 // write the dhcp section
610 stream << "[DHCP]\n";
611
612 // Hardcoding the client identifier to mac, to address below issue
613 // https://github.com/openbmc/openbmc/issues/1280
614 stream << "ClientIdentifier=mac\n";
615 if (manager.getDHCPConf())
616 {
617 auto value = manager.getDHCPConf()->dNSEnabled() ? "true"s : "false"s;
618 stream << "UseDNS="s + value + "\n";
619
620 value = manager.getDHCPConf()->nTPEnabled() ? "true"s : "false"s;
621 stream << "UseNTP="s + value + "\n";
622
623 value = manager.getDHCPConf()->hostNameEnabled() ? "true"s : "false"s;
624 stream << "UseHostname="s + value + "\n";
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -0600625
626 value =
627 manager.getDHCPConf()->sendHostNameEnabled() ? "true"s : "false"s;
628 stream << "SendHostname="s + value + "\n";
Ratan Gupta2b106532017-07-25 16:05:02 +0530629 }
630}
631
Ratan Guptabd303b12017-08-18 17:10:07 +0530632std::string EthernetInterface::mACAddress(std::string value)
633{
634 if (!mac_address::validate(value))
635 {
Gunnar Mills90480c42018-06-19 16:02:17 -0500636 log<level::ERR>("MACAddress is not valid.",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500637 entry("MAC=%s", value.c_str()));
Gunnar Mills90480c42018-06-19 16:02:17 -0500638 elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
639 Argument::ARGUMENT_VALUE(value.c_str()));
Ratan Guptabd303b12017-08-18 17:10:07 +0530640 }
641
642 // check whether MAC is broadcast mac.
643 auto intMac = mac_address::internal::convertToInt(value);
644
645 if (!(intMac ^ mac_address::broadcastMac))
646 {
Gunnar Mills90480c42018-06-19 16:02:17 -0500647 log<level::ERR>("MACAddress is a broadcast mac.",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500648 entry("MAC=%s", value.c_str()));
Gunnar Mills90480c42018-06-19 16:02:17 -0500649 elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
650 Argument::ARGUMENT_VALUE(value.c_str()));
Ratan Guptabd303b12017-08-18 17:10:07 +0530651 }
652
Patrick Ventured475cd62018-02-26 17:07:41 -0800653 // Check if the MAC changed.
654 auto pmac = MacAddressIntf::mACAddress();
655 if (strcasecmp(pmac.c_str(), value.c_str()) == 0)
656 {
657 return MacAddressIntf::mACAddress();
658 }
659
Ratan Guptabd303b12017-08-18 17:10:07 +0530660 // Allow the mac to be set if one of the condition is true.
661 // 1) Incoming Mac is of local admin type.
662 // or
663 // 2) Incoming mac is same as eeprom Mac.
664
665 if (!(intMac & mac_address::localAdminMask))
666 {
667 try
668 {
669 auto inventoryMac = mac_address::getfromInventory(bus);
Gunnar Mills57d9c502018-09-14 14:42:34 -0500670 auto intInventoryMac =
671 mac_address::internal::convertToInt(inventoryMac);
Ratan Guptabd303b12017-08-18 17:10:07 +0530672
673 if (intInventoryMac != intMac)
674 {
Gunnar Mills90480c42018-06-19 16:02:17 -0500675 log<level::ERR>("Given MAC address is neither a local Admin "
Gunnar Mills57d9c502018-09-14 14:42:34 -0500676 "type nor is same as in inventory");
Gunnar Mills90480c42018-06-19 16:02:17 -0500677 elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
678 Argument::ARGUMENT_VALUE(value.c_str()));
Ratan Guptabd303b12017-08-18 17:10:07 +0530679 }
680 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500681 catch (InternalFailure& e)
Ratan Guptabd303b12017-08-18 17:10:07 +0530682 {
Patrick Venturee0ad43a2017-11-29 18:19:54 -0800683 log<level::ERR>("Exception occurred during getting of MAC "
684 "address from Inventory");
Gunnar Millsce262822018-06-19 16:21:34 -0500685 elog<InternalFailure>();
Ratan Guptabd303b12017-08-18 17:10:07 +0530686 }
687 }
688 auto interface = interfaceName();
689 execute("/sbin/fw_setenv", "fw_setenv", "ethaddr", value.c_str());
Gunnar Mills57d9c502018-09-14 14:42:34 -0500690 // TODO: would replace below three calls
Ratan Guptabd303b12017-08-18 17:10:07 +0530691 // with restarting of systemd-netwokd
692 // through https://github.com/systemd/systemd/issues/6696
693 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "down");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500694 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(),
695 "address", value.c_str());
Ratan Guptabd303b12017-08-18 17:10:07 +0530696
697 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "up");
698
699 auto mac = MacAddressIntf::mACAddress(std::move(value));
Gunnar Mills57d9c502018-09-14 14:42:34 -0500700 // update all the vlan interfaces
701 for (const auto& intf : vlanInterfaces)
Ratan Guptabd303b12017-08-18 17:10:07 +0530702 {
703 intf.second->updateMacAddress();
704 }
Ratan Gupta677ae122017-09-18 16:28:50 +0530705
706 // restart the systemd networkd so that dhcp client gets the
707 // ip for the changed mac address.
708 if (dHCPEnabled())
709 {
Ratan Gupta895f9e52018-11-26 20:57:34 +0530710 manager.restartSystemdUnit(networkdService);
Ratan Gupta677ae122017-09-18 16:28:50 +0530711 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530712 return mac;
Ratan Guptabd303b12017-08-18 17:10:07 +0530713}
714
Ratan Guptae9c9b812017-09-22 17:15:37 +0530715void EthernetInterface::deleteAll()
716{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500717 if (EthernetInterfaceIntf::dHCPEnabled())
Ratan Guptae9c9b812017-09-22 17:15:37 +0530718 {
719 log<level::INFO>("DHCP enabled on the interface"),
Gunnar Mills57d9c502018-09-14 14:42:34 -0500720 entry("INTERFACE=%s", interfaceName().c_str());
Ratan Guptae9c9b812017-09-22 17:15:37 +0530721 }
722
723 // clear all the ip on the interface
724 addrs.clear();
725 manager.writeToConfigurationFile();
726}
727
Gunnar Mills57d9c502018-09-14 14:42:34 -0500728} // namespace network
729} // namespace phosphor