blob: 19dfec47bc0f41547f3952937866b9845f1dbfb3 [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
Ratan Gupta87c13982017-06-15 09:27:27 +053060void EthernetInterface::createIPAddressObjects()
Ratan Gupta29b0e432017-05-25 12:51:40 +053061{
Ratan Gupta82549cc2017-04-21 08:45:23 +053062 std::string gateway;
Ratan Gupta87c13982017-06-15 09:27:27 +053063 addrs.clear();
Ratan Gupta82549cc2017-04-21 08:45:23 +053064
Ratan Gupta87c13982017-06-15 09:27:27 +053065 auto addrs = getInterfaceAddrs()[interfaceName()];
Ratan Gupta5978dd12017-07-25 13:47:13 +053066
Ratan Gupta82549cc2017-04-21 08:45:23 +053067 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta29b0e432017-05-25 12:51:40 +053068 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Guptafc2c7242017-05-29 08:46:06 +053069 route::Table routingTable;
Ratan Gupta5978dd12017-07-25 13:47:13 +053070
Ratan Gupta6a387c12017-08-03 13:26:19 +053071 for (auto& addr : addrs)
Ratan Gupta82549cc2017-04-21 08:45:23 +053072 {
73 if (addr.addrType == AF_INET6)
74 {
75 addressType = IP::Protocol::IPv6;
76 }
Ratan Guptafc2c7242017-05-29 08:46:06 +053077 if (dHCPEnabled())
78 {
79 origin = IP::AddressOrigin::DHCP;
80 }
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050081 else if (isLinkLocalIP(addr.ipaddress))
Ratan Guptafc2c7242017-05-29 08:46:06 +053082 {
83 origin = IP::AddressOrigin::LinkLocal;
84 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 gateway =
86 routingTable.getGateway(addr.addrType, addr.ipaddress, addr.prefix);
Ratan Gupta82549cc2017-04-21 08:45:23 +053087
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 std::string ipAddressObjectPath = generateObjectPath(
89 addressType, addr.ipaddress, addr.prefix, gateway);
Ratan Guptafc2c7242017-05-29 08:46:06 +053090
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 this->addrs.emplace(addr.ipaddress,
92 std::make_shared<phosphor::network::IPAddress>(
93 bus, ipAddressObjectPath.c_str(), *this,
94 addressType, addr.ipaddress, origin,
95 addr.prefix, gateway));
Ratan Gupta603598d2017-11-14 20:58:38 +053096
97 origin = IP::AddressOrigin::Static;
Ratan Gupta82549cc2017-04-21 08:45:23 +053098 }
Ratan Gupta91a99cc2017-04-14 16:32:09 +053099}
100
Gunnar Mills57d9c502018-09-14 14:42:34 -0500101void EthernetInterface::iP(IP::Protocol protType, std::string ipaddress,
102 uint8_t prefixLength, std::string gateway)
Ratan Gupta82549cc2017-04-21 08:45:23 +0530103{
Ratan Guptafc2c7242017-05-29 08:46:06 +0530104
105 if (dHCPEnabled())
106 {
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530107 log<level::INFO>("DHCP enabled on the interface"),
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500108 entry("INTERFACE=%s", interfaceName().c_str());
109 dHCPEnabled(false);
110 }
111
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500112 IP::AddressOrigin origin = IP::AddressOrigin::Static;
113
114 int addressFamily = (protType == IP::Protocol::IPv4) ? AF_INET : AF_INET6;
115
116 if (!isValidIP(addressFamily, ipaddress))
117 {
118 log<level::ERR>("Not a valid IP address"),
119 entry("ADDRESS=%s", ipaddress.c_str());
120 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"),
121 Argument::ARGUMENT_VALUE(ipaddress.c_str()));
122 }
123
124 if (!gateway.empty() && (!isValidIP(addressFamily, gateway)))
125 {
126 log<level::ERR>("Not a valid Gateway"),
127 entry("GATEWAY=%s", gateway.c_str());
128 elog<InvalidArgument>(Argument::ARGUMENT_NAME("gateway"),
129 Argument::ARGUMENT_VALUE(gateway.c_str()));
130 }
131
132 if (!isValidPrefix(addressFamily, prefixLength))
133 {
134 log<level::ERR>("PrefixLength is not correct "),
135 entry("PREFIXLENGTH=%d", gateway.c_str());
Gunnar Mills57d9c502018-09-14 14:42:34 -0500136 elog<InvalidArgument>(
137 Argument::ARGUMENT_NAME("prefixLength"),
138 Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str()));
Ratan Guptafc2c7242017-05-29 08:46:06 +0530139 }
140
Gunnar Mills57d9c502018-09-14 14:42:34 -0500141 std::string objectPath =
142 generateObjectPath(protType, ipaddress, prefixLength, gateway);
143 this->addrs.emplace(ipaddress,
144 std::make_shared<phosphor::network::IPAddress>(
145 bus, objectPath.c_str(), *this, protType, ipaddress,
146 origin, prefixLength, gateway));
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530147
Ratan Guptae05083a2017-09-16 07:12:11 +0530148 manager.writeToConfigurationFile();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530149}
150
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530151/*
152Note: We don't have support for ethtool now
153will enable this code once we bring the ethtool
154in the image.
155TODO: https://github.com/openbmc/openbmc/issues/1484
156*/
Ratan Gupta82549cc2017-04-21 08:45:23 +0530157
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530158InterfaceInfo EthernetInterface::getInterfaceInfo() const
159{
160 int sock{-1};
Gunnar Mills57d9c502018-09-14 14:42:34 -0500161 struct ifreq ifr
162 {
163 0
164 };
165 struct ethtool_cmd edata
166 {
167 0
168 };
169 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{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500212 struct ifreq ifr
213 {
214 };
215 char macAddress[mac_address::size]{};
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530216
217 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
218 if (sock < 0)
219 {
220 log<level::ERR>("socket creation failed:",
221 entry("ERROR=%s", strerror(errno)));
222 return macAddress;
223 }
224
Ratan Guptada7d3af2017-08-13 17:49:56 +0530225 strcpy(ifr.ifr_name, interfaceName.c_str());
226 if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0)
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530227 {
Ratan Guptada7d3af2017-08-13 17:49:56 +0530228 log<level::ERR>("ioctl failed for SIOCGIFHWADDR:",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229 entry("ERROR=%s", strerror(errno)));
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530230 return macAddress;
231 }
232
Ratan Guptabd303b12017-08-18 17:10:07 +0530233 snprintf(macAddress, mac_address::size, mac_address::format,
Gunnar Mills57d9c502018-09-14 14:42:34 -0500234 ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1],
235 ifr.ifr_hwaddr.sa_data[2], ifr.ifr_hwaddr.sa_data[3],
236 ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530237
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530238 return macAddress;
239}
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530240
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530241std::string EthernetInterface::generateId(const std::string& ipaddress,
242 uint8_t prefixLength,
243 const std::string& gateway)
Ratan Gupta82549cc2017-04-21 08:45:23 +0530244{
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530245 std::stringstream hexId;
246 std::string hashString = ipaddress;
247 hashString += std::to_string(prefixLength);
248 hashString += gateway;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530249
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530250 // Only want 8 hex digits.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500251 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530252 return hexId.str();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530253}
254
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530255void EthernetInterface::deleteObject(const std::string& ipaddress)
256{
Ratan Gupta29b0e432017-05-25 12:51:40 +0530257 auto it = addrs.find(ipaddress);
Ratan Guptafc2c7242017-05-29 08:46:06 +0530258 if (it == addrs.end())
Ratan Gupta29b0e432017-05-25 12:51:40 +0530259 {
Ratan Guptafc2c7242017-05-29 08:46:06 +0530260 log<level::ERR>("DeleteObject:Unable to find the object.");
261 return;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530262 }
263 this->addrs.erase(it);
Ratan Guptae05083a2017-09-16 07:12:11 +0530264 manager.writeToConfigurationFile();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530265}
266
Ratan Guptae9c9b812017-09-22 17:15:37 +0530267void EthernetInterface::deleteVLANFromSystem(const std::string& interface)
Ratan Guptabc886292017-07-25 18:29:57 +0530268{
Ratan Guptabc886292017-07-25 18:29:57 +0530269 auto confDir = manager.getConfDir();
270 fs::path networkFile = confDir;
271 networkFile /= systemd::config::networkFilePrefix + interface +
272 systemd::config::networkFileSuffix;
273
274 fs::path deviceFile = confDir;
275 deviceFile /= interface + systemd::config::deviceFileSuffix;
276
277 // delete the vlan network file
278 if (fs::is_regular_file(networkFile))
279 {
280 fs::remove(networkFile);
281 }
282
283 // delete the vlan device file
284 if (fs::is_regular_file(deviceFile))
285 {
286 fs::remove(deviceFile);
287 }
Ratan Guptabc886292017-07-25 18:29:57 +0530288
289 // TODO systemd doesn't delete the virtual network interface
290 // even after deleting all the related configuartion.
291 // https://github.com/systemd/systemd/issues/6600
292 try
293 {
294 deleteInterface(interface);
295 }
296 catch (InternalFailure& e)
297 {
298 commit<InternalFailure>();
299 }
Ratan Guptae9c9b812017-09-22 17:15:37 +0530300}
301
302void EthernetInterface::deleteVLANObject(const std::string& interface)
303{
304 auto it = vlanInterfaces.find(interface);
305 if (it == vlanInterfaces.end())
306 {
307 log<level::ERR>("DeleteVLANObject:Unable to find the object",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500308 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptae9c9b812017-09-22 17:15:37 +0530309 return;
310 }
311
312 deleteVLANFromSystem(interface);
313 // delete the interface
314 vlanInterfaces.erase(it);
315
Ratan Guptae05083a2017-09-16 07:12:11 +0530316 manager.writeToConfigurationFile();
Ratan Guptabc886292017-07-25 18:29:57 +0530317}
318
Gunnar Mills57d9c502018-09-14 14:42:34 -0500319std::string EthernetInterface::generateObjectPath(
320 IP::Protocol addressType, const std::string& ipaddress,
321 uint8_t prefixLength, const std::string& gateway) const
Ratan Gupta82549cc2017-04-21 08:45:23 +0530322{
Ratan Gupta82549cc2017-04-21 08:45:23 +0530323 std::string type = convertForMessage(addressType);
Ratan Gupta29b0e432017-05-25 12:51:40 +0530324 type = type.substr(type.rfind('.') + 1);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530325 std::transform(type.begin(), type.end(), type.begin(), ::tolower);
326
327 std::experimental::filesystem::path objectPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530328 objectPath /= objPath;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530329 objectPath /= type;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530330 objectPath /= generateId(ipaddress, prefixLength, gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530331 return objectPath.string();
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530332}
333
Ratan Gupta87c13982017-06-15 09:27:27 +0530334bool EthernetInterface::dHCPEnabled(bool value)
335{
Ratan Gupta5978dd12017-07-25 13:47:13 +0530336 if (value == EthernetInterfaceIntf::dHCPEnabled())
337 {
338 return value;
339 }
340
Ratan Gupta87c13982017-06-15 09:27:27 +0530341 EthernetInterfaceIntf::dHCPEnabled(value);
Ratan Guptae05083a2017-09-16 07:12:11 +0530342 manager.writeToConfigurationFile();
Ratan Gupta87c13982017-06-15 09:27:27 +0530343 return value;
344}
345
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530346ServerList EthernetInterface::nameservers(ServerList value)
347{
348 try
349 {
350 EthernetInterfaceIntf::nameservers(value);
351
352 writeConfigurationFile();
353
354 // Currently we don't have systemd-resolved enabled
355 // in the openbmc. Once we update the network conf file,
356 // it should be read by systemd-resolved.service.
357
358 // The other reason to write the resolv conf is,
359 // we don't want to restart the networkd for nameserver change.
360 // as restarting of systemd-networkd takes more then 2 secs
361 writeDNSEntries(value, resolvConfFile);
362 }
363 catch (InternalFailure& e)
364 {
365 log<level::ERR>("Exception processing DNS entries");
366 }
367 return EthernetInterfaceIntf::nameservers();
368}
369
370ServerList EthernetInterface::getNameServerFromConf()
371{
372 fs::path confPath = manager.getConfDir();
373
374 std::string fileName = systemd::config::networkFilePrefix +
Gunnar Mills57d9c502018-09-14 14:42:34 -0500375 interfaceName() + systemd::config::networkFileSuffix;
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530376 confPath /= fileName;
377 ServerList servers;
Ratan Guptac27170a2017-11-22 15:44:42 +0530378 config::Parser parser(confPath.string());
379 auto rc = config::ReturnCode::SUCCESS;
380
381 std::tie(rc, servers) = parser.getValues("Network", "DNS");
382 if (rc != config::ReturnCode::SUCCESS)
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530383 {
Ratan Guptac27170a2017-11-22 15:44:42 +0530384 log<level::DEBUG>("Unable to get the value for network[DNS]",
385 entry("RC=%d", rc));
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530386 }
387 return servers;
388}
389
390void EthernetInterface::writeDNSEntries(const ServerList& dnsList,
391 const std::string& file)
392{
393 std::fstream outStream(file, std::fstream::out);
394 if (!outStream.is_open())
395 {
396 log<level::ERR>("Unable to open the file",
397 entry("FILE=%s", file.c_str()));
398 elog<InternalFailure>();
399 }
400
Ratan Guptafe116912017-11-10 16:00:59 +0530401 outStream << "### Generated manually via dbus settings ###\n";
Gunnar Mills57d9c502018-09-14 14:42:34 -0500402 for (const auto& server : dnsList)
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530403 {
404 outStream << "nameserver " << server << "\n";
405 }
406}
407
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530408void EthernetInterface::loadVLAN(VlanId id)
409{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500410 std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530411 std::string path = objPath;
412 path += "_" + std::to_string(id);
413
Gunnar Mills57d9c502018-09-14 14:42:34 -0500414 auto dhcpEnabled =
415 getDHCPValue(manager.getConfDir().string(), vlanInterfaceName);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530416
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530417 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500418 bus, path.c_str(), dhcpEnabled, id, *this, manager);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530419
Gunnar Mills57d9c502018-09-14 14:42:34 -0500420 // Fetch the ip address from the system
421 // and create the dbus object.
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530422 vlanIntf->createIPAddressObjects();
423
424 this->vlanInterfaces.emplace(std::move(vlanInterfaceName),
425 std::move(vlanIntf));
426}
427
Ratan Gupta5978dd12017-07-25 13:47:13 +0530428void EthernetInterface::createVLAN(VlanId id)
429{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500430 std::string vlanInterfaceName = interfaceName() + "." + std::to_string(id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530431 std::string path = objPath;
432 path += "_" + std::to_string(id);
433
Ratan Gupta5978dd12017-07-25 13:47:13 +0530434 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500435 bus, path.c_str(), false, id, *this, manager);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530436
437 // write the device file for the vlan interface.
438 vlanIntf->writeDeviceFile();
439
Gunnar Mills57d9c502018-09-14 14:42:34 -0500440 this->vlanInterfaces.emplace(vlanInterfaceName, std::move(vlanIntf));
Ratan Gupta5978dd12017-07-25 13:47:13 +0530441 // write the new vlan device entry to the configuration(network) file.
Ratan Guptae05083a2017-09-16 07:12:11 +0530442 manager.writeToConfigurationFile();
Ratan Gupta5978dd12017-07-25 13:47:13 +0530443}
Ratan Gupta2b106532017-07-25 16:05:02 +0530444
Ratan Gupta497c0c92017-08-22 19:15:59 +0530445ServerList EthernetInterface::getNTPServersFromConf()
446{
447 fs::path confPath = manager.getConfDir();
448
Gunnar Mills57d9c502018-09-14 14:42:34 -0500449 std::string fileName = systemd::config::networkFilePrefix +
450 interfaceName() + systemd::config::networkFileSuffix;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530451 confPath /= fileName;
Ratan Guptac27170a2017-11-22 15:44:42 +0530452
Ratan Gupta497c0c92017-08-22 19:15:59 +0530453 ServerList servers;
Ratan Guptac27170a2017-11-22 15:44:42 +0530454 config::Parser parser(confPath.string());
455 auto rc = config::ReturnCode::SUCCESS;
456
457 std::tie(rc, servers) = parser.getValues("Network", "NTP");
458 if (rc != config::ReturnCode::SUCCESS)
Ratan Gupta497c0c92017-08-22 19:15:59 +0530459 {
Ratan Guptac27170a2017-11-22 15:44:42 +0530460 log<level::DEBUG>("Unable to get the value for Network[NTP]",
461 entry("rc=%d", rc));
Ratan Gupta497c0c92017-08-22 19:15:59 +0530462 }
Ratan Guptac27170a2017-11-22 15:44:42 +0530463
Ratan Gupta497c0c92017-08-22 19:15:59 +0530464 return servers;
465}
466
467ServerList EthernetInterface::nTPServers(ServerList servers)
468{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500469 auto ntpServers = EthernetInterfaceIntf::nTPServers(servers);
Ratan Gupta497c0c92017-08-22 19:15:59 +0530470
471 writeConfigurationFile();
472 // timesynchd reads the NTP server configuration from the
473 // network file.
Lei YU250011e2018-02-01 14:07:06 +0800474 restartSystemdUnit(networkdService);
Ratan Gupta497c0c92017-08-22 19:15:59 +0530475 return ntpServers;
476}
Ratan Gupta2b106532017-07-25 16:05:02 +0530477// Need to merge the below function with the code which writes the
478// config file during factory reset.
479// TODO openbmc/openbmc#1751
480
481void EthernetInterface::writeConfigurationFile()
482{
483 // write all the static ip address in the systemd-network conf file
484
485 using namespace std::string_literals;
486 using AddressOrigin =
487 sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin;
488 namespace fs = std::experimental::filesystem;
Ratan Guptae05083a2017-09-16 07:12:11 +0530489
490 // if there is vlan interafce then write the configuration file
491 // for vlan also.
492
Gunnar Mills57d9c502018-09-14 14:42:34 -0500493 for (const auto& intf : vlanInterfaces)
Ratan Guptae05083a2017-09-16 07:12:11 +0530494 {
495 intf.second->writeConfigurationFile();
496 }
497
Ratan Gupta2b106532017-07-25 16:05:02 +0530498 fs::path confPath = manager.getConfDir();
499
Gunnar Mills57d9c502018-09-14 14:42:34 -0500500 std::string fileName = systemd::config::networkFilePrefix +
501 interfaceName() + systemd::config::networkFileSuffix;
Ratan Gupta2b106532017-07-25 16:05:02 +0530502 confPath /= fileName;
503 std::fstream stream;
504
505 stream.open(confPath.c_str(), std::fstream::out);
506 if (!stream.is_open())
507 {
508 log<level::ERR>("Unable to open the file",
509 entry("FILE=%s", confPath.c_str()));
510 elog<InternalFailure>();
511 }
512
513 // Write the device
Gunnar Mills57d9c502018-09-14 14:42:34 -0500514 stream << "["
515 << "Match"
516 << "]\n";
Ratan Gupta2b106532017-07-25 16:05:02 +0530517 stream << "Name=" << interfaceName() << "\n";
518
519 auto addrs = getAddresses();
520
521 // write the network section
Gunnar Mills57d9c502018-09-14 14:42:34 -0500522 stream << "["
523 << "Network"
524 << "]\n";
Oskar Senftad21fc22018-07-26 16:32:23 -0400525#ifdef LINK_LOCAL_AUTOCONFIGURATION
Nagaraju Goruganti24afe362017-09-21 07:40:26 -0500526 stream << "LinkLocalAddressing=yes\n";
Oskar Senftad21fc22018-07-26 16:32:23 -0400527#else
528 stream << "LinkLocalAddressing=no\n";
529#endif
Ratan Guptae9629412017-12-21 08:20:25 +0530530 stream << "IPv6AcceptRA=false\n";
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530531
532 // Add the VLAN entry
Gunnar Mills57d9c502018-09-14 14:42:34 -0500533 for (const auto& intf : vlanInterfaces)
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530534 {
535 stream << "VLAN=" << intf.second->EthernetInterface::interfaceName()
Gunnar Mills57d9c502018-09-14 14:42:34 -0500536 << "\n";
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530537 }
Nagaraju Gorugantie8b83ec2018-03-26 05:21:45 -0500538 // Add the DHCP entry
539 auto value = dHCPEnabled() ? "true"s : "false"s;
540 stream << "DHCP="s + value + "\n";
541
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600542 // When the interface configured as dhcp, we don't need below given entries
543 // in config file.
544 if (dHCPEnabled() == false)
Ratan Gupta2b106532017-07-25 16:05:02 +0530545 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500546 // Add the NTP server
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600547 for (const auto& ntp : EthernetInterfaceIntf::nTPServers())
Ratan Gupta2b106532017-07-25 16:05:02 +0530548 {
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600549 stream << "NTP=" << ntp << "\n";
550 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530551
Gunnar Mills57d9c502018-09-14 14:42:34 -0500552 // Add the DNS entry
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600553 for (const auto& dns : EthernetInterfaceIntf::nameservers())
Ratan Gupta2b106532017-07-25 16:05:02 +0530554 {
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600555 stream << "DNS=" << dns << "\n";
556 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530557
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600558 // Static
559 for (const auto& addr : addrs)
560 {
Oskar Senftad21fc22018-07-26 16:32:23 -0400561 if (addr.second->origin() == AddressOrigin::Static
562#ifndef LINK_LOCAL_AUTOCONFIGURATION
563 || addr.second->origin() == AddressOrigin::LinkLocal
564#endif
565 )
Ratan Gupta2b106532017-07-25 16:05:02 +0530566 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500567 std::string address =
568 addr.second->address() + "/" +
569 std::to_string(addr.second->prefixLength());
Ratan Gupta2b106532017-07-25 16:05:02 +0530570
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600571 stream << "Address=" << address << "\n";
572 }
573 }
574
575 if (manager.getSystemConf())
576 {
577 stream << "Gateway=" << manager.getSystemConf()->defaultGateway()
578 << "\n";
579 }
580
581 // write the route section
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600582 for (const auto& addr : addrs)
583 {
584 if (addr.second->origin() == AddressOrigin::Static)
585 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500586 int addressFamily = addr.second->type() == IP::Protocol::IPv4
587 ? AF_INET
588 : AF_INET6;
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600589
Gunnar Mills57d9c502018-09-14 14:42:34 -0500590 std::string destination =
591 getNetworkID(addressFamily, addr.second->address(),
592 addr.second->prefixLength());
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600593
594 if (addr.second->gateway() != "0.0.0.0" &&
Gunnar Mills57d9c502018-09-14 14:42:34 -0500595 addr.second->gateway() != "" && destination != "0.0.0.0" &&
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600596 destination != "")
597 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500598 stream << "["
599 << "Route"
600 << "]\n";
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600601 stream << "Gateway=" << addr.second->gateway() << "\n";
602 stream << "Destination=" << destination << "\n";
603 }
604 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530605 }
606 }
607
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600608 // Write the dhcp section irrespective of whether DHCP is enabled or not
609 writeDHCPSection(stream);
610
Ratan Gupta2b106532017-07-25 16:05:02 +0530611 stream.close();
Ratan Gupta2b106532017-07-25 16:05:02 +0530612}
613
614void EthernetInterface::writeDHCPSection(std::fstream& stream)
615{
616 using namespace std::string_literals;
Ratan Gupta2b106532017-07-25 16:05:02 +0530617 // write the dhcp section
618 stream << "[DHCP]\n";
619
620 // Hardcoding the client identifier to mac, to address below issue
621 // https://github.com/openbmc/openbmc/issues/1280
622 stream << "ClientIdentifier=mac\n";
623 if (manager.getDHCPConf())
624 {
625 auto value = manager.getDHCPConf()->dNSEnabled() ? "true"s : "false"s;
626 stream << "UseDNS="s + value + "\n";
627
628 value = manager.getDHCPConf()->nTPEnabled() ? "true"s : "false"s;
629 stream << "UseNTP="s + value + "\n";
630
631 value = manager.getDHCPConf()->hostNameEnabled() ? "true"s : "false"s;
632 stream << "UseHostname="s + value + "\n";
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -0600633
634 value =
635 manager.getDHCPConf()->sendHostNameEnabled() ? "true"s : "false"s;
636 stream << "SendHostname="s + value + "\n";
Ratan Gupta2b106532017-07-25 16:05:02 +0530637 }
638}
639
Ratan Guptabd303b12017-08-18 17:10:07 +0530640std::string EthernetInterface::mACAddress(std::string value)
641{
642 if (!mac_address::validate(value))
643 {
Gunnar Mills90480c42018-06-19 16:02:17 -0500644 log<level::ERR>("MACAddress is not valid.",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500645 entry("MAC=%s", value.c_str()));
Gunnar Mills90480c42018-06-19 16:02:17 -0500646 elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
647 Argument::ARGUMENT_VALUE(value.c_str()));
Ratan Guptabd303b12017-08-18 17:10:07 +0530648 }
649
650 // check whether MAC is broadcast mac.
651 auto intMac = mac_address::internal::convertToInt(value);
652
653 if (!(intMac ^ mac_address::broadcastMac))
654 {
Gunnar Mills90480c42018-06-19 16:02:17 -0500655 log<level::ERR>("MACAddress is a broadcast mac.",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500656 entry("MAC=%s", value.c_str()));
Gunnar Mills90480c42018-06-19 16:02:17 -0500657 elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
658 Argument::ARGUMENT_VALUE(value.c_str()));
Ratan Guptabd303b12017-08-18 17:10:07 +0530659 }
660
Patrick Ventured475cd62018-02-26 17:07:41 -0800661 // Check if the MAC changed.
662 auto pmac = MacAddressIntf::mACAddress();
663 if (strcasecmp(pmac.c_str(), value.c_str()) == 0)
664 {
665 return MacAddressIntf::mACAddress();
666 }
667
Ratan Guptabd303b12017-08-18 17:10:07 +0530668 // Allow the mac to be set if one of the condition is true.
669 // 1) Incoming Mac is of local admin type.
670 // or
671 // 2) Incoming mac is same as eeprom Mac.
672
673 if (!(intMac & mac_address::localAdminMask))
674 {
675 try
676 {
677 auto inventoryMac = mac_address::getfromInventory(bus);
Gunnar Mills57d9c502018-09-14 14:42:34 -0500678 auto intInventoryMac =
679 mac_address::internal::convertToInt(inventoryMac);
Ratan Guptabd303b12017-08-18 17:10:07 +0530680
681 if (intInventoryMac != intMac)
682 {
Gunnar Mills90480c42018-06-19 16:02:17 -0500683 log<level::ERR>("Given MAC address is neither a local Admin "
Gunnar Mills57d9c502018-09-14 14:42:34 -0500684 "type nor is same as in inventory");
Gunnar Mills90480c42018-06-19 16:02:17 -0500685 elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
686 Argument::ARGUMENT_VALUE(value.c_str()));
Ratan Guptabd303b12017-08-18 17:10:07 +0530687 }
688 }
Gunnar Mills57d9c502018-09-14 14:42:34 -0500689 catch (InternalFailure& e)
Ratan Guptabd303b12017-08-18 17:10:07 +0530690 {
Patrick Venturee0ad43a2017-11-29 18:19:54 -0800691 log<level::ERR>("Exception occurred during getting of MAC "
692 "address from Inventory");
Gunnar Millsce262822018-06-19 16:21:34 -0500693 elog<InternalFailure>();
Ratan Guptabd303b12017-08-18 17:10:07 +0530694 }
695 }
696 auto interface = interfaceName();
697 execute("/sbin/fw_setenv", "fw_setenv", "ethaddr", value.c_str());
Gunnar Mills57d9c502018-09-14 14:42:34 -0500698 // TODO: would replace below three calls
Ratan Guptabd303b12017-08-18 17:10:07 +0530699 // with restarting of systemd-netwokd
700 // through https://github.com/systemd/systemd/issues/6696
701 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "down");
Gunnar Mills57d9c502018-09-14 14:42:34 -0500702 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(),
703 "address", value.c_str());
Ratan Guptabd303b12017-08-18 17:10:07 +0530704
705 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "up");
706
707 auto mac = MacAddressIntf::mACAddress(std::move(value));
Gunnar Mills57d9c502018-09-14 14:42:34 -0500708 // update all the vlan interfaces
709 for (const auto& intf : vlanInterfaces)
Ratan Guptabd303b12017-08-18 17:10:07 +0530710 {
711 intf.second->updateMacAddress();
712 }
Ratan Gupta677ae122017-09-18 16:28:50 +0530713
714 // restart the systemd networkd so that dhcp client gets the
715 // ip for the changed mac address.
716 if (dHCPEnabled())
717 {
Lei YU250011e2018-02-01 14:07:06 +0800718 restartSystemdUnit(networkdService);
Ratan Gupta677ae122017-09-18 16:28:50 +0530719 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530720 return mac;
Ratan Guptabd303b12017-08-18 17:10:07 +0530721}
722
Ratan Guptae9c9b812017-09-22 17:15:37 +0530723void EthernetInterface::deleteAll()
724{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500725 if (EthernetInterfaceIntf::dHCPEnabled())
Ratan Guptae9c9b812017-09-22 17:15:37 +0530726 {
727 log<level::INFO>("DHCP enabled on the interface"),
Gunnar Mills57d9c502018-09-14 14:42:34 -0500728 entry("INTERFACE=%s", interfaceName().c_str());
Ratan Guptae9c9b812017-09-22 17:15:37 +0530729 }
730
731 // clear all the ip on the interface
732 addrs.clear();
733 manager.writeToConfigurationFile();
734}
735
Gunnar Mills57d9c502018-09-14 14:42:34 -0500736} // namespace network
737} // namespace phosphor