blob: d0768ac41e9836aab44de3b2f947ddfb8b0f2aa6 [file] [log] [blame]
Patrick Venture586d35b2018-09-07 19:56:18 -07001#include "transporthandler.hpp"
2
Patrick Venture3a5071a2018-09-12 13:27:42 -07003#include "app/channel.hpp"
Johnathan Mantey74a21022018-12-13 13:17:56 -08004#include "user_channel/channel_layer.hpp"
Patrick Venture3a5071a2018-09-12 13:27:42 -07005
Patrick Venture0b02be92018-08-31 11:55:55 -07006#include <arpa/inet.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07007
8#include <chrono>
Vernon Mauerybdda8002019-02-26 10:18:51 -08009#include <filesystem>
Patrick Venture0b02be92018-08-31 11:55:55 -070010#include <fstream>
Vernon Mauerye08fbff2019-04-03 09:19:34 -070011#include <ipmid/api.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070012#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070013#include <phosphor-logging/elog-errors.hpp>
14#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070015#include <sdbusplus/message/types.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070016#include <sdbusplus/timer.hpp>
tomjose26e17732016-03-03 08:52:51 -060017#include <string>
Patrick Venture3a5071a2018-09-12 13:27:42 -070018#include <xyz/openbmc_project/Common/error.hpp>
19
20#define SYSTEMD_NETWORKD_DBUS 1
21
22#ifdef SYSTEMD_NETWORKD_DBUS
23#include <mapper.h>
24#include <systemd/sd-bus.h>
25#endif
Patrick Venture586d35b2018-09-07 19:56:18 -070026
Vernon Mauery1b7f6f22019-03-13 13:11:25 -070027// timer for network changes
28std::unique_ptr<phosphor::Timer> networkTimer = nullptr;
Ratan Gupta1247e0b2018-03-07 10:47:25 +053029
Patrick Venture0b02be92018-08-31 11:55:55 -070030const int SIZE_MAC = 18; // xx:xx:xx:xx:xx:xx
Ratan Gupta1247e0b2018-03-07 10:47:25 +053031constexpr auto ipv4Protocol = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
Adriana Kobylake08fbc62016-02-09 16:17:23 -060032
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080033std::map<int, std::unique_ptr<struct ChannelConfig_t>> channelConfig;
Hariharasubramanian R83951912016-01-20 07:06:36 -060034
Ratan Guptab8e99552017-07-27 07:07:48 +053035using namespace phosphor::logging;
36using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta7a7f0122018-03-07 12:31:05 +053037
Vernon Mauery185b9f82018-07-20 10:52:36 -070038namespace fs = std::filesystem;
Hariharasubramanian R83951912016-01-20 07:06:36 -060039
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050040void register_netfn_transport_functions() __attribute__((constructor));
41
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080042struct ChannelConfig_t* getChannelConfig(int channel)
43{
44 auto item = channelConfig.find(channel);
45 if (item == channelConfig.end())
46 {
47 channelConfig[channel] = std::make_unique<struct ChannelConfig_t>();
48 }
49
50 return channelConfig[channel].get();
51}
52
Patrick Venture0b02be92018-08-31 11:55:55 -070053// Helper Function to get IP Address/NetMask/Gateway/MAC Address from Network
54// Manager or Cache based on Set-In-Progress State
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080055ipmi_ret_t getNetworkData(uint8_t lan_param, uint8_t* data, int channel)
tomjose26e17732016-03-03 08:52:51 -060056{
tomjose26e17732016-03-03 08:52:51 -060057 ipmi_ret_t rc = IPMI_CC_OK;
Ratan Guptab8e99552017-07-27 07:07:48 +053058 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Gupta533d03b2017-07-30 10:39:22 +053059
Johnathan Mantey74a21022018-12-13 13:17:56 -080060 auto ethdevice = ipmi::getChannelName(channel);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080061 // if ethdevice is an empty string they weren't expecting this channel.
62 if (ethdevice.empty())
63 {
64 // TODO: return error from getNetworkData()
65 return IPMI_CC_INVALID_FIELD_REQUEST;
66 }
67 auto ethIP = ethdevice + "/" + ipmi::network::IP_TYPE;
68 auto channelConf = getChannelConfig(channel);
69
Ratan Guptab8e99552017-07-27 07:07:48 +053070 try
tomjose26e17732016-03-03 08:52:51 -060071 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -080072 switch (static_cast<LanParam>(lan_param))
tomjose26e17732016-03-03 08:52:51 -060073 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -080074 case LanParam::IP:
Ratan Guptab8e99552017-07-27 07:07:48 +053075 {
76 std::string ipaddress;
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080077 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptab8e99552017-07-27 07:07:48 +053078 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +053079 try
80 {
Patrick Venture0b02be92018-08-31 11:55:55 -070081 auto ipObjectInfo =
82 ipmi::getIPObject(bus, ipmi::network::IP_INTERFACE,
83 ipmi::network::ROOT, ethIP);
Ratan Guptadd646202017-11-21 17:46:59 +053084
85 auto properties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -070086 bus, ipObjectInfo.second, ipObjectInfo.first,
87 ipmi::network::IP_INTERFACE);
Ratan Guptadd646202017-11-21 17:46:59 +053088
William A. Kennington III4c008022018-10-12 17:18:14 -070089 ipaddress =
Vernon Maueryf442e112019-04-09 11:44:36 -070090 std::get<std::string>(properties["Address"]);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +053091 }
Gunnar Millsc9fa69e2018-04-08 16:35:25 -050092 // ignore the exception, as it is a valid condition that
93 // the system is not configured with any IP.
Ratan Guptacc6cdbf2017-09-01 23:06:25 +053094 catch (InternalFailure& e)
95 {
96 // nothing to do.
97 }
Ratan Guptab8e99552017-07-27 07:07:48 +053098 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080099 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptab8e99552017-07-27 07:07:48 +0530100 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800101 ipaddress = channelConf->ipaddr;
Ratan Guptab8e99552017-07-27 07:07:48 +0530102 }
103
104 inet_pton(AF_INET, ipaddress.c_str(),
105 reinterpret_cast<void*>(data));
106 }
107 break;
108
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800109 case LanParam::IPSRC:
Ratan Guptab8e99552017-07-27 07:07:48 +0530110 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530111 std::string networkInterfacePath;
112
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800113 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptab8e99552017-07-27 07:07:48 +0530114 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530115 try
116 {
117 ipmi::ObjectTree ancestorMap;
118 // if the system is having ip object,then
119 // get the IP object.
120 auto ipObject = ipmi::getDbusObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700121 bus, ipmi::network::IP_INTERFACE,
122 ipmi::network::ROOT, ethIP);
Ratan Guptab8e99552017-07-27 07:07:48 +0530123
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530124 // Get the parent interface of the IP object.
125 try
126 {
127 ipmi::InterfaceList interfaces;
128 interfaces.emplace_back(
Patrick Venture0b02be92018-08-31 11:55:55 -0700129 ipmi::network::ETHERNET_INTERFACE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530130
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530131 ancestorMap = ipmi::getAllAncestors(
Patrick Venture0b02be92018-08-31 11:55:55 -0700132 bus, ipObject.first, std::move(interfaces));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530133 }
134 catch (InternalFailure& e)
135 {
136 // if unable to get the parent interface
137 // then commit the error and return.
Patrick Venture0b02be92018-08-31 11:55:55 -0700138 log<level::ERR>(
139 "Unable to get the parent interface",
140 entry("PATH=%s", ipObject.first.c_str()),
141 entry("INTERFACE=%s",
142 ipmi::network::ETHERNET_INTERFACE));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530143 break;
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530144 }
145 // for an ip object there would be single parent
146 // interface.
147 networkInterfacePath = ancestorMap.begin()->first;
148 }
149 catch (InternalFailure& e)
150 {
151 // if there is no ip configured on the system,then
152 // get the network interface object.
153 auto networkInterfaceObject = ipmi::getDbusObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700154 bus, ipmi::network::ETHERNET_INTERFACE,
155 ipmi::network::ROOT, ethdevice);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530156
157 networkInterfacePath = networkInterfaceObject.first;
158 }
159
160 auto variant = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700161 bus, ipmi::network::SERVICE, networkInterfacePath,
162 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled");
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530163
Vernon Maueryf442e112019-04-09 11:44:36 -0700164 auto dhcpEnabled = std::get<bool>(variant);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530165 // As per IPMI spec 2=>DHCP, 1=STATIC
Patrick Venture0b02be92018-08-31 11:55:55 -0700166 auto ipsrc = dhcpEnabled ? ipmi::network::IPOrigin::DHCP
167 : ipmi::network::IPOrigin::STATIC;
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530168
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700169 std::memcpy(data, &ipsrc, ipmi::network::IPSRC_SIZE_BYTE);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530170 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800171 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530172 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700173 std::memcpy(data, &(channelConf->ipsrc),
174 ipmi::network::IPSRC_SIZE_BYTE);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530175 }
176 }
177 break;
178
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800179 case LanParam::SUBNET:
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530180 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700181 unsigned long mask{};
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800182 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530183 {
184 try
185 {
Johnathan Mantey88416832019-01-30 15:51:04 -0800186 auto ipObjectInfo =
187 ipmi::getIPObject(bus, ipmi::network::IP_INTERFACE,
188 ipmi::network::ROOT, ethIP);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530189
190 auto properties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -0700191 bus, ipObjectInfo.second, ipObjectInfo.first,
192 ipmi::network::IP_INTERFACE);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530193
Vernon Maueryf442e112019-04-09 11:44:36 -0700194 auto prefix =
195 std::get<uint8_t>(properties["PrefixLength"]);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530196 mask = ipmi::network::MASK_32_BIT;
197 mask = htonl(mask << (ipmi::network::BITS_32 - prefix));
198 }
Gunnar Millsc9fa69e2018-04-08 16:35:25 -0500199 // ignore the exception, as it is a valid condition that
200 // the system is not configured with any IP.
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530201 catch (InternalFailure& e)
202 {
203 // nothing to do
204 }
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700205 std::memcpy(data, &mask,
206 ipmi::network::IPV4_ADDRESS_SIZE_BYTE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530207 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800208 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptab8e99552017-07-27 07:07:48 +0530209 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800210 inet_pton(AF_INET, channelConf->netmask.c_str(),
Ratan Guptab8e99552017-07-27 07:07:48 +0530211 reinterpret_cast<void*>(data));
Ratan Guptab8e99552017-07-27 07:07:48 +0530212 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530213 }
214 break;
215
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800216 case LanParam::GATEWAY:
Ratan Guptab8e99552017-07-27 07:07:48 +0530217 {
218 std::string gateway;
219
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800220 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptab8e99552017-07-27 07:07:48 +0530221 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530222 try
223 {
224 auto systemObject = ipmi::getDbusObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700225 bus, ipmi::network::SYSTEMCONFIG_INTERFACE,
226 ipmi::network::ROOT);
Ratan Guptab8e99552017-07-27 07:07:48 +0530227
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530228 auto systemProperties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -0700229 bus, systemObject.second, systemObject.first,
230 ipmi::network::SYSTEMCONFIG_INTERFACE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530231
Vernon Maueryf442e112019-04-09 11:44:36 -0700232 gateway = std::get<std::string>(
William A. Kennington III4c008022018-10-12 17:18:14 -0700233 systemProperties["DefaultGateway"]);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530234 }
Gunnar Millsc9fa69e2018-04-08 16:35:25 -0500235 // ignore the exception, as it is a valid condition that
236 // the system is not configured with any IP.
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530237 catch (InternalFailure& e)
238 {
239 // nothing to do
240 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530241 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800242 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptab8e99552017-07-27 07:07:48 +0530243 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800244 gateway = channelConf->gateway;
Ratan Guptab8e99552017-07-27 07:07:48 +0530245 }
246
247 inet_pton(AF_INET, gateway.c_str(),
248 reinterpret_cast<void*>(data));
Ratan Guptab8e99552017-07-27 07:07:48 +0530249 }
250 break;
251
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800252 case LanParam::MAC:
Ratan Guptab8e99552017-07-27 07:07:48 +0530253 {
254 std::string macAddress;
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800255 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptab8e99552017-07-27 07:07:48 +0530256 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700257 auto macObjectInfo =
258 ipmi::getDbusObject(bus, ipmi::network::MAC_INTERFACE,
259 ipmi::network::ROOT, ethdevice);
Ratan Guptab8e99552017-07-27 07:07:48 +0530260
261 auto variant = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700262 bus, macObjectInfo.second, macObjectInfo.first,
263 ipmi::network::MAC_INTERFACE, "MACAddress");
Ratan Guptab8e99552017-07-27 07:07:48 +0530264
Vernon Maueryf442e112019-04-09 11:44:36 -0700265 macAddress = std::get<std::string>(variant);
Ratan Guptab8e99552017-07-27 07:07:48 +0530266 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800267 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptab8e99552017-07-27 07:07:48 +0530268 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800269 macAddress = channelConf->macAddress;
Ratan Guptab8e99552017-07-27 07:07:48 +0530270 }
271
272 sscanf(macAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
Patrick Venture0b02be92018-08-31 11:55:55 -0700273 (data), (data + 1), (data + 2), (data + 3), (data + 4),
Ratan Guptab8e99552017-07-27 07:07:48 +0530274 (data + 5));
275 }
276 break;
277
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800278 case LanParam::VLAN:
Ratan Gupta533d03b2017-07-30 10:39:22 +0530279 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700280 uint16_t vlanID{};
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800281 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Gupta533d03b2017-07-30 10:39:22 +0530282 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530283 try
Ratan Gupta533d03b2017-07-30 10:39:22 +0530284 {
Ratan Guptadd646202017-11-21 17:46:59 +0530285 auto ipObjectInfo = ipmi::getIPObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700286 bus, ipmi::network::IP_INTERFACE,
287 ipmi::network::ROOT, ipmi::network::IP_TYPE);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530288
289 vlanID = static_cast<uint16_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700290 ipmi::network::getVLAN(ipObjectInfo.first));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530291
292 vlanID = htole16(vlanID);
293
294 if (vlanID)
295 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700296 // Enable the 16th bit
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530297 vlanID |= htole16(ipmi::network::VLAN_ENABLE_MASK);
298 }
299 }
Gunnar Millsc9fa69e2018-04-08 16:35:25 -0500300 // ignore the exception, as it is a valid condition that
301 // the system is not configured with any IP.
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530302 catch (InternalFailure& e)
303 {
304 // nothing to do
Ratan Gupta533d03b2017-07-30 10:39:22 +0530305 }
306
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700307 std::memcpy(data, &vlanID, ipmi::network::VLAN_SIZE_BYTE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530308 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800309 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Gupta533d03b2017-07-30 10:39:22 +0530310 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700311 std::memcpy(data, &(channelConf->vlanID),
312 ipmi::network::VLAN_SIZE_BYTE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530313 }
314 }
315 break;
316
Ratan Guptab8e99552017-07-27 07:07:48 +0530317 default:
318 rc = IPMI_CC_PARM_OUT_OF_RANGE;
tomjose26e17732016-03-03 08:52:51 -0600319 }
320 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530321 catch (InternalFailure& e)
tomjose26e17732016-03-03 08:52:51 -0600322 {
Ratan Guptab8e99552017-07-27 07:07:48 +0530323 commit<InternalFailure>();
324 rc = IPMI_CC_UNSPECIFIED_ERROR;
325 return rc;
tomjose26e17732016-03-03 08:52:51 -0600326 }
tomjose26e17732016-03-03 08:52:51 -0600327 return rc;
328}
329
Tom Josepha30c8d32018-03-22 02:15:03 +0530330namespace cipher
331{
332
333std::vector<uint8_t> getCipherList()
334{
335 std::vector<uint8_t> cipherList;
336
337 std::ifstream jsonFile(configFile);
338 if (!jsonFile.is_open())
339 {
340 log<level::ERR>("Channel Cipher suites file not found");
341 elog<InternalFailure>();
342 }
343
344 auto data = Json::parse(jsonFile, nullptr, false);
345 if (data.is_discarded())
346 {
347 log<level::ERR>("Parsing channel cipher suites JSON failed");
348 elog<InternalFailure>();
349 }
350
351 // Byte 1 is reserved
352 cipherList.push_back(0x00);
353
354 for (const auto& record : data)
355 {
356 cipherList.push_back(record.value(cipher, 0));
357 }
358
359 return cipherList;
360}
361
Patrick Venture0b02be92018-08-31 11:55:55 -0700362} // namespace cipher
Tom Josepha30c8d32018-03-22 02:15:03 +0530363
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500364ipmi_ret_t ipmi_transport_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700365 ipmi_request_t request,
366 ipmi_response_t response,
367 ipmi_data_len_t data_len,
368 ipmi_context_t context)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500369{
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500370 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800371 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500372 *data_len = 0;
373 return rc;
374}
375
Ratan Guptab8e99552017-07-27 07:07:48 +0530376struct set_lan_t
377{
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500378 uint8_t channel;
379 uint8_t parameter;
380 uint8_t data[8]; // Per IPMI spec, not expecting more than this size
Patrick Venture0b02be92018-08-31 11:55:55 -0700381} __attribute__((packed));
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500382
Richard Marian Thomaiyar75b480b2019-01-22 00:20:15 +0530383ipmi_ret_t checkAndUpdateNetwork(int channel)
384{
385 auto channelConf = getChannelConfig(channel);
386 using namespace std::chrono_literals;
387 // time to wait before applying the network changes.
388 constexpr auto networkTimeout = 10000000us; // 10 sec
389
390 // Skip the timer. Expecting more update as we are in SET_IN_PROGRESS
391 if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
392 {
393 return IPMI_CC_OK;
394 }
395
396 // Start the timer, if it is direct single param update without
397 // SET_IN_PROGRESS or many params updated through SET_IN_PROGRESS to
398 // SET_COMPLETE Note: Even for update with SET_IN_PROGRESS, don't apply the
399 // changes immediately, as ipmitool sends each param individually
400 // through SET_IN_PROGRESS to SET_COMPLETE.
401 channelConf->flush = true;
402 if (!networkTimer)
403 {
404 log<level::ERR>("Network timer is not instantiated");
405 return IPMI_CC_UNSPECIFIED_ERROR;
406 }
407 // start the timer.
408 networkTimer->start(networkTimeout);
409 return IPMI_CC_OK;
410}
411
Patrick Venture0b02be92018-08-31 11:55:55 -0700412ipmi_ret_t ipmi_transport_set_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Ratan Guptab8e99552017-07-27 07:07:48 +0530413 ipmi_request_t request,
414 ipmi_response_t response,
415 ipmi_data_len_t data_len,
416 ipmi_context_t context)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500417{
418 ipmi_ret_t rc = IPMI_CC_OK;
419 *data_len = 0;
Nan Li3d0df912016-10-18 19:51:41 +0800420
Ratan Guptab8e99552017-07-27 07:07:48 +0530421 char ipaddr[INET_ADDRSTRLEN];
422 char netmask[INET_ADDRSTRLEN];
423 char gateway[INET_ADDRSTRLEN];
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500424
Ratan Guptab8e99552017-07-27 07:07:48 +0530425 auto reqptr = reinterpret_cast<const set_lan_t*>(request);
426 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500427
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800428 // channel number is the lower nibble
429 int channel = reqptr->channel & CHANNEL_MASK;
Johnathan Mantey74a21022018-12-13 13:17:56 -0800430 auto ethdevice = ipmi::getChannelName(channel);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800431 if (ethdevice.empty())
432 {
433 return IPMI_CC_INVALID_FIELD_REQUEST;
434 }
435 auto channelConf = getChannelConfig(channel);
436
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800437 switch (static_cast<LanParam>(reqptr->parameter))
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500438 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800439 case LanParam::IP:
Hariharasubramanian R83951912016-01-20 07:06:36 -0600440 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700441 std::snprintf(ipaddr, INET_ADDRSTRLEN,
442 ipmi::network::IP_ADDRESS_FORMAT, reqptr->data[0],
443 reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500444
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800445 channelConf->ipaddr.assign(ipaddr);
Ratan Guptab8e99552017-07-27 07:07:48 +0530446 }
447 break;
448
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800449 case LanParam::IPSRC:
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530450 {
451 uint8_t ipsrc{};
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700452 std::memcpy(&ipsrc, reqptr->data, ipmi::network::IPSRC_SIZE_BYTE);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800453 channelConf->ipsrc = static_cast<ipmi::network::IPOrigin>(ipsrc);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530454 }
455 break;
456
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800457 case LanParam::MAC:
Ratan Guptab8e99552017-07-27 07:07:48 +0530458 {
459 char mac[SIZE_MAC];
460
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700461 std::snprintf(mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
462 reqptr->data[0], reqptr->data[1], reqptr->data[2],
463 reqptr->data[3], reqptr->data[4], reqptr->data[5]);
Ratan Guptab8e99552017-07-27 07:07:48 +0530464
Patrick Venture0b02be92018-08-31 11:55:55 -0700465 auto macObjectInfo =
466 ipmi::getDbusObject(bus, ipmi::network::MAC_INTERFACE,
467 ipmi::network::ROOT, ethdevice);
Ratan Guptab8e99552017-07-27 07:07:48 +0530468
Patrick Venture0b02be92018-08-31 11:55:55 -0700469 ipmi::setDbusProperty(
470 bus, macObjectInfo.second, macObjectInfo.first,
471 ipmi::network::MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptab8e99552017-07-27 07:07:48 +0530472
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800473 channelConf->macAddress = mac;
Ratan Guptab8e99552017-07-27 07:07:48 +0530474 }
475 break;
476
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800477 case LanParam::SUBNET:
Ratan Guptab8e99552017-07-27 07:07:48 +0530478 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700479 std::snprintf(netmask, INET_ADDRSTRLEN,
480 ipmi::network::IP_ADDRESS_FORMAT, reqptr->data[0],
481 reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800482 channelConf->netmask.assign(netmask);
Ratan Guptab8e99552017-07-27 07:07:48 +0530483 }
484 break;
485
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800486 case LanParam::GATEWAY:
Ratan Guptab8e99552017-07-27 07:07:48 +0530487 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700488 std::snprintf(gateway, INET_ADDRSTRLEN,
489 ipmi::network::IP_ADDRESS_FORMAT, reqptr->data[0],
490 reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800491 channelConf->gateway.assign(gateway);
Ratan Guptab8e99552017-07-27 07:07:48 +0530492 }
493 break;
494
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800495 case LanParam::VLAN:
Ratan Gupta533d03b2017-07-30 10:39:22 +0530496 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700497 uint16_t vlan{};
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700498 std::memcpy(&vlan, reqptr->data, ipmi::network::VLAN_SIZE_BYTE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530499 // We are not storing the enable bit
500 // We assume that ipmitool always send enable
501 // bit as 1.
502 vlan = le16toh(vlan);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800503 channelConf->vlanID = vlan;
Ratan Gupta533d03b2017-07-30 10:39:22 +0530504 }
505 break;
506
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800507 case LanParam::INPROGRESS:
Ratan Guptab8e99552017-07-27 07:07:48 +0530508 {
509 if (reqptr->data[0] == SET_COMPLETE)
510 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800511 channelConf->lan_set_in_progress = SET_COMPLETE;
Ratan Guptab8e99552017-07-27 07:07:48 +0530512
Patrick Venture0b02be92018-08-31 11:55:55 -0700513 log<level::INFO>(
514 "Network data from Cache",
515 entry("PREFIX=%s", channelConf->netmask.c_str()),
516 entry("ADDRESS=%s", channelConf->ipaddr.c_str()),
517 entry("GATEWAY=%s", channelConf->gateway.c_str()),
518 entry("VLAN=%d", channelConf->vlanID));
Ratan Guptab8e99552017-07-27 07:07:48 +0530519 }
520 else if (reqptr->data[0] == SET_IN_PROGRESS) // Set In Progress
521 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800522 channelConf->lan_set_in_progress = SET_IN_PROGRESS;
Ratan Guptab8e99552017-07-27 07:07:48 +0530523 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530524 }
525 break;
526
527 default:
528 {
Ratan Guptab8e99552017-07-27 07:07:48 +0530529 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Richard Marian Thomaiyar75b480b2019-01-22 00:20:15 +0530530 return rc;
Ratan Guptab8e99552017-07-27 07:07:48 +0530531 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530532 }
Richard Marian Thomaiyar75b480b2019-01-22 00:20:15 +0530533 rc = checkAndUpdateNetwork(channel);
vishwa1eaea4f2016-02-26 11:57:40 -0600534
tomjose26e17732016-03-03 08:52:51 -0600535 return rc;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500536}
537
Ratan Guptab8e99552017-07-27 07:07:48 +0530538struct get_lan_t
539{
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500540 uint8_t rev_channel;
541 uint8_t parameter;
542 uint8_t parameter_set;
543 uint8_t parameter_block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700544} __attribute__((packed));
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500545
Patrick Venture0b02be92018-08-31 11:55:55 -0700546ipmi_ret_t ipmi_transport_get_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Ratan Guptab8e99552017-07-27 07:07:48 +0530547 ipmi_request_t request,
548 ipmi_response_t response,
549 ipmi_data_len_t data_len,
550 ipmi_context_t context)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500551{
552 ipmi_ret_t rc = IPMI_CC_OK;
553 *data_len = 0;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500554 const uint8_t current_revision = 0x11; // Current rev per IPMI Spec 2.0
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500555
Patrick Venture0b02be92018-08-31 11:55:55 -0700556 get_lan_t* reqptr = (get_lan_t*)request;
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800557 // channel number is the lower nibble
558 int channel = reqptr->rev_channel & CHANNEL_MASK;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500559
560 if (reqptr->rev_channel & 0x80) // Revision is bit 7
561 {
562 // Only current revision was requested
563 *data_len = sizeof(current_revision);
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700564 std::memcpy(response, &current_revision, *data_len);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500565 return IPMI_CC_OK;
566 }
567
Tom Josepha30c8d32018-03-22 02:15:03 +0530568 static std::vector<uint8_t> cipherList;
569 static auto listInit = false;
570
571 if (!listInit)
572 {
573 try
574 {
575 cipherList = cipher::getCipherList();
576 listInit = true;
577 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700578 catch (const std::exception& e)
Tom Josepha30c8d32018-03-22 02:15:03 +0530579 {
580 return IPMI_CC_UNSPECIFIED_ERROR;
581 }
582 }
583
Johnathan Mantey74a21022018-12-13 13:17:56 -0800584 auto ethdevice = ipmi::getChannelName(channel);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800585 if (ethdevice.empty())
586 {
587 return IPMI_CC_INVALID_FIELD_REQUEST;
588 }
589 auto channelConf = getChannelConfig(channel);
590
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800591 LanParam param = static_cast<LanParam>(reqptr->parameter);
592 switch (param)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500593 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800594 case LanParam::INPROGRESS:
vishwa1eaea4f2016-02-26 11:57:40 -0600595 {
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800596 uint8_t buf[] = {current_revision,
597 channelConf->lan_set_in_progress};
598 *data_len = sizeof(buf);
599 std::memcpy(response, &buf, *data_len);
600 break;
601 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800602 case LanParam::AUTHSUPPORT:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800603 {
604 uint8_t buf[] = {current_revision, 0x04};
605 *data_len = sizeof(buf);
606 std::memcpy(response, &buf, *data_len);
607 break;
608 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800609 case LanParam::AUTHENABLES:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800610 {
611 uint8_t buf[] = {current_revision, 0x04, 0x04, 0x04, 0x04, 0x04};
612 *data_len = sizeof(buf);
613 std::memcpy(response, &buf, *data_len);
614 break;
615 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800616 case LanParam::IP:
617 case LanParam::SUBNET:
618 case LanParam::GATEWAY:
619 case LanParam::MAC:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800620 {
621 uint8_t buf[ipmi::network::MAC_ADDRESS_SIZE_BYTE + 1] = {};
622
623 *data_len = sizeof(current_revision);
624 std::memcpy(buf, &current_revision, *data_len);
625
626 if (getNetworkData(reqptr->parameter, &buf[1], channel) ==
627 IPMI_CC_OK)
Ratan Guptab8e99552017-07-27 07:07:48 +0530628 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800629 if (param == LanParam::MAC)
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800630 {
631 *data_len = sizeof(buf);
632 }
633 else
634 {
635 *data_len = ipmi::network::IPV4_ADDRESS_SIZE_BYTE + 1;
636 }
637 std::memcpy(response, &buf, *data_len);
Ratan Guptab8e99552017-07-27 07:07:48 +0530638 }
639 else
640 {
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800641 rc = IPMI_CC_UNSPECIFIED_ERROR;
Ratan Guptab8e99552017-07-27 07:07:48 +0530642 }
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800643 break;
Adriana Kobylak342df102016-02-10 13:48:16 -0600644 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800645 case LanParam::VLAN:
Hariharasubramanian R83951912016-01-20 07:06:36 -0600646 {
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800647 uint8_t buf[ipmi::network::VLAN_SIZE_BYTE + 1] = {};
Ratan Gupta533d03b2017-07-30 10:39:22 +0530648
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800649 *data_len = sizeof(current_revision);
650 std::memcpy(buf, &current_revision, *data_len);
651 if (getNetworkData(reqptr->parameter, &buf[1], channel) ==
652 IPMI_CC_OK)
653 {
654 *data_len = sizeof(buf);
655 std::memcpy(response, &buf, *data_len);
656 }
657 break;
Ratan Gupta533d03b2017-07-30 10:39:22 +0530658 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800659 case LanParam::IPSRC:
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530660 {
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800661 uint8_t buff[ipmi::network::IPSRC_SIZE_BYTE + 1] = {};
662 *data_len = sizeof(current_revision);
663 std::memcpy(buff, &current_revision, *data_len);
664 if (getNetworkData(reqptr->parameter, &buff[1], channel) ==
665 IPMI_CC_OK)
666 {
667 *data_len = sizeof(buff);
668 std::memcpy(response, &buff, *data_len);
669 }
670 break;
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530671 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800672 case LanParam::CIPHER_SUITE_COUNT:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800673 {
674 *(static_cast<uint8_t*>(response)) = current_revision;
675 // Byte 1 is reserved byte and does not indicate a cipher suite ID,
676 // so no of cipher suite entry count is one less than the size of
677 // the vector
678 auto count = static_cast<uint8_t>(cipherList.size() - 1);
679 *(static_cast<uint8_t*>(response) + 1) = count;
680 *data_len = sizeof(current_revision) + sizeof(count);
681 break;
682 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800683 case LanParam::CIPHER_SUITE_ENTRIES:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800684 {
685 *(static_cast<uint8_t*>(response)) = current_revision;
686 // Byte 1 is reserved
687 std::copy_n(cipherList.data(), cipherList.size(),
688 static_cast<uint8_t*>(response) + 1);
689 *data_len = sizeof(current_revision) +
690 static_cast<uint8_t>(cipherList.size());
691 break;
692 }
693 default:
694 log<level::ERR>("Unsupported parameter",
695 entry("PARAMETER=0x%x", reqptr->parameter));
696 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500697 }
698
699 return rc;
700}
701
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530702void applyChanges(int channel)
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530703{
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530704 std::string ipaddress;
705 std::string gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700706 uint8_t prefix{};
707 uint32_t vlanID{};
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530708 std::string networkInterfacePath;
709 ipmi::DbusObjectInfo ipObject;
710 ipmi::DbusObjectInfo systemObject;
711
Johnathan Mantey74a21022018-12-13 13:17:56 -0800712 auto ethdevice = ipmi::getChannelName(channel);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530713 if (ethdevice.empty())
714 {
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530715 log<level::ERR>("Unable to get the interface name",
716 entry("CHANNEL=%d", channel));
717 return;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530718 }
719 auto ethIp = ethdevice + "/" + ipmi::network::IP_TYPE;
720 auto channelConf = getChannelConfig(channel);
721
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530722 try
723 {
724 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
725
726 log<level::INFO>("Network data from Cache",
727 entry("PREFIX=%s", channelConf->netmask.c_str()),
728 entry("ADDRESS=%s", channelConf->ipaddr.c_str()),
729 entry("GATEWAY=%s", channelConf->gateway.c_str()),
730 entry("VLAN=%d", channelConf->vlanID),
731 entry("IPSRC=%d", channelConf->ipsrc));
732 if (channelConf->vlanID != ipmi::network::VLAN_ID_MASK)
733 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700734 // get the first twelve bits which is vlan id
735 // not interested in rest of the bits.
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530736 channelConf->vlanID = le32toh(channelConf->vlanID);
737 vlanID = channelConf->vlanID & ipmi::network::VLAN_ID_MASK;
738 }
739
740 // if the asked ip src is DHCP then not interested in
741 // any given data except vlan.
742 if (channelConf->ipsrc != ipmi::network::IPOrigin::DHCP)
743 {
744 // always get the system object
Patrick Venture0b02be92018-08-31 11:55:55 -0700745 systemObject =
746 ipmi::getDbusObject(bus, ipmi::network::SYSTEMCONFIG_INTERFACE,
747 ipmi::network::ROOT);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530748
749 // the below code is to determine the mode of the interface
750 // as the handling is same, if the system is configured with
751 // DHCP or user has given all the data.
752 try
753 {
754 ipmi::ObjectTree ancestorMap;
755
Patrick Venture0b02be92018-08-31 11:55:55 -0700756 ipmi::InterfaceList interfaces{
757 ipmi::network::ETHERNET_INTERFACE};
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530758
759 // if the system is having ip object,then
760 // get the IP object.
Patrick Venture0b02be92018-08-31 11:55:55 -0700761 ipObject = ipmi::getIPObject(bus, ipmi::network::IP_INTERFACE,
762 ipmi::network::ROOT, ethIp);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530763
764 // Get the parent interface of the IP object.
765 try
766 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700767 ancestorMap = ipmi::getAllAncestors(bus, ipObject.first,
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530768 std::move(interfaces));
769 }
770 catch (InternalFailure& e)
771 {
772 // if unable to get the parent interface
773 // then commit the error and return.
774 log<level::ERR>("Unable to get the parent interface",
775 entry("PATH=%s", ipObject.first.c_str()),
776 entry("INTERFACE=%s",
777 ipmi::network::ETHERNET_INTERFACE));
778 commit<InternalFailure>();
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530779 channelConf->clear();
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530780 return;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530781 }
782
783 networkInterfacePath = ancestorMap.begin()->first;
784 }
785 catch (InternalFailure& e)
786 {
787 // TODO Currently IPMI supports single interface,need to handle
788 // Multiple interface through
789 // https://github.com/openbmc/openbmc/issues/2138
790
791 // if there is no ip configured on the system,then
792 // get the network interface object.
Patrick Venture0b02be92018-08-31 11:55:55 -0700793 auto networkInterfaceObject =
794 ipmi::getDbusObject(bus, ipmi::network::ETHERNET_INTERFACE,
795 ipmi::network::ROOT, ethdevice);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530796
797 networkInterfacePath = std::move(networkInterfaceObject.first);
798 }
799
800 // get the configured mode on the system.
Vernon Maueryf442e112019-04-09 11:44:36 -0700801 auto enableDHCP = std::get<bool>(ipmi::getDbusProperty(
William A. Kennington III4c008022018-10-12 17:18:14 -0700802 bus, ipmi::network::SERVICE, networkInterfacePath,
803 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled"));
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530804
805 // if ip address source is not given then get the ip source mode
806 // from the system so that it can be applied later.
807 if (channelConf->ipsrc == ipmi::network::IPOrigin::UNSPECIFIED)
808 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700809 channelConf->ipsrc = (enableDHCP)
810 ? ipmi::network::IPOrigin::DHCP
811 : ipmi::network::IPOrigin::STATIC;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530812 }
813
814 // check whether user has given all the data
815 // or the configured system interface is dhcp enabled,
816 // in both of the cases get the values from the cache.
817 if ((!channelConf->ipaddr.empty() &&
818 !channelConf->netmask.empty() &&
819 !channelConf->gateway.empty()) ||
820 (enableDHCP)) // configured system interface mode = DHCP
821 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700822 // convert mask into prefix
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530823 ipaddress = channelConf->ipaddr;
824 prefix = ipmi::network::toPrefix(AF_INET, channelConf->netmask);
825 gateway = channelConf->gateway;
826 }
827 else // asked ip src = static and configured system src = static
Patrick Venture0b02be92018-08-31 11:55:55 -0700828 // or partially given data.
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530829 {
830 // We have partial filled cache so get the remaining
831 // info from the system.
832
833 // Get the network data from the system as user has
834 // not given all the data then use the data fetched from the
835 // system but it is implementation dependent,IPMI spec doesn't
836 // force it.
837
838 // if system is not having any ip object don't throw error,
839 try
840 {
841 auto properties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -0700842 bus, ipObject.second, ipObject.first,
843 ipmi::network::IP_INTERFACE);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530844
Vernon Maueryf442e112019-04-09 11:44:36 -0700845 ipaddress =
846 channelConf->ipaddr.empty()
847 ? std::get<std::string>(properties["Address"])
848 : channelConf->ipaddr;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530849
Patrick Venture0b02be92018-08-31 11:55:55 -0700850 prefix = channelConf->netmask.empty()
Vernon Maueryf442e112019-04-09 11:44:36 -0700851 ? std::get<uint8_t>(properties["PrefixLength"])
Patrick Venture0b02be92018-08-31 11:55:55 -0700852 : ipmi::network::toPrefix(
853 AF_INET, channelConf->netmask);
854 }
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530855 catch (InternalFailure& e)
856 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700857 log<level::INFO>(
858 "Failed to get IP object which matches",
859 entry("INTERFACE=%s", ipmi::network::IP_INTERFACE),
860 entry("MATCH=%s", ethIp.c_str()));
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530861 }
862
863 auto systemProperties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -0700864 bus, systemObject.second, systemObject.first,
865 ipmi::network::SYSTEMCONFIG_INTERFACE);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530866
William A. Kennington III4c008022018-10-12 17:18:14 -0700867 gateway = channelConf->gateway.empty()
Vernon Maueryf442e112019-04-09 11:44:36 -0700868 ? std::get<std::string>(
William A. Kennington III4c008022018-10-12 17:18:14 -0700869 systemProperties["DefaultGateway"])
870 : channelConf->gateway;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530871 }
872 }
873
874 // Currently network manager doesn't support purging of all the
875 // ip addresses and the vlan interfaces from the parent interface,
876 // TODO once the support is there, will make the change here.
877 // https://github.com/openbmc/openbmc/issues/2141.
878
879 // TODO Currently IPMI supports single interface,need to handle
880 // Multiple interface through
881 // https://github.com/openbmc/openbmc/issues/2138
882
883 // instead of deleting all the vlan interfaces and
884 // all the ipv4 address,we will call reset method.
Patrick Venture0b02be92018-08-31 11:55:55 -0700885 // delete all the vlan interfaces
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530886
Patrick Venture0b02be92018-08-31 11:55:55 -0700887 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530888 ipmi::network::VLAN_INTERFACE);
889
890 // set the interface mode to static
Patrick Venture0b02be92018-08-31 11:55:55 -0700891 auto networkInterfaceObject =
892 ipmi::getDbusObject(bus, ipmi::network::ETHERNET_INTERFACE,
893 ipmi::network::ROOT, ethdevice);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530894
895 // setting the physical interface mode to static.
Patrick Venture0b02be92018-08-31 11:55:55 -0700896 ipmi::setDbusProperty(
897 bus, ipmi::network::SERVICE, networkInterfaceObject.first,
898 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled", false);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530899
900 networkInterfacePath = networkInterfaceObject.first;
901
Patrick Venture0b02be92018-08-31 11:55:55 -0700902 // delete all the ipv4 addresses
903 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
904 ipmi::network::IP_INTERFACE, ethIp);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530905
906 if (vlanID)
907 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700908 ipmi::network::createVLAN(bus, ipmi::network::SERVICE,
909 ipmi::network::ROOT, ethdevice, vlanID);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530910
911 auto networkInterfaceObject = ipmi::getDbusObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700912 bus, ipmi::network::VLAN_INTERFACE, ipmi::network::ROOT);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530913
Patrick Venture0b02be92018-08-31 11:55:55 -0700914 networkInterfacePath = networkInterfaceObject.first;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530915 }
916
917 if (channelConf->ipsrc == ipmi::network::IPOrigin::DHCP)
918 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700919 ipmi::setDbusProperty(
920 bus, ipmi::network::SERVICE, networkInterfacePath,
921 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled", true);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530922 }
923 else
924 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700925 // change the mode to static
926 ipmi::setDbusProperty(
927 bus, ipmi::network::SERVICE, networkInterfacePath,
928 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled", false);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530929
930 if (!ipaddress.empty())
931 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700932 ipmi::network::createIP(bus, ipmi::network::SERVICE,
933 networkInterfacePath, ipv4Protocol,
934 ipaddress, prefix);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530935 }
936
937 if (!gateway.empty())
938 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700939 ipmi::setDbusProperty(bus, systemObject.second,
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530940 systemObject.first,
941 ipmi::network::SYSTEMCONFIG_INTERFACE,
Patrick Venture0b02be92018-08-31 11:55:55 -0700942 "DefaultGateway", std::string(gateway));
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530943 }
944 }
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530945 }
Vernon Mauery7a614182018-11-27 12:54:52 -0800946 catch (sdbusplus::exception::exception& e)
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530947 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700948 log<level::ERR>(
949 "Failed to set network data", entry("PREFIX=%d", prefix),
950 entry("ADDRESS=%s", ipaddress.c_str()),
951 entry("GATEWAY=%s", gateway.c_str()), entry("VLANID=%d", vlanID),
952 entry("IPSRC=%d", channelConf->ipsrc));
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530953
954 commit<InternalFailure>();
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530955 }
956
957 channelConf->clear();
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530958}
959
960void commitNetworkChanges()
961{
Patrick Venture0b02be92018-08-31 11:55:55 -0700962 for (const auto& channel : channelConfig)
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530963 {
964 if (channel.second->flush)
965 {
966 applyChanges(channel.first);
967 }
968 }
969}
970
971void createNetworkTimer()
972{
973 if (!networkTimer)
974 {
975 std::function<void()> networkTimerCallback(
Patrick Venture0b02be92018-08-31 11:55:55 -0700976 std::bind(&commitNetworkChanges));
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530977
Vernon Mauery1181af72018-10-08 12:05:00 -0700978 networkTimer = std::make_unique<phosphor::Timer>(networkTimerCallback);
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530979 }
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530980}
981
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500982void register_netfn_transport_functions()
983{
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530984 // As this timer is only for transport handler
985 // so creating it here.
986 createNetworkTimer();
Tom05732372016-09-06 17:21:23 +0530987 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -0700988 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_WILDCARD, NULL,
989 ipmi_transport_wildcard, PRIVILEGE_USER);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500990
Tom05732372016-09-06 17:21:23 +0530991 // <Set LAN Configuration Parameters>
Patrick Venture0b02be92018-08-31 11:55:55 -0700992 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_SET_LAN, NULL,
993 ipmi_transport_set_lan, PRIVILEGE_ADMIN);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500994
Tom05732372016-09-06 17:21:23 +0530995 // <Get LAN Configuration Parameters>
Patrick Venture0b02be92018-08-31 11:55:55 -0700996 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_GET_LAN, NULL,
997 ipmi_transport_get_lan, PRIVILEGE_OPERATOR);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500998
999 return;
1000}