blob: 7c4e8bb7185a5bbc364fbc5fad738594534457bd [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 Guptae05083a2017-09-16 07:12:11 +0530137 manager.writeToConfigurationFile();
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 Guptae05083a2017-09-16 07:12:11 +0530249 manager.writeToConfigurationFile();
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);
Ratan Guptabc886292017-07-25 18:29:57 +0530285
286 // TODO systemd doesn't delete the virtual network interface
287 // even after deleting all the related configuartion.
288 // https://github.com/systemd/systemd/issues/6600
289 try
290 {
291 deleteInterface(interface);
292 }
293 catch (InternalFailure& e)
294 {
295 commit<InternalFailure>();
296 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530297
298 manager.writeToConfigurationFile();
Ratan Guptabc886292017-07-25 18:29:57 +0530299}
300
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530301std::string EthernetInterface::generateObjectPath(IP::Protocol addressType,
302 const std::string& ipaddress,
303 uint8_t prefixLength,
304 const std::string& gateway) const
Ratan Gupta82549cc2017-04-21 08:45:23 +0530305{
Ratan Gupta82549cc2017-04-21 08:45:23 +0530306 std::string type = convertForMessage(addressType);
Ratan Gupta29b0e432017-05-25 12:51:40 +0530307 type = type.substr(type.rfind('.') + 1);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530308 std::transform(type.begin(), type.end(), type.begin(), ::tolower);
309
310 std::experimental::filesystem::path objectPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530311 objectPath /= objPath;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530312 objectPath /= type;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530313 objectPath /= generateId(ipaddress, prefixLength, gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530314 return objectPath.string();
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530315}
316
Ratan Gupta87c13982017-06-15 09:27:27 +0530317bool EthernetInterface::dHCPEnabled(bool value)
318{
Ratan Gupta5978dd12017-07-25 13:47:13 +0530319 if (value == EthernetInterfaceIntf::dHCPEnabled())
320 {
321 return value;
322 }
323
Ratan Gupta87c13982017-06-15 09:27:27 +0530324 EthernetInterfaceIntf::dHCPEnabled(value);
Ratan Guptae05083a2017-09-16 07:12:11 +0530325 manager.writeToConfigurationFile();
Ratan Gupta87c13982017-06-15 09:27:27 +0530326 return value;
327}
328
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530329void EthernetInterface::loadVLAN(VlanId id)
330{
331 std::string vlanInterfaceName = interfaceName() + "." +
332 std::to_string(id);
333 std::string path = objPath;
334 path += "_" + std::to_string(id);
335
Ratan Gupta6e8df632017-08-13 09:41:58 +0530336 auto dhcpEnabled = getDHCPValue(manager.getConfDir().string(),
337 vlanInterfaceName);
338
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530339 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
340 bus,
341 path.c_str(),
Ratan Gupta6e8df632017-08-13 09:41:58 +0530342 dhcpEnabled,
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530343 id,
344 *this,
345 manager);
346
347 // Fetch the ip address from the system
348 // and create the dbus object.
349 vlanIntf->createIPAddressObjects();
350
351 this->vlanInterfaces.emplace(std::move(vlanInterfaceName),
352 std::move(vlanIntf));
353}
354
Ratan Gupta5978dd12017-07-25 13:47:13 +0530355void EthernetInterface::createVLAN(VlanId id)
356{
357 std::string vlanInterfaceName = interfaceName() + "." +
358 std::to_string(id);
359 std::string path = objPath;
360 path += "_" + std::to_string(id);
361
362
363 auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>(
364 bus,
365 path.c_str(),
Ratan Guptae05083a2017-09-16 07:12:11 +0530366 false,
Ratan Gupta5978dd12017-07-25 13:47:13 +0530367 id,
368 *this,
369 manager);
370
371 // write the device file for the vlan interface.
372 vlanIntf->writeDeviceFile();
373
Ratan Gupta6e8df632017-08-13 09:41:58 +0530374 this->vlanInterfaces.emplace(vlanInterfaceName,
Ratan Gupta5978dd12017-07-25 13:47:13 +0530375 std::move(vlanIntf));
376 // write the new vlan device entry to the configuration(network) file.
Ratan Guptae05083a2017-09-16 07:12:11 +0530377 manager.writeToConfigurationFile();
Ratan Gupta5978dd12017-07-25 13:47:13 +0530378}
Ratan Gupta2b106532017-07-25 16:05:02 +0530379
380// Need to merge the below function with the code which writes the
381// config file during factory reset.
382// TODO openbmc/openbmc#1751
383
384void EthernetInterface::writeConfigurationFile()
385{
386 // write all the static ip address in the systemd-network conf file
387
388 using namespace std::string_literals;
389 using AddressOrigin =
390 sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin;
391 namespace fs = std::experimental::filesystem;
Ratan Guptae05083a2017-09-16 07:12:11 +0530392
393 // if there is vlan interafce then write the configuration file
394 // for vlan also.
395
396 for (const auto& intf: vlanInterfaces)
397 {
398 intf.second->writeConfigurationFile();
399 }
400
Ratan Gupta2b106532017-07-25 16:05:02 +0530401 fs::path confPath = manager.getConfDir();
402
Ratan Guptabc886292017-07-25 18:29:57 +0530403 std::string fileName = systemd::config::networkFilePrefix + interfaceName() +
404 systemd::config::networkFileSuffix;
Ratan Gupta2b106532017-07-25 16:05:02 +0530405 confPath /= fileName;
406 std::fstream stream;
407
408 stream.open(confPath.c_str(), std::fstream::out);
409 if (!stream.is_open())
410 {
411 log<level::ERR>("Unable to open the file",
412 entry("FILE=%s", confPath.c_str()));
413 elog<InternalFailure>();
414 }
415
416 // Write the device
417 stream << "[" << "Match" << "]\n";
418 stream << "Name=" << interfaceName() << "\n";
419
420 auto addrs = getAddresses();
421
422 // write the network section
423 stream << "[" << "Network" << "]\n";
Ratan Gupta4f67dac2017-08-28 22:18:21 +0530424
425 // Add the VLAN entry
426 for (const auto& intf: vlanInterfaces)
427 {
428 stream << "VLAN=" << intf.second->EthernetInterface::interfaceName()
429 << "\n";
430 }
431
Ratan Gupta2b106532017-07-25 16:05:02 +0530432 // DHCP
433 if (dHCPEnabled() == true)
434 {
435 // write the dhcp section if interface is
436 // configured as dhcp.
437 writeDHCPSection(stream);
438 stream.close();
439 return;
440 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530441
442 // Static
443 for (const auto& addr : addrs)
444 {
445 if (addr.second->origin() == AddressOrigin::Static)
446 {
447 std::string address = addr.second->address() + "/" + std::to_string(
448 addr.second->prefixLength());
449
450 stream << "Address=" << address << "\n";
451 }
452 }
453
454 if (manager.getSystemConf())
455 {
456 stream << "Gateway=" << manager.getSystemConf()->defaultGateway()
457 << "\n";
458 }
459 // write the route section
460 stream << "[" << "Route" << "]\n";
461 for(const auto& addr : addrs)
462 {
463 if (addr.second->origin() == AddressOrigin::Static)
464 {
465 int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6;
466 std::string destination = getNetworkID(
467 addressFamily,
468 addr.second->address(),
469 addr.second->prefixLength());
470
471 if (addr.second->gateway() != "0.0.0.0" &&
472 addr.second->gateway() != "" &&
473 destination != "0.0.0.0" &&
474 destination != "")
475 {
476 stream << "Gateway=" << addr.second->gateway() << "\n";
477 stream << "Destination=" << destination << "\n";
478 }
479
480 }
481 }
482
483 stream.close();
Ratan Gupta2b106532017-07-25 16:05:02 +0530484}
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