blob: 39c12cfc278d0b1916f59efac0001727134c35d7 [file] [log] [blame]
Ratan Gupta82549cc2017-04-21 08:45:23 +05301#include "config.h"
Ratan Gupta91a99cc2017-04-14 16:32:09 +05302#include "ethernet_interface.hpp"
Ratan Gupta2b106532017-07-25 16:05:02 +05303#include "ipaddress.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 Gupta2b106532017-07-25 16:05:02 +05306#include "vlan_interface.hpp"
7#include "xyz/openbmc_project/Common/error.hpp"
Ratan Gupta91a99cc2017-04-14 16:32:09 +05308
Ratan Gupta2b106532017-07-25 16:05:02 +05309#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta91a99cc2017-04-14 16:32:09 +053010#include <phosphor-logging/log.hpp>
11
Ratan Gupta82549cc2017-04-21 08:45:23 +053012#include <arpa/inet.h>
Ratan Gupta91a99cc2017-04-14 16:32:09 +053013#include <linux/ethtool.h>
Ratan Gupta91a99cc2017-04-14 16:32:09 +053014#include <linux/sockios.h>
Ratan Gupta2b106532017-07-25 16:05:02 +053015#include <net/if.h>
Ratan Gupta91a99cc2017-04-14 16:32:09 +053016#include <netinet/in.h>
17#include <sys/ioctl.h>
18#include <sys/socket.h>
19#include <unistd.h>
20
Ratan Gupta2b106532017-07-25 16:05:02 +053021
Ratan Gupta82549cc2017-04-21 08:45:23 +053022#include <algorithm>
23#include <experimental/filesystem>
Ratan Gupta2b106532017-07-25 16:05:02 +053024#include <fstream>
25#include <sstream>
26#include <string>
Ratan Gupta82549cc2017-04-21 08:45:23 +053027
Ratan Gupta91a99cc2017-04-14 16:32:09 +053028namespace phosphor
29{
30namespace network
31{
32
33using namespace phosphor::logging;
Ratan Gupta2b106532017-07-25 16:05:02 +053034using namespace sdbusplus::xyz::openbmc_project::Common::Error;
35
Ratan Gupta91a99cc2017-04-14 16:32:09 +053036EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus,
37 const std::string& objPath,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053038 bool dhcpEnabled,
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053039 Manager& parent,
40 bool emitSignal) :
Ratan Gupta82549cc2017-04-21 08:45:23 +053041 Ifaces(bus, objPath.c_str(), true),
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053042 bus(bus),
Ratan Gupta47722dc2017-05-26 18:32:23 +053043 manager(parent),
44 objPath(objPath)
Ratan Gupta91a99cc2017-04-14 16:32:09 +053045{
46 auto intfName = objPath.substr(objPath.rfind("/") + 1);
Ratan Gupta5978dd12017-07-25 13:47:13 +053047 std::replace(intfName.begin(), intfName.end(), '_', '.');
Ratan Gupta91a99cc2017-04-14 16:32:09 +053048 interfaceName(intfName);
Ratan Guptac35481d2017-08-18 06:12:26 +053049 EthernetInterfaceIntf::dHCPEnabled(dhcpEnabled);
Ratan Guptabd303b12017-08-18 17:10:07 +053050 MacAddressIntf::mACAddress(getMACAddress(intfName));
Ratan Gupta5978dd12017-07-25 13:47:13 +053051
Ratan Gupta29b0e432017-05-25 12:51:40 +053052 // Emit deferred signal.
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053053 if (emitSignal)
54 {
55 this->emit_object_added();
56 }
Ratan Gupta29b0e432017-05-25 12:51:40 +053057}
58
Ratan Gupta87c13982017-06-15 09:27:27 +053059void EthernetInterface::createIPAddressObjects()
Ratan Gupta29b0e432017-05-25 12:51:40 +053060{
Ratan Gupta82549cc2017-04-21 08:45:23 +053061 std::string gateway;
Ratan Gupta87c13982017-06-15 09:27:27 +053062 addrs.clear();
Ratan Gupta82549cc2017-04-21 08:45:23 +053063
Ratan Gupta87c13982017-06-15 09:27:27 +053064 auto addrs = getInterfaceAddrs()[interfaceName()];
Ratan Gupta5978dd12017-07-25 13:47:13 +053065
Ratan Gupta82549cc2017-04-21 08:45:23 +053066 IP::Protocol addressType = IP::Protocol::IPv4;
Ratan Gupta29b0e432017-05-25 12:51:40 +053067 IP::AddressOrigin origin = IP::AddressOrigin::Static;
Ratan Guptafc2c7242017-05-29 08:46:06 +053068 route::Table routingTable;
Ratan Gupta5978dd12017-07-25 13:47:13 +053069
Ratan Gupta6a387c12017-08-03 13:26:19 +053070 for (auto& addr : addrs)
Ratan Gupta82549cc2017-04-21 08:45:23 +053071 {
72 if (addr.addrType == AF_INET6)
73 {
74 addressType = IP::Protocol::IPv6;
75 }
Ratan Guptafc2c7242017-05-29 08:46:06 +053076 if (dHCPEnabled())
77 {
78 origin = IP::AddressOrigin::DHCP;
79 }
80 else if (isLinkLocal(addr.ipaddress))
81 {
82 origin = IP::AddressOrigin::LinkLocal;
83 }
84 gateway = routingTable.getGateway(addr.addrType, addr.ipaddress, addr.prefix);
Ratan Gupta82549cc2017-04-21 08:45:23 +053085
Ratan Gupta65e5abe2017-05-23 13:20:44 +053086 std::string ipAddressObjectPath = generateObjectPath(addressType,
87 addr.ipaddress,
88 addr.prefix,
89 gateway);
Ratan Guptafc2c7242017-05-29 08:46:06 +053090
Ratan Gupta82549cc2017-04-21 08:45:23 +053091 this->addrs.emplace(
Ratan Guptae578d562017-08-02 07:04:16 +053092 std::move(addr.ipaddress),
93 std::make_shared<phosphor::network::IPAddress>(
Ratan Gupta719f83a2017-06-02 11:54:53 +053094 bus,
95 ipAddressObjectPath.c_str(),
96 *this,
97 addressType,
Ratan Gupta82549cc2017-04-21 08:45:23 +053098 addr.ipaddress,
Ratan Gupta29b0e432017-05-25 12:51:40 +053099 origin,
Ratan Gupta719f83a2017-06-02 11:54:53 +0530100 addr.prefix,
Ratan Guptae578d562017-08-02 07:04:16 +0530101 gateway));
Ratan Gupta82549cc2017-04-21 08:45:23 +0530102 }
Ratan Guptafc2c7242017-05-29 08:46:06 +0530103
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530104}
105
Ratan Gupta82549cc2017-04-21 08:45:23 +0530106void EthernetInterface::iP(IP::Protocol protType,
107 std::string ipaddress,
108 uint8_t prefixLength,
109 std::string gateway)
110{
Ratan Guptafc2c7242017-05-29 08:46:06 +0530111
112 if (dHCPEnabled())
113 {
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530114 log<level::INFO>("DHCP enabled on the interface"),
Ratan Guptabd303b12017-08-18 17:10:07 +0530115 entry("INTERFACE=%s",interfaceName().c_str());
Ratan Guptafc2c7242017-05-29 08:46:06 +0530116 return;
117 }
118
Ratan Gupta29b0e432017-05-25 12:51:40 +0530119 IP::AddressOrigin origin = IP::AddressOrigin::Static;
120
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530121 std::string objectPath = generateObjectPath(protType,
122 ipaddress,
123 prefixLength,
124 gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530125 this->addrs.emplace(
Ratan Guptae578d562017-08-02 07:04:16 +0530126 std::move(ipaddress),
127 std::make_shared<phosphor::network::IPAddress>(
128 bus,
129 objectPath.c_str(),
130 *this,
131 protType,
132 ipaddress,
133 origin,
134 prefixLength,
135 gateway));
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530136
Ratan Gupta2b106532017-07-25 16:05:02 +0530137 writeConfigurationFile();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530138}
139
140
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530141/*
142Note: We don't have support for ethtool now
143will enable this code once we bring the ethtool
144in the image.
145TODO: https://github.com/openbmc/openbmc/issues/1484
146*/
Ratan Gupta82549cc2017-04-21 08:45:23 +0530147
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530148InterfaceInfo EthernetInterface::getInterfaceInfo() const
149{
150 int sock{-1};
151 struct ifreq ifr{0};
152 struct ethtool_cmd edata{0};
153 LinkSpeed speed {0};
154 Autoneg autoneg {0};
155 DuplexMode duplex {0};
156 do
157 {
158 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
159 if (sock < 0)
160 {
161 log<level::ERR>("socket creation failed:",
162 entry("ERROR=%s", strerror(errno)));
163 break;
164 }
165
166 strncpy(ifr.ifr_name, interfaceName().c_str(), sizeof(ifr.ifr_name));
167 ifr.ifr_data = reinterpret_cast<char*>(&edata);
168
169 edata.cmd = ETHTOOL_GSET;
170
171 if (ioctl(sock, SIOCETHTOOL, &ifr) < 0)
172 {
173 log<level::ERR>("ioctl failed for SIOCETHTOOL:",
174 entry("ERROR=%s", strerror(errno)));
175 break;
176
177 }
178 speed = edata.speed;
179 duplex = edata.duplex;
180 autoneg = edata.autoneg;
181 }
182 while (0);
183
184 if (sock)
185 {
186 close(sock);
187 }
188 return std::make_tuple(speed, duplex, autoneg);
189}
190
191/** @brief get the mac address of the interface.
192 * @return macaddress on success
193 */
194
Ratan Guptada7d3af2017-08-13 17:49:56 +0530195std::string EthernetInterface::getMACAddress(
196 const std::string& interfaceName) const
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530197{
Ratan Guptada7d3af2017-08-13 17:49:56 +0530198 struct ifreq ifr{};
Ratan Guptabd303b12017-08-18 17:10:07 +0530199 char macAddress[mac_address::size] {};
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530200
201 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
202 if (sock < 0)
203 {
204 log<level::ERR>("socket creation failed:",
205 entry("ERROR=%s", strerror(errno)));
206 return macAddress;
207 }
208
Ratan Guptada7d3af2017-08-13 17:49:56 +0530209 strcpy(ifr.ifr_name, interfaceName.c_str());
210 if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0)
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530211 {
Ratan Guptada7d3af2017-08-13 17:49:56 +0530212 log<level::ERR>("ioctl failed for SIOCGIFHWADDR:",
213 entry("ERROR=%s", strerror(errno)));
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530214 return macAddress;
215 }
216
Ratan Guptabd303b12017-08-18 17:10:07 +0530217 snprintf(macAddress, mac_address::size, mac_address::format,
Ratan Guptada7d3af2017-08-13 17:49:56 +0530218 ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1],
219 ifr.ifr_hwaddr.sa_data[2], ifr.ifr_hwaddr.sa_data[3],
220 ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530221
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530222 return macAddress;
223}
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530224
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530225std::string EthernetInterface::generateId(const std::string& ipaddress,
226 uint8_t prefixLength,
227 const std::string& gateway)
Ratan Gupta82549cc2017-04-21 08:45:23 +0530228{
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530229 std::stringstream hexId;
230 std::string hashString = ipaddress;
231 hashString += std::to_string(prefixLength);
232 hashString += gateway;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530233
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530234 // Only want 8 hex digits.
235 hexId << std::hex << ((std::hash<std::string> {}(
Ratan Guptafc2c7242017-05-29 08:46:06 +0530236 hashString)) & 0xFFFFFFFF);
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530237 return hexId.str();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530238}
239
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530240void EthernetInterface::deleteObject(const std::string& ipaddress)
241{
Ratan Gupta29b0e432017-05-25 12:51:40 +0530242 auto it = addrs.find(ipaddress);
Ratan Guptafc2c7242017-05-29 08:46:06 +0530243 if (it == addrs.end())
Ratan Gupta29b0e432017-05-25 12:51:40 +0530244 {
Ratan Guptafc2c7242017-05-29 08:46:06 +0530245 log<level::ERR>("DeleteObject:Unable to find the object.");
246 return;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530247 }
248 this->addrs.erase(it);
Ratan Gupta2b106532017-07-25 16:05:02 +0530249 writeConfigurationFile();
Ratan Gupta82549cc2017-04-21 08:45:23 +0530250}
251
Ratan Guptabc886292017-07-25 18:29:57 +0530252void EthernetInterface::deleteVLANObject(const std::string& interface)
253{
254 using namespace std::string_literals;
255
256 auto it = vlanInterfaces.find(interface);
257 if (it == vlanInterfaces.end())
258 {
259 log<level::ERR>("DeleteVLANObject:Unable to find the object",
260 entry("INTERFACE=%s",interface.c_str()));
261 return;
262 }
263
264 auto confDir = manager.getConfDir();
265 fs::path networkFile = confDir;
266 networkFile /= systemd::config::networkFilePrefix + interface +
267 systemd::config::networkFileSuffix;
268
269 fs::path deviceFile = confDir;
270 deviceFile /= interface + systemd::config::deviceFileSuffix;
271
272 // delete the vlan network file
273 if (fs::is_regular_file(networkFile))
274 {
275 fs::remove(networkFile);
276 }
277
278 // delete the vlan device file
279 if (fs::is_regular_file(deviceFile))
280 {
281 fs::remove(deviceFile);
282 }
283 // delete the interface
284 vlanInterfaces.erase(it);
285 // restart the systemd-networkd
286
287 restartSystemdUnit("systemd-networkd.service");
288
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 }
300}
301
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530302std::string EthernetInterface::generateObjectPath(IP::Protocol addressType,
303 const std::string& ipaddress,
304 uint8_t prefixLength,
305 const std::string& gateway) const
Ratan Gupta82549cc2017-04-21 08:45:23 +0530306{
Ratan Gupta82549cc2017-04-21 08:45:23 +0530307 std::string type = convertForMessage(addressType);
Ratan Gupta29b0e432017-05-25 12:51:40 +0530308 type = type.substr(type.rfind('.') + 1);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530309 std::transform(type.begin(), type.end(), type.begin(), ::tolower);
310
311 std::experimental::filesystem::path objectPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530312 objectPath /= objPath;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530313 objectPath /= type;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530314 objectPath /= generateId(ipaddress, prefixLength, gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530315 return objectPath.string();
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530316}
317
Ratan Gupta87c13982017-06-15 09:27:27 +0530318bool EthernetInterface::dHCPEnabled(bool value)
319{
Ratan Gupta5978dd12017-07-25 13:47:13 +0530320 if (value == EthernetInterfaceIntf::dHCPEnabled())
321 {
322 return value;
323 }
324
Ratan Gupta87c13982017-06-15 09:27:27 +0530325 EthernetInterfaceIntf::dHCPEnabled(value);
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530326 writeConfigurationFile();
327 createIPAddressObjects();
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530328
Ratan Gupta87c13982017-06-15 09:27:27 +0530329 return value;
330}
331
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530332void EthernetInterface::loadVLAN(VlanId id)
333{
334 std::string vlanInterfaceName = interfaceName() + "." +
335 std::to_string(id);
336 std::string path = objPath;
337 path += "_" + std::to_string(id);
338
Ratan Gupta6e8df632017-08-13 09:41:58 +0530339 auto dhcpEnabled = getDHCPValue(manager.getConfDir().string(),
340 vlanInterfaceName);
341
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530342 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
343 bus,
344 path.c_str(),
Ratan Gupta6e8df632017-08-13 09:41:58 +0530345 dhcpEnabled,
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530346 id,
347 *this,
348 manager);
349
350 // Fetch the ip address from the system
351 // and create the dbus object.
352 vlanIntf->createIPAddressObjects();
353
354 this->vlanInterfaces.emplace(std::move(vlanInterfaceName),
355 std::move(vlanIntf));
356}
357
Ratan Gupta5978dd12017-07-25 13:47:13 +0530358void EthernetInterface::createVLAN(VlanId id)
359{
360 std::string vlanInterfaceName = interfaceName() + "." +
361 std::to_string(id);
362 std::string path = objPath;
363 path += "_" + std::to_string(id);
364
365
366 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
367 bus,
368 path.c_str(),
369 EthernetInterfaceIntf::dHCPEnabled(),
370 id,
371 *this,
372 manager);
373
374 // write the device file for the vlan interface.
375 vlanIntf->writeDeviceFile();
376
Ratan Gupta6e8df632017-08-13 09:41:58 +0530377 this->vlanInterfaces.emplace(vlanInterfaceName,
Ratan Gupta5978dd12017-07-25 13:47:13 +0530378 std::move(vlanIntf));
379 // write the new vlan device entry to the configuration(network) file.
Ratan Gupta2b106532017-07-25 16:05:02 +0530380 writeConfigurationFile();
Ratan Gupta6e8df632017-08-13 09:41:58 +0530381
382 // Create the dbus object for the link local ipv6 address.
383 vlanInterfaces[vlanInterfaceName]->createIPAddressObjects();
384
Ratan Gupta5978dd12017-07-25 13:47:13 +0530385}
Ratan Gupta2b106532017-07-25 16:05:02 +0530386
387// Need to merge the below function with the code which writes the
388// config file during factory reset.
389// TODO openbmc/openbmc#1751
390
391void EthernetInterface::writeConfigurationFile()
392{
393 // write all the static ip address in the systemd-network conf file
394
395 using namespace std::string_literals;
396 using AddressOrigin =
397 sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin;
398 namespace fs = std::experimental::filesystem;
399 fs::path confPath = manager.getConfDir();
400
Ratan Guptabc886292017-07-25 18:29:57 +0530401 std::string fileName = systemd::config::networkFilePrefix + interfaceName() +
402 systemd::config::networkFileSuffix;
Ratan Gupta2b106532017-07-25 16:05:02 +0530403 confPath /= fileName;
404 std::fstream stream;
405
406 stream.open(confPath.c_str(), std::fstream::out);
407 if (!stream.is_open())
408 {
409 log<level::ERR>("Unable to open the file",
410 entry("FILE=%s", confPath.c_str()));
411 elog<InternalFailure>();
412 }
413
414 // Write the device
415 stream << "[" << "Match" << "]\n";
416 stream << "Name=" << interfaceName() << "\n";
417
418 auto addrs = getAddresses();
419
420 // write the network section
421 stream << "[" << "Network" << "]\n";
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530422
423 // Add the VLAN entry
424 for (const auto& intf: vlanInterfaces)
425 {
426 stream << "VLAN=" << intf.second->EthernetInterface::interfaceName()
427 << "\n";
428 }
429
Ratan Gupta2b106532017-07-25 16:05:02 +0530430 // DHCP
431 if (dHCPEnabled() == true)
432 {
433 // write the dhcp section if interface is
434 // configured as dhcp.
435 writeDHCPSection(stream);
436 stream.close();
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530437 restartSystemdUnit("systemd-networkd.service");
Ratan Gupta2b106532017-07-25 16:05:02 +0530438 return;
439 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530440
441 // Static
442 for (const auto& addr : addrs)
443 {
444 if (addr.second->origin() == AddressOrigin::Static)
445 {
446 std::string address = addr.second->address() + "/" + std::to_string(
447 addr.second->prefixLength());
448
449 stream << "Address=" << address << "\n";
450 }
451 }
452
453 if (manager.getSystemConf())
454 {
455 stream << "Gateway=" << manager.getSystemConf()->defaultGateway()
456 << "\n";
457 }
458 // write the route section
459 stream << "[" << "Route" << "]\n";
460 for(const auto& addr : addrs)
461 {
462 if (addr.second->origin() == AddressOrigin::Static)
463 {
464 int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6;
465 std::string destination = getNetworkID(
466 addressFamily,
467 addr.second->address(),
468 addr.second->prefixLength());
469
470 if (addr.second->gateway() != "0.0.0.0" &&
471 addr.second->gateway() != "" &&
472 destination != "0.0.0.0" &&
473 destination != "")
474 {
475 stream << "Gateway=" << addr.second->gateway() << "\n";
476 stream << "Destination=" << destination << "\n";
477 }
478
479 }
480 }
481
482 stream.close();
483 restartSystemdUnit("systemd-networkd.service");
484}
485
486void EthernetInterface::writeDHCPSection(std::fstream& stream)
487{
488 using namespace std::string_literals;
489 stream << "DHCP=true\n";
490 // write the dhcp section
491 stream << "[DHCP]\n";
492
493 // Hardcoding the client identifier to mac, to address below issue
494 // https://github.com/openbmc/openbmc/issues/1280
495 stream << "ClientIdentifier=mac\n";
496 if (manager.getDHCPConf())
497 {
498 auto value = manager.getDHCPConf()->dNSEnabled() ? "true"s : "false"s;
499 stream << "UseDNS="s + value + "\n";
500
501 value = manager.getDHCPConf()->nTPEnabled() ? "true"s : "false"s;
502 stream << "UseNTP="s + value + "\n";
503
504 value = manager.getDHCPConf()->hostNameEnabled() ? "true"s : "false"s;
505 stream << "UseHostname="s + value + "\n";
506 }
507}
508
Ratan Guptabd303b12017-08-18 17:10:07 +0530509std::string EthernetInterface::mACAddress(std::string value)
510{
511 if (!mac_address::validate(value))
512 {
513 log<level::DEBUG>("MACAddress is not valid.",
514 entry("MAC=%s", value.c_str()));
515 return MacAddressIntf::mACAddress();
516 }
517
518 // check whether MAC is broadcast mac.
519 auto intMac = mac_address::internal::convertToInt(value);
520
521 if (!(intMac ^ mac_address::broadcastMac))
522 {
523 log<level::DEBUG>("MACAddress is a broadcast mac.",
524 entry("MAC=%s", value.c_str()));
525 return MacAddressIntf::mACAddress();
526 }
527
528 // Allow the mac to be set if one of the condition is true.
529 // 1) Incoming Mac is of local admin type.
530 // or
531 // 2) Incoming mac is same as eeprom Mac.
532
533 if (!(intMac & mac_address::localAdminMask))
534 {
535 try
536 {
537 auto inventoryMac = mac_address::getfromInventory(bus);
538 auto intInventoryMac = mac_address::internal::convertToInt(inventoryMac);
539
540 if (intInventoryMac != intMac)
541 {
542 log<level::DEBUG>("Given MAC address is neither a local Admin \
543 type nor is same as in inventory");
544 return MacAddressIntf::mACAddress();
545 }
546 }
547 catch(InternalFailure& e)
548 {
549 log<level::ERR>("Exception occured during getting of MAC \
550 address from Inventory");
551 return MacAddressIntf::mACAddress();
552 }
553 }
554 auto interface = interfaceName();
555 execute("/sbin/fw_setenv", "fw_setenv", "ethaddr", value.c_str());
556 //TODO: would replace below three calls
557 // with restarting of systemd-netwokd
558 // through https://github.com/systemd/systemd/issues/6696
559 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "down");
560 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "address",
561 value.c_str());
562
563 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "up");
564
565 auto mac = MacAddressIntf::mACAddress(std::move(value));
566 //update all the vlan interfaces
567 for(const auto& intf: vlanInterfaces)
568 {
569 intf.second->updateMacAddress();
570 }
571 return mac;
572
573}
574
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530575}//namespace network
576}//namespace phosphor