blob: 8172cc4def2778acaf2535caa7529eed24a8ffdb [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"
4#include "ipmid.hpp"
Johnathan Mantey74a21022018-12-13 13:17:56 -08005#include "user_channel/channel_layer.hpp"
Patrick Venture3a5071a2018-09-12 13:27:42 -07006
Patrick Venture0b02be92018-08-31 11:55:55 -07007#include <arpa/inet.h>
William A. Kennington III194375f2018-12-14 02:14:33 -08008#include <ipmid/api.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07009
10#include <chrono>
Vernon Mauerybdda8002019-02-26 10:18:51 -080011#include <filesystem>
Patrick Venture0b02be92018-08-31 11:55:55 -070012#include <fstream>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070013#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070014#include <phosphor-logging/elog-errors.hpp>
15#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070016#include <sdbusplus/message/types.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070017#include <sdbusplus/timer.hpp>
tomjose26e17732016-03-03 08:52:51 -060018#include <string>
Patrick Venture3a5071a2018-09-12 13:27:42 -070019#include <xyz/openbmc_project/Common/error.hpp>
20
21#define SYSTEMD_NETWORKD_DBUS 1
22
23#ifdef SYSTEMD_NETWORKD_DBUS
24#include <mapper.h>
25#include <systemd/sd-bus.h>
26#endif
Patrick Venture586d35b2018-09-07 19:56:18 -070027
Vernon Mauery1b7f6f22019-03-13 13:11:25 -070028// timer for network changes
29std::unique_ptr<phosphor::Timer> networkTimer = nullptr;
Ratan Gupta1247e0b2018-03-07 10:47:25 +053030
Patrick Venture0b02be92018-08-31 11:55:55 -070031const int SIZE_MAC = 18; // xx:xx:xx:xx:xx:xx
Ratan Gupta1247e0b2018-03-07 10:47:25 +053032constexpr auto ipv4Protocol = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
Adriana Kobylake08fbc62016-02-09 16:17:23 -060033
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080034std::map<int, std::unique_ptr<struct ChannelConfig_t>> channelConfig;
Hariharasubramanian R83951912016-01-20 07:06:36 -060035
Ratan Guptab8e99552017-07-27 07:07:48 +053036using namespace phosphor::logging;
37using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta7a7f0122018-03-07 12:31:05 +053038
Vernon Mauery185b9f82018-07-20 10:52:36 -070039namespace fs = std::filesystem;
William A. Kennington III4c008022018-10-12 17:18:14 -070040namespace variant_ns = sdbusplus::message::variant_ns;
Hariharasubramanian R83951912016-01-20 07:06:36 -060041
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050042void register_netfn_transport_functions() __attribute__((constructor));
43
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080044struct ChannelConfig_t* getChannelConfig(int channel)
45{
46 auto item = channelConfig.find(channel);
47 if (item == channelConfig.end())
48 {
49 channelConfig[channel] = std::make_unique<struct ChannelConfig_t>();
50 }
51
52 return channelConfig[channel].get();
53}
54
Patrick Venture0b02be92018-08-31 11:55:55 -070055// Helper Function to get IP Address/NetMask/Gateway/MAC Address from Network
56// Manager or Cache based on Set-In-Progress State
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080057ipmi_ret_t getNetworkData(uint8_t lan_param, uint8_t* data, int channel)
tomjose26e17732016-03-03 08:52:51 -060058{
tomjose26e17732016-03-03 08:52:51 -060059 ipmi_ret_t rc = IPMI_CC_OK;
Ratan Guptab8e99552017-07-27 07:07:48 +053060 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Ratan Gupta533d03b2017-07-30 10:39:22 +053061
Johnathan Mantey74a21022018-12-13 13:17:56 -080062 auto ethdevice = ipmi::getChannelName(channel);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080063 // if ethdevice is an empty string they weren't expecting this channel.
64 if (ethdevice.empty())
65 {
66 // TODO: return error from getNetworkData()
67 return IPMI_CC_INVALID_FIELD_REQUEST;
68 }
69 auto ethIP = ethdevice + "/" + ipmi::network::IP_TYPE;
70 auto channelConf = getChannelConfig(channel);
71
Ratan Guptab8e99552017-07-27 07:07:48 +053072 try
tomjose26e17732016-03-03 08:52:51 -060073 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -080074 switch (static_cast<LanParam>(lan_param))
tomjose26e17732016-03-03 08:52:51 -060075 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -080076 case LanParam::IP:
Ratan Guptab8e99552017-07-27 07:07:48 +053077 {
78 std::string ipaddress;
Patrick Venturec7c1c3c2017-11-15 14:29:18 -080079 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptab8e99552017-07-27 07:07:48 +053080 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +053081 try
82 {
Patrick Venture0b02be92018-08-31 11:55:55 -070083 auto ipObjectInfo =
84 ipmi::getIPObject(bus, ipmi::network::IP_INTERFACE,
85 ipmi::network::ROOT, ethIP);
Ratan Guptadd646202017-11-21 17:46:59 +053086
87 auto properties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -070088 bus, ipObjectInfo.second, ipObjectInfo.first,
89 ipmi::network::IP_INTERFACE);
Ratan Guptadd646202017-11-21 17:46:59 +053090
William A. Kennington III4c008022018-10-12 17:18:14 -070091 ipaddress =
92 variant_ns::get<std::string>(properties["Address"]);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +053093 }
Gunnar Millsc9fa69e2018-04-08 16:35:25 -050094 // ignore the exception, as it is a valid condition that
95 // the system is not configured with any IP.
Ratan Guptacc6cdbf2017-09-01 23:06:25 +053096 catch (InternalFailure& e)
97 {
98 // nothing to do.
99 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530100 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800101 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptab8e99552017-07-27 07:07:48 +0530102 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800103 ipaddress = channelConf->ipaddr;
Ratan Guptab8e99552017-07-27 07:07:48 +0530104 }
105
106 inet_pton(AF_INET, ipaddress.c_str(),
107 reinterpret_cast<void*>(data));
108 }
109 break;
110
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800111 case LanParam::IPSRC:
Ratan Guptab8e99552017-07-27 07:07:48 +0530112 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530113 std::string networkInterfacePath;
114
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800115 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptab8e99552017-07-27 07:07:48 +0530116 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530117 try
118 {
119 ipmi::ObjectTree ancestorMap;
120 // if the system is having ip object,then
121 // get the IP object.
122 auto ipObject = ipmi::getDbusObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700123 bus, ipmi::network::IP_INTERFACE,
124 ipmi::network::ROOT, ethIP);
Ratan Guptab8e99552017-07-27 07:07:48 +0530125
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530126 // Get the parent interface of the IP object.
127 try
128 {
129 ipmi::InterfaceList interfaces;
130 interfaces.emplace_back(
Patrick Venture0b02be92018-08-31 11:55:55 -0700131 ipmi::network::ETHERNET_INTERFACE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530132
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530133 ancestorMap = ipmi::getAllAncestors(
Patrick Venture0b02be92018-08-31 11:55:55 -0700134 bus, ipObject.first, std::move(interfaces));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530135 }
136 catch (InternalFailure& e)
137 {
138 // if unable to get the parent interface
139 // then commit the error and return.
Patrick Venture0b02be92018-08-31 11:55:55 -0700140 log<level::ERR>(
141 "Unable to get the parent interface",
142 entry("PATH=%s", ipObject.first.c_str()),
143 entry("INTERFACE=%s",
144 ipmi::network::ETHERNET_INTERFACE));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530145 break;
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530146 }
147 // for an ip object there would be single parent
148 // interface.
149 networkInterfacePath = ancestorMap.begin()->first;
150 }
151 catch (InternalFailure& e)
152 {
153 // if there is no ip configured on the system,then
154 // get the network interface object.
155 auto networkInterfaceObject = ipmi::getDbusObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700156 bus, ipmi::network::ETHERNET_INTERFACE,
157 ipmi::network::ROOT, ethdevice);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530158
159 networkInterfacePath = networkInterfaceObject.first;
160 }
161
162 auto variant = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700163 bus, ipmi::network::SERVICE, networkInterfacePath,
164 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled");
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530165
William A. Kennington III4c008022018-10-12 17:18:14 -0700166 auto dhcpEnabled = variant_ns::get<bool>(variant);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530167 // As per IPMI spec 2=>DHCP, 1=STATIC
Patrick Venture0b02be92018-08-31 11:55:55 -0700168 auto ipsrc = dhcpEnabled ? ipmi::network::IPOrigin::DHCP
169 : ipmi::network::IPOrigin::STATIC;
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530170
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700171 std::memcpy(data, &ipsrc, ipmi::network::IPSRC_SIZE_BYTE);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530172 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800173 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530174 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700175 std::memcpy(data, &(channelConf->ipsrc),
176 ipmi::network::IPSRC_SIZE_BYTE);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530177 }
178 }
179 break;
180
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800181 case LanParam::SUBNET:
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530182 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700183 unsigned long mask{};
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800184 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530185 {
186 try
187 {
Johnathan Mantey88416832019-01-30 15:51:04 -0800188 auto ipObjectInfo =
189 ipmi::getIPObject(bus, ipmi::network::IP_INTERFACE,
190 ipmi::network::ROOT, ethIP);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530191
192 auto properties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -0700193 bus, ipObjectInfo.second, ipObjectInfo.first,
194 ipmi::network::IP_INTERFACE);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530195
William A. Kennington III4c008022018-10-12 17:18:14 -0700196 auto prefix = variant_ns::get<uint8_t>(
197 properties["PrefixLength"]);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530198 mask = ipmi::network::MASK_32_BIT;
199 mask = htonl(mask << (ipmi::network::BITS_32 - prefix));
200 }
Gunnar Millsc9fa69e2018-04-08 16:35:25 -0500201 // ignore the exception, as it is a valid condition that
202 // the system is not configured with any IP.
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530203 catch (InternalFailure& e)
204 {
205 // nothing to do
206 }
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700207 std::memcpy(data, &mask,
208 ipmi::network::IPV4_ADDRESS_SIZE_BYTE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530209 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800210 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptab8e99552017-07-27 07:07:48 +0530211 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800212 inet_pton(AF_INET, channelConf->netmask.c_str(),
Ratan Guptab8e99552017-07-27 07:07:48 +0530213 reinterpret_cast<void*>(data));
Ratan Guptab8e99552017-07-27 07:07:48 +0530214 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530215 }
216 break;
217
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800218 case LanParam::GATEWAY:
Ratan Guptab8e99552017-07-27 07:07:48 +0530219 {
220 std::string gateway;
221
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800222 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptab8e99552017-07-27 07:07:48 +0530223 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530224 try
225 {
226 auto systemObject = ipmi::getDbusObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700227 bus, ipmi::network::SYSTEMCONFIG_INTERFACE,
228 ipmi::network::ROOT);
Ratan Guptab8e99552017-07-27 07:07:48 +0530229
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530230 auto systemProperties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -0700231 bus, systemObject.second, systemObject.first,
232 ipmi::network::SYSTEMCONFIG_INTERFACE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530233
William A. Kennington III4c008022018-10-12 17:18:14 -0700234 gateway = variant_ns::get<std::string>(
235 systemProperties["DefaultGateway"]);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530236 }
Gunnar Millsc9fa69e2018-04-08 16:35:25 -0500237 // ignore the exception, as it is a valid condition that
238 // the system is not configured with any IP.
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530239 catch (InternalFailure& e)
240 {
241 // nothing to do
242 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530243 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800244 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptab8e99552017-07-27 07:07:48 +0530245 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800246 gateway = channelConf->gateway;
Ratan Guptab8e99552017-07-27 07:07:48 +0530247 }
248
249 inet_pton(AF_INET, gateway.c_str(),
250 reinterpret_cast<void*>(data));
Ratan Guptab8e99552017-07-27 07:07:48 +0530251 }
252 break;
253
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800254 case LanParam::MAC:
Ratan Guptab8e99552017-07-27 07:07:48 +0530255 {
256 std::string macAddress;
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800257 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Guptab8e99552017-07-27 07:07:48 +0530258 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700259 auto macObjectInfo =
260 ipmi::getDbusObject(bus, ipmi::network::MAC_INTERFACE,
261 ipmi::network::ROOT, ethdevice);
Ratan Guptab8e99552017-07-27 07:07:48 +0530262
263 auto variant = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700264 bus, macObjectInfo.second, macObjectInfo.first,
265 ipmi::network::MAC_INTERFACE, "MACAddress");
Ratan Guptab8e99552017-07-27 07:07:48 +0530266
William A. Kennington III4c008022018-10-12 17:18:14 -0700267 macAddress = variant_ns::get<std::string>(variant);
Ratan Guptab8e99552017-07-27 07:07:48 +0530268 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800269 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Guptab8e99552017-07-27 07:07:48 +0530270 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800271 macAddress = channelConf->macAddress;
Ratan Guptab8e99552017-07-27 07:07:48 +0530272 }
273
274 sscanf(macAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT,
Patrick Venture0b02be92018-08-31 11:55:55 -0700275 (data), (data + 1), (data + 2), (data + 3), (data + 4),
Ratan Guptab8e99552017-07-27 07:07:48 +0530276 (data + 5));
277 }
278 break;
279
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800280 case LanParam::VLAN:
Ratan Gupta533d03b2017-07-30 10:39:22 +0530281 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700282 uint16_t vlanID{};
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800283 if (channelConf->lan_set_in_progress == SET_COMPLETE)
Ratan Gupta533d03b2017-07-30 10:39:22 +0530284 {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530285 try
Ratan Gupta533d03b2017-07-30 10:39:22 +0530286 {
Ratan Guptadd646202017-11-21 17:46:59 +0530287 auto ipObjectInfo = ipmi::getIPObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700288 bus, ipmi::network::IP_INTERFACE,
289 ipmi::network::ROOT, ipmi::network::IP_TYPE);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530290
291 vlanID = static_cast<uint16_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700292 ipmi::network::getVLAN(ipObjectInfo.first));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530293
294 vlanID = htole16(vlanID);
295
296 if (vlanID)
297 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700298 // Enable the 16th bit
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530299 vlanID |= htole16(ipmi::network::VLAN_ENABLE_MASK);
300 }
301 }
Gunnar Millsc9fa69e2018-04-08 16:35:25 -0500302 // ignore the exception, as it is a valid condition that
303 // the system is not configured with any IP.
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530304 catch (InternalFailure& e)
305 {
306 // nothing to do
Ratan Gupta533d03b2017-07-30 10:39:22 +0530307 }
308
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700309 std::memcpy(data, &vlanID, ipmi::network::VLAN_SIZE_BYTE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530310 }
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800311 else if (channelConf->lan_set_in_progress == SET_IN_PROGRESS)
Ratan Gupta533d03b2017-07-30 10:39:22 +0530312 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700313 std::memcpy(data, &(channelConf->vlanID),
314 ipmi::network::VLAN_SIZE_BYTE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530315 }
316 }
317 break;
318
Ratan Guptab8e99552017-07-27 07:07:48 +0530319 default:
320 rc = IPMI_CC_PARM_OUT_OF_RANGE;
tomjose26e17732016-03-03 08:52:51 -0600321 }
322 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530323 catch (InternalFailure& e)
tomjose26e17732016-03-03 08:52:51 -0600324 {
Ratan Guptab8e99552017-07-27 07:07:48 +0530325 commit<InternalFailure>();
326 rc = IPMI_CC_UNSPECIFIED_ERROR;
327 return rc;
tomjose26e17732016-03-03 08:52:51 -0600328 }
tomjose26e17732016-03-03 08:52:51 -0600329 return rc;
330}
331
Tom Josepha30c8d32018-03-22 02:15:03 +0530332namespace cipher
333{
334
335std::vector<uint8_t> getCipherList()
336{
337 std::vector<uint8_t> cipherList;
338
339 std::ifstream jsonFile(configFile);
340 if (!jsonFile.is_open())
341 {
342 log<level::ERR>("Channel Cipher suites file not found");
343 elog<InternalFailure>();
344 }
345
346 auto data = Json::parse(jsonFile, nullptr, false);
347 if (data.is_discarded())
348 {
349 log<level::ERR>("Parsing channel cipher suites JSON failed");
350 elog<InternalFailure>();
351 }
352
353 // Byte 1 is reserved
354 cipherList.push_back(0x00);
355
356 for (const auto& record : data)
357 {
358 cipherList.push_back(record.value(cipher, 0));
359 }
360
361 return cipherList;
362}
363
Patrick Venture0b02be92018-08-31 11:55:55 -0700364} // namespace cipher
Tom Josepha30c8d32018-03-22 02:15:03 +0530365
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500366ipmi_ret_t ipmi_transport_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700367 ipmi_request_t request,
368 ipmi_response_t response,
369 ipmi_data_len_t data_len,
370 ipmi_context_t context)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500371{
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500372 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800373 ipmi_ret_t rc = IPMI_CC_INVALID;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500374 *data_len = 0;
375 return rc;
376}
377
Ratan Guptab8e99552017-07-27 07:07:48 +0530378struct set_lan_t
379{
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500380 uint8_t channel;
381 uint8_t parameter;
382 uint8_t data[8]; // Per IPMI spec, not expecting more than this size
Patrick Venture0b02be92018-08-31 11:55:55 -0700383} __attribute__((packed));
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500384
Patrick Venture0b02be92018-08-31 11:55:55 -0700385ipmi_ret_t ipmi_transport_set_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Ratan Guptab8e99552017-07-27 07:07:48 +0530386 ipmi_request_t request,
387 ipmi_response_t response,
388 ipmi_data_len_t data_len,
389 ipmi_context_t context)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500390{
391 ipmi_ret_t rc = IPMI_CC_OK;
392 *data_len = 0;
Nan Li3d0df912016-10-18 19:51:41 +0800393
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530394 using namespace std::chrono_literals;
395
396 // time to wait before applying the network changes.
397 constexpr auto networkTimeout = 10000000us; // 10 sec
398
Ratan Guptab8e99552017-07-27 07:07:48 +0530399 char ipaddr[INET_ADDRSTRLEN];
400 char netmask[INET_ADDRSTRLEN];
401 char gateway[INET_ADDRSTRLEN];
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500402
Ratan Guptab8e99552017-07-27 07:07:48 +0530403 auto reqptr = reinterpret_cast<const set_lan_t*>(request);
404 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500405
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800406 // channel number is the lower nibble
407 int channel = reqptr->channel & CHANNEL_MASK;
Johnathan Mantey74a21022018-12-13 13:17:56 -0800408 auto ethdevice = ipmi::getChannelName(channel);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800409 if (ethdevice.empty())
410 {
411 return IPMI_CC_INVALID_FIELD_REQUEST;
412 }
413 auto channelConf = getChannelConfig(channel);
414
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800415 switch (static_cast<LanParam>(reqptr->parameter))
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500416 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800417 case LanParam::IP:
Hariharasubramanian R83951912016-01-20 07:06:36 -0600418 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700419 std::snprintf(ipaddr, INET_ADDRSTRLEN,
420 ipmi::network::IP_ADDRESS_FORMAT, reqptr->data[0],
421 reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500422
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800423 channelConf->ipaddr.assign(ipaddr);
Ratan Guptab8e99552017-07-27 07:07:48 +0530424 }
425 break;
426
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800427 case LanParam::IPSRC:
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530428 {
429 uint8_t ipsrc{};
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700430 std::memcpy(&ipsrc, reqptr->data, ipmi::network::IPSRC_SIZE_BYTE);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800431 channelConf->ipsrc = static_cast<ipmi::network::IPOrigin>(ipsrc);
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530432 }
433 break;
434
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800435 case LanParam::MAC:
Ratan Guptab8e99552017-07-27 07:07:48 +0530436 {
437 char mac[SIZE_MAC];
438
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700439 std::snprintf(mac, SIZE_MAC, ipmi::network::MAC_ADDRESS_FORMAT,
440 reqptr->data[0], reqptr->data[1], reqptr->data[2],
441 reqptr->data[3], reqptr->data[4], reqptr->data[5]);
Ratan Guptab8e99552017-07-27 07:07:48 +0530442
Patrick Venture0b02be92018-08-31 11:55:55 -0700443 auto macObjectInfo =
444 ipmi::getDbusObject(bus, ipmi::network::MAC_INTERFACE,
445 ipmi::network::ROOT, ethdevice);
Ratan Guptab8e99552017-07-27 07:07:48 +0530446
Patrick Venture0b02be92018-08-31 11:55:55 -0700447 ipmi::setDbusProperty(
448 bus, macObjectInfo.second, macObjectInfo.first,
449 ipmi::network::MAC_INTERFACE, "MACAddress", std::string(mac));
Ratan Guptab8e99552017-07-27 07:07:48 +0530450
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800451 channelConf->macAddress = mac;
Ratan Guptab8e99552017-07-27 07:07:48 +0530452 }
453 break;
454
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800455 case LanParam::SUBNET:
Ratan Guptab8e99552017-07-27 07:07:48 +0530456 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700457 std::snprintf(netmask, INET_ADDRSTRLEN,
458 ipmi::network::IP_ADDRESS_FORMAT, reqptr->data[0],
459 reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800460 channelConf->netmask.assign(netmask);
Ratan Guptab8e99552017-07-27 07:07:48 +0530461 }
462 break;
463
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800464 case LanParam::GATEWAY:
Ratan Guptab8e99552017-07-27 07:07:48 +0530465 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700466 std::snprintf(gateway, INET_ADDRSTRLEN,
467 ipmi::network::IP_ADDRESS_FORMAT, reqptr->data[0],
468 reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800469 channelConf->gateway.assign(gateway);
Ratan Guptab8e99552017-07-27 07:07:48 +0530470 }
471 break;
472
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800473 case LanParam::VLAN:
Ratan Gupta533d03b2017-07-30 10:39:22 +0530474 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700475 uint16_t vlan{};
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700476 std::memcpy(&vlan, reqptr->data, ipmi::network::VLAN_SIZE_BYTE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530477 // We are not storing the enable bit
478 // We assume that ipmitool always send enable
479 // bit as 1.
480 vlan = le16toh(vlan);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800481 channelConf->vlanID = vlan;
Ratan Gupta533d03b2017-07-30 10:39:22 +0530482 }
483 break;
484
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800485 case LanParam::INPROGRESS:
Ratan Guptab8e99552017-07-27 07:07:48 +0530486 {
487 if (reqptr->data[0] == SET_COMPLETE)
488 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800489 channelConf->lan_set_in_progress = SET_COMPLETE;
Ratan Guptab8e99552017-07-27 07:07:48 +0530490
Patrick Venture0b02be92018-08-31 11:55:55 -0700491 log<level::INFO>(
492 "Network data from Cache",
493 entry("PREFIX=%s", channelConf->netmask.c_str()),
494 entry("ADDRESS=%s", channelConf->ipaddr.c_str()),
495 entry("GATEWAY=%s", channelConf->gateway.c_str()),
496 entry("VLAN=%d", channelConf->vlanID));
Ratan Guptab8e99552017-07-27 07:07:48 +0530497
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530498 if (!networkTimer)
499 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700500 log<level::ERR>("Network timer is not instantiated");
501 return IPMI_CC_UNSPECIFIED_ERROR;
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530502 }
503
504 // start/restart the timer
Vernon Mauery1181af72018-10-08 12:05:00 -0700505 networkTimer->start(networkTimeout);
Ratan Guptab8e99552017-07-27 07:07:48 +0530506 }
507 else if (reqptr->data[0] == SET_IN_PROGRESS) // Set In Progress
508 {
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800509 channelConf->lan_set_in_progress = SET_IN_PROGRESS;
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530510 channelConf->flush = true;
Ratan Guptab8e99552017-07-27 07:07:48 +0530511 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530512 }
513 break;
514
515 default:
516 {
Ratan Guptab8e99552017-07-27 07:07:48 +0530517 rc = IPMI_CC_PARM_NOT_SUPPORTED;
518 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530519 }
vishwa1eaea4f2016-02-26 11:57:40 -0600520
tomjose26e17732016-03-03 08:52:51 -0600521 return rc;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500522}
523
Ratan Guptab8e99552017-07-27 07:07:48 +0530524struct get_lan_t
525{
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500526 uint8_t rev_channel;
527 uint8_t parameter;
528 uint8_t parameter_set;
529 uint8_t parameter_block;
Patrick Venture0b02be92018-08-31 11:55:55 -0700530} __attribute__((packed));
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500531
Patrick Venture0b02be92018-08-31 11:55:55 -0700532ipmi_ret_t ipmi_transport_get_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Ratan Guptab8e99552017-07-27 07:07:48 +0530533 ipmi_request_t request,
534 ipmi_response_t response,
535 ipmi_data_len_t data_len,
536 ipmi_context_t context)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500537{
538 ipmi_ret_t rc = IPMI_CC_OK;
539 *data_len = 0;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500540 const uint8_t current_revision = 0x11; // Current rev per IPMI Spec 2.0
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500541
Patrick Venture0b02be92018-08-31 11:55:55 -0700542 get_lan_t* reqptr = (get_lan_t*)request;
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800543 // channel number is the lower nibble
544 int channel = reqptr->rev_channel & CHANNEL_MASK;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500545
546 if (reqptr->rev_channel & 0x80) // Revision is bit 7
547 {
548 // Only current revision was requested
549 *data_len = sizeof(current_revision);
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700550 std::memcpy(response, &current_revision, *data_len);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500551 return IPMI_CC_OK;
552 }
553
Tom Josepha30c8d32018-03-22 02:15:03 +0530554 static std::vector<uint8_t> cipherList;
555 static auto listInit = false;
556
557 if (!listInit)
558 {
559 try
560 {
561 cipherList = cipher::getCipherList();
562 listInit = true;
563 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700564 catch (const std::exception& e)
Tom Josepha30c8d32018-03-22 02:15:03 +0530565 {
566 return IPMI_CC_UNSPECIFIED_ERROR;
567 }
568 }
569
Johnathan Mantey74a21022018-12-13 13:17:56 -0800570 auto ethdevice = ipmi::getChannelName(channel);
Patrick Venturec7c1c3c2017-11-15 14:29:18 -0800571 if (ethdevice.empty())
572 {
573 return IPMI_CC_INVALID_FIELD_REQUEST;
574 }
575 auto channelConf = getChannelConfig(channel);
576
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800577 LanParam param = static_cast<LanParam>(reqptr->parameter);
578 switch (param)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500579 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800580 case LanParam::INPROGRESS:
vishwa1eaea4f2016-02-26 11:57:40 -0600581 {
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800582 uint8_t buf[] = {current_revision,
583 channelConf->lan_set_in_progress};
584 *data_len = sizeof(buf);
585 std::memcpy(response, &buf, *data_len);
586 break;
587 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800588 case LanParam::AUTHSUPPORT:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800589 {
590 uint8_t buf[] = {current_revision, 0x04};
591 *data_len = sizeof(buf);
592 std::memcpy(response, &buf, *data_len);
593 break;
594 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800595 case LanParam::AUTHENABLES:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800596 {
597 uint8_t buf[] = {current_revision, 0x04, 0x04, 0x04, 0x04, 0x04};
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::IP:
603 case LanParam::SUBNET:
604 case LanParam::GATEWAY:
605 case LanParam::MAC:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800606 {
607 uint8_t buf[ipmi::network::MAC_ADDRESS_SIZE_BYTE + 1] = {};
608
609 *data_len = sizeof(current_revision);
610 std::memcpy(buf, &current_revision, *data_len);
611
612 if (getNetworkData(reqptr->parameter, &buf[1], channel) ==
613 IPMI_CC_OK)
Ratan Guptab8e99552017-07-27 07:07:48 +0530614 {
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800615 if (param == LanParam::MAC)
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800616 {
617 *data_len = sizeof(buf);
618 }
619 else
620 {
621 *data_len = ipmi::network::IPV4_ADDRESS_SIZE_BYTE + 1;
622 }
623 std::memcpy(response, &buf, *data_len);
Ratan Guptab8e99552017-07-27 07:07:48 +0530624 }
625 else
626 {
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800627 rc = IPMI_CC_UNSPECIFIED_ERROR;
Ratan Guptab8e99552017-07-27 07:07:48 +0530628 }
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800629 break;
Adriana Kobylak342df102016-02-10 13:48:16 -0600630 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800631 case LanParam::VLAN:
Hariharasubramanian R83951912016-01-20 07:06:36 -0600632 {
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800633 uint8_t buf[ipmi::network::VLAN_SIZE_BYTE + 1] = {};
Ratan Gupta533d03b2017-07-30 10:39:22 +0530634
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800635 *data_len = sizeof(current_revision);
636 std::memcpy(buf, &current_revision, *data_len);
637 if (getNetworkData(reqptr->parameter, &buf[1], channel) ==
638 IPMI_CC_OK)
639 {
640 *data_len = sizeof(buf);
641 std::memcpy(response, &buf, *data_len);
642 }
643 break;
Ratan Gupta533d03b2017-07-30 10:39:22 +0530644 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800645 case LanParam::IPSRC:
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530646 {
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800647 uint8_t buff[ipmi::network::IPSRC_SIZE_BYTE + 1] = {};
648 *data_len = sizeof(current_revision);
649 std::memcpy(buff, &current_revision, *data_len);
650 if (getNetworkData(reqptr->parameter, &buff[1], channel) ==
651 IPMI_CC_OK)
652 {
653 *data_len = sizeof(buff);
654 std::memcpy(response, &buff, *data_len);
655 }
656 break;
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530657 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800658 case LanParam::CIPHER_SUITE_COUNT:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800659 {
660 *(static_cast<uint8_t*>(response)) = current_revision;
661 // Byte 1 is reserved byte and does not indicate a cipher suite ID,
662 // so no of cipher suite entry count is one less than the size of
663 // the vector
664 auto count = static_cast<uint8_t>(cipherList.size() - 1);
665 *(static_cast<uint8_t*>(response) + 1) = count;
666 *data_len = sizeof(current_revision) + sizeof(count);
667 break;
668 }
William A. Kennington IIIaab20232018-11-19 18:20:39 -0800669 case LanParam::CIPHER_SUITE_ENTRIES:
William A. Kennington III39f94ef2018-11-19 22:36:16 -0800670 {
671 *(static_cast<uint8_t*>(response)) = current_revision;
672 // Byte 1 is reserved
673 std::copy_n(cipherList.data(), cipherList.size(),
674 static_cast<uint8_t*>(response) + 1);
675 *data_len = sizeof(current_revision) +
676 static_cast<uint8_t>(cipherList.size());
677 break;
678 }
679 default:
680 log<level::ERR>("Unsupported parameter",
681 entry("PARAMETER=0x%x", reqptr->parameter));
682 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500683 }
684
685 return rc;
686}
687
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530688void applyChanges(int channel)
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530689{
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530690 std::string ipaddress;
691 std::string gateway;
Patrick Venture0b02be92018-08-31 11:55:55 -0700692 uint8_t prefix{};
693 uint32_t vlanID{};
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530694 std::string networkInterfacePath;
695 ipmi::DbusObjectInfo ipObject;
696 ipmi::DbusObjectInfo systemObject;
697
Johnathan Mantey74a21022018-12-13 13:17:56 -0800698 auto ethdevice = ipmi::getChannelName(channel);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530699 if (ethdevice.empty())
700 {
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530701 log<level::ERR>("Unable to get the interface name",
702 entry("CHANNEL=%d", channel));
703 return;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530704 }
705 auto ethIp = ethdevice + "/" + ipmi::network::IP_TYPE;
706 auto channelConf = getChannelConfig(channel);
707
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530708 try
709 {
710 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
711
712 log<level::INFO>("Network data from Cache",
713 entry("PREFIX=%s", channelConf->netmask.c_str()),
714 entry("ADDRESS=%s", channelConf->ipaddr.c_str()),
715 entry("GATEWAY=%s", channelConf->gateway.c_str()),
716 entry("VLAN=%d", channelConf->vlanID),
717 entry("IPSRC=%d", channelConf->ipsrc));
718 if (channelConf->vlanID != ipmi::network::VLAN_ID_MASK)
719 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700720 // get the first twelve bits which is vlan id
721 // not interested in rest of the bits.
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530722 channelConf->vlanID = le32toh(channelConf->vlanID);
723 vlanID = channelConf->vlanID & ipmi::network::VLAN_ID_MASK;
724 }
725
726 // if the asked ip src is DHCP then not interested in
727 // any given data except vlan.
728 if (channelConf->ipsrc != ipmi::network::IPOrigin::DHCP)
729 {
730 // always get the system object
Patrick Venture0b02be92018-08-31 11:55:55 -0700731 systemObject =
732 ipmi::getDbusObject(bus, ipmi::network::SYSTEMCONFIG_INTERFACE,
733 ipmi::network::ROOT);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530734
735 // the below code is to determine the mode of the interface
736 // as the handling is same, if the system is configured with
737 // DHCP or user has given all the data.
738 try
739 {
740 ipmi::ObjectTree ancestorMap;
741
Patrick Venture0b02be92018-08-31 11:55:55 -0700742 ipmi::InterfaceList interfaces{
743 ipmi::network::ETHERNET_INTERFACE};
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530744
745 // if the system is having ip object,then
746 // get the IP object.
Patrick Venture0b02be92018-08-31 11:55:55 -0700747 ipObject = ipmi::getIPObject(bus, ipmi::network::IP_INTERFACE,
748 ipmi::network::ROOT, ethIp);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530749
750 // Get the parent interface of the IP object.
751 try
752 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700753 ancestorMap = ipmi::getAllAncestors(bus, ipObject.first,
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530754 std::move(interfaces));
755 }
756 catch (InternalFailure& e)
757 {
758 // if unable to get the parent interface
759 // then commit the error and return.
760 log<level::ERR>("Unable to get the parent interface",
761 entry("PATH=%s", ipObject.first.c_str()),
762 entry("INTERFACE=%s",
763 ipmi::network::ETHERNET_INTERFACE));
764 commit<InternalFailure>();
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530765 channelConf->clear();
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530766 return;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530767 }
768
769 networkInterfacePath = ancestorMap.begin()->first;
770 }
771 catch (InternalFailure& e)
772 {
773 // TODO Currently IPMI supports single interface,need to handle
774 // Multiple interface through
775 // https://github.com/openbmc/openbmc/issues/2138
776
777 // if there is no ip configured on the system,then
778 // get the network interface object.
Patrick Venture0b02be92018-08-31 11:55:55 -0700779 auto networkInterfaceObject =
780 ipmi::getDbusObject(bus, ipmi::network::ETHERNET_INTERFACE,
781 ipmi::network::ROOT, ethdevice);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530782
783 networkInterfacePath = std::move(networkInterfaceObject.first);
784 }
785
786 // get the configured mode on the system.
William A. Kennington III4c008022018-10-12 17:18:14 -0700787 auto enableDHCP = variant_ns::get<bool>(ipmi::getDbusProperty(
788 bus, ipmi::network::SERVICE, networkInterfacePath,
789 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled"));
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530790
791 // if ip address source is not given then get the ip source mode
792 // from the system so that it can be applied later.
793 if (channelConf->ipsrc == ipmi::network::IPOrigin::UNSPECIFIED)
794 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700795 channelConf->ipsrc = (enableDHCP)
796 ? ipmi::network::IPOrigin::DHCP
797 : ipmi::network::IPOrigin::STATIC;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530798 }
799
800 // check whether user has given all the data
801 // or the configured system interface is dhcp enabled,
802 // in both of the cases get the values from the cache.
803 if ((!channelConf->ipaddr.empty() &&
804 !channelConf->netmask.empty() &&
805 !channelConf->gateway.empty()) ||
806 (enableDHCP)) // configured system interface mode = DHCP
807 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700808 // convert mask into prefix
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530809 ipaddress = channelConf->ipaddr;
810 prefix = ipmi::network::toPrefix(AF_INET, channelConf->netmask);
811 gateway = channelConf->gateway;
812 }
813 else // asked ip src = static and configured system src = static
Patrick Venture0b02be92018-08-31 11:55:55 -0700814 // or partially given data.
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530815 {
816 // We have partial filled cache so get the remaining
817 // info from the system.
818
819 // Get the network data from the system as user has
820 // not given all the data then use the data fetched from the
821 // system but it is implementation dependent,IPMI spec doesn't
822 // force it.
823
824 // if system is not having any ip object don't throw error,
825 try
826 {
827 auto properties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -0700828 bus, ipObject.second, ipObject.first,
829 ipmi::network::IP_INTERFACE);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530830
Patrick Venture0b02be92018-08-31 11:55:55 -0700831 ipaddress = channelConf->ipaddr.empty()
William A. Kennington III4c008022018-10-12 17:18:14 -0700832 ? variant_ns::get<std::string>(
833 properties["Address"])
Patrick Venture0b02be92018-08-31 11:55:55 -0700834 : channelConf->ipaddr;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530835
Patrick Venture0b02be92018-08-31 11:55:55 -0700836 prefix = channelConf->netmask.empty()
William A. Kennington III4c008022018-10-12 17:18:14 -0700837 ? variant_ns::get<uint8_t>(
838 properties["PrefixLength"])
Patrick Venture0b02be92018-08-31 11:55:55 -0700839 : ipmi::network::toPrefix(
840 AF_INET, channelConf->netmask);
841 }
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530842 catch (InternalFailure& e)
843 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700844 log<level::INFO>(
845 "Failed to get IP object which matches",
846 entry("INTERFACE=%s", ipmi::network::IP_INTERFACE),
847 entry("MATCH=%s", ethIp.c_str()));
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530848 }
849
850 auto systemProperties = ipmi::getAllDbusProperties(
Patrick Venture0b02be92018-08-31 11:55:55 -0700851 bus, systemObject.second, systemObject.first,
852 ipmi::network::SYSTEMCONFIG_INTERFACE);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530853
William A. Kennington III4c008022018-10-12 17:18:14 -0700854 gateway = channelConf->gateway.empty()
855 ? variant_ns::get<std::string>(
856 systemProperties["DefaultGateway"])
857 : channelConf->gateway;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530858 }
859 }
860
861 // Currently network manager doesn't support purging of all the
862 // ip addresses and the vlan interfaces from the parent interface,
863 // TODO once the support is there, will make the change here.
864 // https://github.com/openbmc/openbmc/issues/2141.
865
866 // TODO Currently IPMI supports single interface,need to handle
867 // Multiple interface through
868 // https://github.com/openbmc/openbmc/issues/2138
869
870 // instead of deleting all the vlan interfaces and
871 // all the ipv4 address,we will call reset method.
Patrick Venture0b02be92018-08-31 11:55:55 -0700872 // delete all the vlan interfaces
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530873
Patrick Venture0b02be92018-08-31 11:55:55 -0700874 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530875 ipmi::network::VLAN_INTERFACE);
876
877 // set the interface mode to static
Patrick Venture0b02be92018-08-31 11:55:55 -0700878 auto networkInterfaceObject =
879 ipmi::getDbusObject(bus, ipmi::network::ETHERNET_INTERFACE,
880 ipmi::network::ROOT, ethdevice);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530881
882 // setting the physical interface mode to static.
Patrick Venture0b02be92018-08-31 11:55:55 -0700883 ipmi::setDbusProperty(
884 bus, ipmi::network::SERVICE, networkInterfaceObject.first,
885 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled", false);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530886
887 networkInterfacePath = networkInterfaceObject.first;
888
Patrick Venture0b02be92018-08-31 11:55:55 -0700889 // delete all the ipv4 addresses
890 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
891 ipmi::network::IP_INTERFACE, ethIp);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530892
893 if (vlanID)
894 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700895 ipmi::network::createVLAN(bus, ipmi::network::SERVICE,
896 ipmi::network::ROOT, ethdevice, vlanID);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530897
898 auto networkInterfaceObject = ipmi::getDbusObject(
Patrick Venture0b02be92018-08-31 11:55:55 -0700899 bus, ipmi::network::VLAN_INTERFACE, ipmi::network::ROOT);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530900
Patrick Venture0b02be92018-08-31 11:55:55 -0700901 networkInterfacePath = networkInterfaceObject.first;
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530902 }
903
904 if (channelConf->ipsrc == ipmi::network::IPOrigin::DHCP)
905 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700906 ipmi::setDbusProperty(
907 bus, ipmi::network::SERVICE, networkInterfacePath,
908 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled", true);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530909 }
910 else
911 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700912 // change the mode to static
913 ipmi::setDbusProperty(
914 bus, ipmi::network::SERVICE, networkInterfacePath,
915 ipmi::network::ETHERNET_INTERFACE, "DHCPEnabled", false);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530916
917 if (!ipaddress.empty())
918 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700919 ipmi::network::createIP(bus, ipmi::network::SERVICE,
920 networkInterfacePath, ipv4Protocol,
921 ipaddress, prefix);
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530922 }
923
924 if (!gateway.empty())
925 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700926 ipmi::setDbusProperty(bus, systemObject.second,
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530927 systemObject.first,
928 ipmi::network::SYSTEMCONFIG_INTERFACE,
Patrick Venture0b02be92018-08-31 11:55:55 -0700929 "DefaultGateway", std::string(gateway));
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530930 }
931 }
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530932 }
933 catch (InternalFailure& e)
934 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700935 log<level::ERR>(
936 "Failed to set network data", entry("PREFIX=%d", prefix),
937 entry("ADDRESS=%s", ipaddress.c_str()),
938 entry("GATEWAY=%s", gateway.c_str()), entry("VLANID=%d", vlanID),
939 entry("IPSRC=%d", channelConf->ipsrc));
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530940
941 commit<InternalFailure>();
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530942 }
943
944 channelConf->clear();
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530945}
946
947void commitNetworkChanges()
948{
Patrick Venture0b02be92018-08-31 11:55:55 -0700949 for (const auto& channel : channelConfig)
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530950 {
951 if (channel.second->flush)
952 {
953 applyChanges(channel.first);
954 }
955 }
956}
957
958void createNetworkTimer()
959{
960 if (!networkTimer)
961 {
962 std::function<void()> networkTimerCallback(
Patrick Venture0b02be92018-08-31 11:55:55 -0700963 std::bind(&commitNetworkChanges));
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530964
Vernon Mauery1181af72018-10-08 12:05:00 -0700965 networkTimer = std::make_unique<phosphor::Timer>(networkTimerCallback);
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530966 }
Ratan Gupta1247e0b2018-03-07 10:47:25 +0530967}
968
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500969void register_netfn_transport_functions()
970{
Ratan Gupta7a7f0122018-03-07 12:31:05 +0530971 // As this timer is only for transport handler
972 // so creating it here.
973 createNetworkTimer();
Tom05732372016-09-06 17:21:23 +0530974 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -0700975 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_WILDCARD, NULL,
976 ipmi_transport_wildcard, PRIVILEGE_USER);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500977
Tom05732372016-09-06 17:21:23 +0530978 // <Set LAN Configuration Parameters>
Patrick Venture0b02be92018-08-31 11:55:55 -0700979 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_SET_LAN, NULL,
980 ipmi_transport_set_lan, PRIVILEGE_ADMIN);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500981
Tom05732372016-09-06 17:21:23 +0530982 // <Get LAN Configuration Parameters>
Patrick Venture0b02be92018-08-31 11:55:55 -0700983 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_GET_LAN, NULL,
984 ipmi_transport_get_lan, PRIVILEGE_OPERATOR);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500985
986 return;
987}