blob: bcb8f6cbddc2ee7fd202d342f00dafe2444d08a5 [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();
Ratan Gupta87c13982017-06-15 09:27:27 +0530327 return value;
328}
329
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530330void EthernetInterface::loadVLAN(VlanId id)
331{
332 std::string vlanInterfaceName = interfaceName() + "." +
333 std::to_string(id);
334 std::string path = objPath;
335 path += "_" + std::to_string(id);
336
Ratan Gupta6e8df632017-08-13 09:41:58 +0530337 auto dhcpEnabled = getDHCPValue(manager.getConfDir().string(),
338 vlanInterfaceName);
339
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530340 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
341 bus,
342 path.c_str(),
Ratan Gupta6e8df632017-08-13 09:41:58 +0530343 dhcpEnabled,
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530344 id,
345 *this,
346 manager);
347
348 // Fetch the ip address from the system
349 // and create the dbus object.
350 vlanIntf->createIPAddressObjects();
351
352 this->vlanInterfaces.emplace(std::move(vlanInterfaceName),
353 std::move(vlanIntf));
354}
355
Ratan Gupta5978dd12017-07-25 13:47:13 +0530356void EthernetInterface::createVLAN(VlanId id)
357{
358 std::string vlanInterfaceName = interfaceName() + "." +
359 std::to_string(id);
360 std::string path = objPath;
361 path += "_" + std::to_string(id);
362
363
364 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
365 bus,
366 path.c_str(),
367 EthernetInterfaceIntf::dHCPEnabled(),
368 id,
369 *this,
370 manager);
371
372 // write the device file for the vlan interface.
373 vlanIntf->writeDeviceFile();
Ratan Gupta96699b72017-09-08 17:34:07 +0530374 vlanIntf->writeConfigurationFile();
Ratan Gupta5978dd12017-07-25 13:47:13 +0530375
Ratan Gupta6e8df632017-08-13 09:41:58 +0530376 this->vlanInterfaces.emplace(vlanInterfaceName,
Ratan Gupta5978dd12017-07-25 13:47:13 +0530377 std::move(vlanIntf));
378 // write the new vlan device entry to the configuration(network) file.
Ratan Gupta2b106532017-07-25 16:05:02 +0530379 writeConfigurationFile();
Ratan Gupta5978dd12017-07-25 13:47:13 +0530380}
Ratan Gupta2b106532017-07-25 16:05:02 +0530381
382// Need to merge the below function with the code which writes the
383// config file during factory reset.
384// TODO openbmc/openbmc#1751
385
386void EthernetInterface::writeConfigurationFile()
387{
388 // write all the static ip address in the systemd-network conf file
389
390 using namespace std::string_literals;
391 using AddressOrigin =
392 sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin;
393 namespace fs = std::experimental::filesystem;
394 fs::path confPath = manager.getConfDir();
395
Ratan Guptabc886292017-07-25 18:29:57 +0530396 std::string fileName = systemd::config::networkFilePrefix + interfaceName() +
397 systemd::config::networkFileSuffix;
Ratan Gupta2b106532017-07-25 16:05:02 +0530398 confPath /= fileName;
399 std::fstream stream;
400
401 stream.open(confPath.c_str(), std::fstream::out);
402 if (!stream.is_open())
403 {
404 log<level::ERR>("Unable to open the file",
405 entry("FILE=%s", confPath.c_str()));
406 elog<InternalFailure>();
407 }
408
409 // Write the device
410 stream << "[" << "Match" << "]\n";
411 stream << "Name=" << interfaceName() << "\n";
412
413 auto addrs = getAddresses();
414
415 // write the network section
416 stream << "[" << "Network" << "]\n";
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530417
418 // Add the VLAN entry
419 for (const auto& intf: vlanInterfaces)
420 {
421 stream << "VLAN=" << intf.second->EthernetInterface::interfaceName()
422 << "\n";
423 }
424
Ratan Gupta2b106532017-07-25 16:05:02 +0530425 // DHCP
426 if (dHCPEnabled() == true)
427 {
428 // write the dhcp section if interface is
429 // configured as dhcp.
430 writeDHCPSection(stream);
431 stream.close();
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530432 restartSystemdUnit("systemd-networkd.service");
Ratan Gupta2b106532017-07-25 16:05:02 +0530433 return;
434 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530435
436 // Static
437 for (const auto& addr : addrs)
438 {
439 if (addr.second->origin() == AddressOrigin::Static)
440 {
441 std::string address = addr.second->address() + "/" + std::to_string(
442 addr.second->prefixLength());
443
444 stream << "Address=" << address << "\n";
445 }
446 }
447
448 if (manager.getSystemConf())
449 {
450 stream << "Gateway=" << manager.getSystemConf()->defaultGateway()
451 << "\n";
452 }
453 // write the route section
454 stream << "[" << "Route" << "]\n";
455 for(const auto& addr : addrs)
456 {
457 if (addr.second->origin() == AddressOrigin::Static)
458 {
459 int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6;
460 std::string destination = getNetworkID(
461 addressFamily,
462 addr.second->address(),
463 addr.second->prefixLength());
464
465 if (addr.second->gateway() != "0.0.0.0" &&
466 addr.second->gateway() != "" &&
467 destination != "0.0.0.0" &&
468 destination != "")
469 {
470 stream << "Gateway=" << addr.second->gateway() << "\n";
471 stream << "Destination=" << destination << "\n";
472 }
473
474 }
475 }
476
477 stream.close();
478 restartSystemdUnit("systemd-networkd.service");
479}
480
481void EthernetInterface::writeDHCPSection(std::fstream& stream)
482{
483 using namespace std::string_literals;
484 stream << "DHCP=true\n";
485 // write the dhcp section
486 stream << "[DHCP]\n";
487
488 // Hardcoding the client identifier to mac, to address below issue
489 // https://github.com/openbmc/openbmc/issues/1280
490 stream << "ClientIdentifier=mac\n";
491 if (manager.getDHCPConf())
492 {
493 auto value = manager.getDHCPConf()->dNSEnabled() ? "true"s : "false"s;
494 stream << "UseDNS="s + value + "\n";
495
496 value = manager.getDHCPConf()->nTPEnabled() ? "true"s : "false"s;
497 stream << "UseNTP="s + value + "\n";
498
499 value = manager.getDHCPConf()->hostNameEnabled() ? "true"s : "false"s;
500 stream << "UseHostname="s + value + "\n";
501 }
502}
503
Ratan Guptabd303b12017-08-18 17:10:07 +0530504std::string EthernetInterface::mACAddress(std::string value)
505{
506 if (!mac_address::validate(value))
507 {
508 log<level::DEBUG>("MACAddress is not valid.",
509 entry("MAC=%s", value.c_str()));
510 return MacAddressIntf::mACAddress();
511 }
512
513 // check whether MAC is broadcast mac.
514 auto intMac = mac_address::internal::convertToInt(value);
515
516 if (!(intMac ^ mac_address::broadcastMac))
517 {
518 log<level::DEBUG>("MACAddress is a broadcast mac.",
519 entry("MAC=%s", value.c_str()));
520 return MacAddressIntf::mACAddress();
521 }
522
523 // Allow the mac to be set if one of the condition is true.
524 // 1) Incoming Mac is of local admin type.
525 // or
526 // 2) Incoming mac is same as eeprom Mac.
527
528 if (!(intMac & mac_address::localAdminMask))
529 {
530 try
531 {
532 auto inventoryMac = mac_address::getfromInventory(bus);
533 auto intInventoryMac = mac_address::internal::convertToInt(inventoryMac);
534
535 if (intInventoryMac != intMac)
536 {
537 log<level::DEBUG>("Given MAC address is neither a local Admin \
538 type nor is same as in inventory");
539 return MacAddressIntf::mACAddress();
540 }
541 }
542 catch(InternalFailure& e)
543 {
544 log<level::ERR>("Exception occured during getting of MAC \
545 address from Inventory");
546 return MacAddressIntf::mACAddress();
547 }
548 }
549 auto interface = interfaceName();
550 execute("/sbin/fw_setenv", "fw_setenv", "ethaddr", value.c_str());
551 //TODO: would replace below three calls
552 // with restarting of systemd-netwokd
553 // through https://github.com/systemd/systemd/issues/6696
554 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "down");
555 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "address",
556 value.c_str());
557
558 execute("/sbin/ip", "ip", "link", "set", "dev", interface.c_str(), "up");
559
560 auto mac = MacAddressIntf::mACAddress(std::move(value));
561 //update all the vlan interfaces
562 for(const auto& intf: vlanInterfaces)
563 {
564 intf.second->updateMacAddress();
565 }
566 return mac;
567
568}
569
Ratan Gupta91a99cc2017-04-14 16:32:09 +0530570}//namespace network
571}//namespace phosphor