blob: 310ac2b1a416e1d99716397fd89a8c5f5f9fdfa9 [file] [log] [blame]
AppaRao Puli071f3f22018-05-24 16:45:30 +05301/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#include "channel_mgmt.hpp"
18
19#include "apphandler.hpp"
20
21#include <sys/stat.h>
22#include <unistd.h>
23
24#include <boost/interprocess/sync/scoped_lock.hpp>
25#include <cerrno>
AppaRao Puli9613ed72018-09-01 23:46:44 +053026#include <exception>
AppaRao Puli071f3f22018-05-24 16:45:30 +053027#include <experimental/filesystem>
28#include <fstream>
29#include <phosphor-logging/log.hpp>
AppaRao Puli9613ed72018-09-01 23:46:44 +053030#include <sdbusplus/bus/match.hpp>
31#include <sdbusplus/server/object.hpp>
AppaRao Puli071f3f22018-05-24 16:45:30 +053032#include <unordered_map>
33
34namespace ipmi
35{
36
William A. Kennington IIIdfad4862018-11-19 17:45:35 -080037namespace variant_ns = sdbusplus::message::variant_ns;
AppaRao Puli071f3f22018-05-24 16:45:30 +053038using namespace phosphor::logging;
39
40static constexpr const char* channelAccessDefaultFilename =
41 "/usr/share/ipmi-providers/channel_access.json";
42static constexpr const char* channelConfigDefaultFilename =
43 "/usr/share/ipmi-providers/channel_config.json";
44static constexpr const char* channelNvDataFilename =
45 "/var/lib/ipmi/channel_access_nv.json";
46static constexpr const char* channelVolatileDataFilename =
47 "/run/ipmi/channel_access_volatile.json";
48
AppaRao Puli9613ed72018-09-01 23:46:44 +053049// TODO: Get the service name dynamically..
50static constexpr const char* networkIntfServiceName =
51 "xyz.openbmc_project.Network";
52static constexpr const char* networkIntfObjectBasePath =
53 "/xyz/openbmc_project/network";
54static constexpr const char* networkChConfigIntfName =
55 "xyz.openbmc_project.Channel.ChannelAccess";
56static constexpr const char* privilegePropertyString = "MaxPrivilege";
57static constexpr const char* dBusPropertiesInterface =
58 "org.freedesktop.DBus.Properties";
59static constexpr const char* propertiesChangedSignal = "PropertiesChanged";
60
AppaRao Puli071f3f22018-05-24 16:45:30 +053061// STRING DEFINES: Should sync with key's in JSON
62static constexpr const char* nameString = "name";
63static constexpr const char* isValidString = "is_valid";
64static constexpr const char* activeSessionsString = "active_sessions";
Vernon Mauery58317122018-11-28 11:02:43 -080065static constexpr const char* maxTransferSizeString = "max_transfer_size";
AppaRao Puli071f3f22018-05-24 16:45:30 +053066static constexpr const char* channelInfoString = "channel_info";
67static constexpr const char* mediumTypeString = "medium_type";
68static constexpr const char* protocolTypeString = "protocol_type";
69static constexpr const char* sessionSupportedString = "session_supported";
70static constexpr const char* isIpmiString = "is_ipmi";
71static constexpr const char* authTypeSupportedString = "auth_type_supported";
72static constexpr const char* accessModeString = "access_mode";
73static constexpr const char* userAuthDisabledString = "user_auth_disabled";
74static constexpr const char* perMsgAuthDisabledString = "per_msg_auth_disabled";
75static constexpr const char* alertingDisabledString = "alerting_disabled";
76static constexpr const char* privLimitString = "priv_limit";
77static constexpr const char* authTypeEnabledString = "auth_type_enabled";
78
79// Default values
80static constexpr const char* defaultChannelName = "RESERVED";
81static constexpr const uint8_t defaultMediumType =
82 static_cast<uint8_t>(EChannelMediumType::reserved);
83static constexpr const uint8_t defaultProtocolType =
84 static_cast<uint8_t>(EChannelProtocolType::reserved);
85static constexpr const uint8_t defaultSessionSupported =
86 static_cast<uint8_t>(EChannelSessSupported::none);
87static constexpr const uint8_t defaultAuthType =
88 static_cast<uint8_t>(EAuthType::none);
89static constexpr const bool defaultIsIpmiState = false;
Vernon Mauery58317122018-11-28 11:02:43 -080090static constexpr size_t smallChannelSize = 64;
AppaRao Puli071f3f22018-05-24 16:45:30 +053091
AppaRao Puli9613ed72018-09-01 23:46:44 +053092std::unique_ptr<sdbusplus::bus::match_t> chPropertiesSignal(nullptr);
93
AppaRao Puli071f3f22018-05-24 16:45:30 +053094// String mappings use in JSON config file
95static std::unordered_map<std::string, EChannelMediumType> mediumTypeMap = {
96 {"reserved", EChannelMediumType::reserved},
97 {"ipmb", EChannelMediumType::ipmb},
98 {"icmb-v1.0", EChannelMediumType::icmbV10},
99 {"icmb-v0.9", EChannelMediumType::icmbV09},
100 {"lan-802.3", EChannelMediumType::lan8032},
101 {"serial", EChannelMediumType::serial},
102 {"other-lan", EChannelMediumType::otherLan},
103 {"pci-smbus", EChannelMediumType::pciSmbus},
104 {"smbus-v1.0", EChannelMediumType::smbusV11},
105 {"smbus-v2.0", EChannelMediumType::smbusV20},
106 {"usb-1x", EChannelMediumType::usbV1x},
107 {"usb-2x", EChannelMediumType::usbV2x},
108 {"system-interface", EChannelMediumType::systemInterface},
109 {"oem", EChannelMediumType::oem},
110 {"unknown", EChannelMediumType::unknown}};
111
ssekarf4b2b092018-07-25 18:49:08 +0530112static std::unordered_map<EInterfaceIndex, std::string> interfaceMap = {
Richard Marian Thomaiyar43cb1282018-12-08 17:22:53 +0530113 {interfaceKCS, "SMS"},
ssekarf4b2b092018-07-25 18:49:08 +0530114 {interfaceLAN1, "LAN1"},
115 {interfaceUnknown, "unknown"}};
116
AppaRao Puli071f3f22018-05-24 16:45:30 +0530117static std::unordered_map<std::string, EChannelProtocolType> protocolTypeMap = {
118 {"na", EChannelProtocolType::na},
119 {"ipmb-1.0", EChannelProtocolType::ipmbV10},
120 {"icmb-2.0", EChannelProtocolType::icmbV11},
121 {"reserved", EChannelProtocolType::reserved},
122 {"ipmi-smbus", EChannelProtocolType::ipmiSmbus},
123 {"kcs", EChannelProtocolType::kcs},
124 {"smic", EChannelProtocolType::smic},
125 {"bt-10", EChannelProtocolType::bt10},
126 {"bt-15", EChannelProtocolType::bt15},
127 {"tmode", EChannelProtocolType::tMode},
128 {"oem", EChannelProtocolType::oem}};
129
130static std::array<std::string, 4> accessModeList = {
131 "disabled", "pre-boot", "always_available", "shared"};
132
133static std::array<std::string, 4> sessionSupportList = {
134 "session-less", "single-session", "multi-session", "session-based"};
135
136static std::array<std::string, PRIVILEGE_OEM + 1> privList = {
137 "priv-reserved", "priv-callback", "priv-user",
138 "priv-operator", "priv-admin", "priv-oem"};
139
AppaRao Puli9613ed72018-09-01 23:46:44 +0530140static constexpr const char* LAN1_STR = "LAN1";
141static constexpr const char* LAN2_STR = "LAN2";
142static constexpr const char* LAN3_STR = "LAN3";
143static constexpr const char* ETH0_STR = "eth0";
144static constexpr const char* ETH1_STR = "eth1";
145static constexpr const char* ETH2_STR = "eth2";
146
147static std::unordered_map<std::string, std::string> channelToInterfaceMap = {
148 {LAN1_STR, ETH0_STR}, {LAN2_STR, ETH1_STR}, {LAN3_STR, ETH2_STR}};
149
150static std::unordered_map<std::string, std::string> interfaceToChannelMap = {
151 {ETH0_STR, LAN1_STR}, {ETH1_STR, LAN2_STR}, {ETH2_STR, LAN3_STR}};
152
153std::string convertToChannelName(const std::string& intfName)
154{
155
156 auto it = interfaceToChannelMap.find(intfName);
157 if (it == interfaceToChannelMap.end())
158 {
159 log<level::ERR>("Invalid network interface.",
160 entry("INTF:%s", intfName.c_str()));
161 throw std::invalid_argument("Invalid network interface");
162 }
163
164 return it->second;
165}
166
167std::string getNetIntfFromPath(const std::string& path)
168{
169 std::size_t pos = path.find(networkIntfObjectBasePath);
170 if (pos == std::string::npos)
171 {
172 log<level::ERR>("Invalid interface path.",
173 entry("PATH:%s", path.c_str()));
174 throw std::invalid_argument("Invalid interface path");
175 }
176 std::string intfName =
177 path.substr(pos + strlen(networkIntfObjectBasePath) + 1);
178 return intfName;
179}
180
181void processChAccessPropChange(ChannelConfig& chConfig, const std::string& path,
182 const DbusChObjProperties& chProperties)
183{
184 // Get interface name from path. ex: '/xyz/openbmc_project/network/eth0'
185 std::string intfName;
186 try
187 {
188 intfName = getNetIntfFromPath(path);
189 }
190 catch (const std::invalid_argument& e)
191 {
192 log<level::ERR>("Exception: ", entry("MSG: %s", e.what()));
193 return;
194 }
195
196 // Get the MaxPrivilege property value from the signal
197 std::string intfPrivStr;
198 std::string propName;
199 for (const auto& prop : chProperties)
200 {
201 if (prop.first == privilegePropertyString)
202 {
203 propName = privilegePropertyString;
William A. Kennington IIIdfad4862018-11-19 17:45:35 -0800204 intfPrivStr = variant_ns::get<std::string>(prop.second);
AppaRao Puli9613ed72018-09-01 23:46:44 +0530205 break;
206 }
207 }
208
209 if (propName != privilegePropertyString)
210 {
211 log<level::ERR>("Unknown signal caught.");
212 return;
213 }
214
215 if (intfPrivStr.empty())
216 {
217 log<level::ERR>("Invalid privilege string.",
218 entry("INTF:%s", intfName.c_str()));
219 return;
220 }
221
222 uint8_t intfPriv = 0;
223 std::string channelName;
224 try
225 {
226 intfPriv =
227 static_cast<uint8_t>(chConfig.convertToPrivLimitIndex(intfPrivStr));
228 channelName = convertToChannelName(intfName);
229 }
230 catch (const std::invalid_argument& e)
231 {
232 log<level::ERR>("Exception: ", entry("MSG: %s", e.what()));
233 return;
234 }
235
236 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
237 channelLock{*chConfig.channelMutex};
238 uint8_t chNum = 0;
239 ChannelData* chData;
240 // Get the channel number based on the channel name.
241 for (chNum = 0; chNum < maxIpmiChannels; chNum++)
242 {
243 chData = chConfig.getChannelDataPtr(chNum);
244 if (chData->chName == channelName)
245 {
246 break;
247 }
248 }
249 if (chNum >= maxIpmiChannels)
250 {
251 log<level::ERR>("Invalid interface in signal path");
252 return;
253 }
254
255 // skip updating the values, if this property change originated from IPMI.
256 if (chConfig.signalFlag & (1 << chNum))
257 {
258 chConfig.signalFlag &= ~(1 << chNum);
259 log<level::DEBUG>("Request originated from IPMI so ignoring signal");
260 return;
261 }
262
263 // Update both volatile & Non-volatile, if there is mismatch.
264 // as property change other than IPMI, has to update both volatile &
265 // non-volatile data.
266 if (chData->chAccess.chNonVolatileData.privLimit != intfPriv)
267 {
268 // Update NV data
269 chData->chAccess.chNonVolatileData.privLimit = intfPriv;
270 if (chConfig.writeChannelPersistData() != 0)
271 {
272 log<level::ERR>("Failed to update the persist data file");
273 return;
274 }
275
276 // Update Volatile data
277 if (chData->chAccess.chVolatileData.privLimit != intfPriv)
278 {
279 chData->chAccess.chVolatileData.privLimit = intfPriv;
280 if (chConfig.writeChannelVolatileData() != 0)
281 {
282 log<level::ERR>("Failed to update the volatile data file");
283 return;
284 }
285 }
286 }
287
288 return;
289}
290
AppaRao Puli071f3f22018-05-24 16:45:30 +0530291ChannelConfig& getChannelConfigObject()
292{
293 static ChannelConfig channelConfig;
294 return channelConfig;
295}
296
AppaRao Puli9613ed72018-09-01 23:46:44 +0530297ChannelConfig::~ChannelConfig()
298{
299 if (signalHndlrObjectState)
300 {
301 chPropertiesSignal.reset();
302 sigHndlrLock.unlock();
303 }
304}
305
AppaRao Puli071f3f22018-05-24 16:45:30 +0530306ChannelConfig::ChannelConfig() : bus(ipmid_get_sd_bus_connection())
307{
308 std::ofstream mutexCleanUpFile;
309 mutexCleanUpFile.open(ipmiChMutexCleanupLockFile,
310 std::ofstream::out | std::ofstream::app);
311 if (!mutexCleanUpFile.good())
312 {
313 log<level::DEBUG>("Unable to open mutex cleanup file");
314 return;
315 }
316 mutexCleanUpFile.close();
317 mutexCleanupLock =
318 boost::interprocess::file_lock(ipmiChMutexCleanupLockFile);
319 if (mutexCleanupLock.try_lock())
320 {
321 boost::interprocess::named_recursive_mutex::remove(ipmiChannelMutex);
322 channelMutex =
323 std::make_unique<boost::interprocess::named_recursive_mutex>(
324 boost::interprocess::open_or_create, ipmiChannelMutex);
325 mutexCleanupLock.lock_sharable();
326 }
327 else
328 {
329 mutexCleanupLock.lock_sharable();
330 channelMutex =
331 std::make_unique<boost::interprocess::named_recursive_mutex>(
332 boost::interprocess::open_or_create, ipmiChannelMutex);
333 }
334
335 initChannelPersistData();
AppaRao Puli9613ed72018-09-01 23:46:44 +0530336
337 sigHndlrLock = boost::interprocess::file_lock(channelNvDataFilename);
338 // Register it for single object and single process either netipimd /
339 // host-ipmid
340 if (chPropertiesSignal == nullptr && sigHndlrLock.try_lock())
341 {
342 log<level::DEBUG>("Registering channel signal handler.");
343 chPropertiesSignal = std::make_unique<sdbusplus::bus::match_t>(
344 bus,
345 sdbusplus::bus::match::rules::path_namespace(
346 networkIntfObjectBasePath) +
347 sdbusplus::bus::match::rules::type::signal() +
348 sdbusplus::bus::match::rules::member(propertiesChangedSignal) +
349 sdbusplus::bus::match::rules::interface(
350 dBusPropertiesInterface) +
351 sdbusplus::bus::match::rules::argN(0, networkChConfigIntfName),
352 [&](sdbusplus::message::message& msg) {
353 DbusChObjProperties props;
354 std::string iface;
355 std::string path = msg.get_path();
356 msg.read(iface, props);
357 processChAccessPropChange(*this, path, props);
358 });
359 signalHndlrObjectState = true;
360 }
361}
362
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530363ChannelData* ChannelConfig::getChannelDataPtr(const uint8_t chNum)
AppaRao Puli9613ed72018-09-01 23:46:44 +0530364{
365 // reload data before using it.
366 checkAndReloadVolatileData();
367 return &channelData[chNum];
AppaRao Puli071f3f22018-05-24 16:45:30 +0530368}
369
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530370bool ChannelConfig::isValidChannel(const uint8_t chNum)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530371{
372 if (chNum > maxIpmiChannels)
373 {
374 log<level::DEBUG>("Invalid channel ID - Out of range");
375 return false;
376 }
377
378 if (channelData[chNum].isChValid == false)
379 {
380 log<level::DEBUG>("Channel is not valid");
381 return false;
382 }
383
384 return true;
385}
386
387EChannelSessSupported
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530388 ChannelConfig::getChannelSessionSupport(const uint8_t chNum)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530389{
390 EChannelSessSupported chSessSupport =
391 (EChannelSessSupported)channelData[chNum].chInfo.sessionSupported;
392 return chSessSupport;
393}
394
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530395bool ChannelConfig::isValidAuthType(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530396 const EAuthType& authType)
397{
398 if ((authType < EAuthType::md2) || (authType > EAuthType::oem))
399 {
400 log<level::DEBUG>("Invalid authentication type");
401 return false;
402 }
403
404 uint8_t authTypeSupported = channelData[chNum].chInfo.authTypeSupported;
405 if (!(authTypeSupported & (1 << static_cast<uint8_t>(authType))))
406 {
407 log<level::DEBUG>("Authentication type is not supported.");
408 return false;
409 }
410
411 return true;
412}
413
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530414int ChannelConfig::getChannelActiveSessions(const uint8_t chNum)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530415{
416 // TODO: TEMPORARY FIX
417 // Channels active session count is managed separatly
418 // by monitoring channel session which includes LAN and
419 // RAKP layer changes. This will be updated, once the
420 // authentication part is implemented.
421 return channelData[chNum].activeSessCount;
422}
423
Vernon Mauery58317122018-11-28 11:02:43 -0800424size_t ChannelConfig::getChannelMaxTransferSize(uint8_t chNum)
425{
426 return channelData[chNum].maxTransferSize;
427}
428
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530429ipmi_ret_t ChannelConfig::getChannelInfo(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530430 ChannelInfo& chInfo)
431{
432 if (!isValidChannel(chNum))
433 {
434 log<level::DEBUG>("Invalid channel");
435 return IPMI_CC_INVALID_FIELD_REQUEST;
436 }
437
438 std::copy_n(reinterpret_cast<uint8_t*>(&channelData[chNum].chInfo),
439 sizeof(channelData[chNum].chInfo),
440 reinterpret_cast<uint8_t*>(&chInfo));
441
442 return IPMI_CC_OK;
443}
444
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530445ipmi_ret_t ChannelConfig::getChannelAccessData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530446 ChannelAccess& chAccessData)
447{
448 if (!isValidChannel(chNum))
449 {
450 log<level::DEBUG>("Invalid channel");
451 return IPMI_CC_INVALID_FIELD_REQUEST;
452 }
453
454 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
455 {
456 log<level::DEBUG>("Session-less channel doesn't have access data.");
457 return IPMI_CC_ACTION_NOT_SUPPORTED_FOR_CHANNEL;
458 }
459
460 if (checkAndReloadVolatileData() != 0)
461 {
462 return IPMI_CC_UNSPECIFIED_ERROR;
463 }
464
465 std::copy_n(
466 reinterpret_cast<uint8_t*>(&channelData[chNum].chAccess.chVolatileData),
467 sizeof(channelData[chNum].chAccess.chVolatileData),
468 reinterpret_cast<uint8_t*>(&chAccessData));
469
470 return IPMI_CC_OK;
471}
472
473ipmi_ret_t
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530474 ChannelConfig::setChannelAccessData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530475 const ChannelAccess& chAccessData,
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530476 const uint8_t setFlag)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530477{
478 if (!isValidChannel(chNum))
479 {
480 log<level::DEBUG>("Invalid channel");
481 return IPMI_CC_INVALID_FIELD_REQUEST;
482 }
483
484 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
485 {
486 log<level::DEBUG>("Session-less channel doesn't have access data.");
487 return IPMI_CC_ACTION_NOT_SUPPORTED_FOR_CHANNEL;
488 }
489
490 if ((setFlag & setAccessMode) &&
491 (!isValidAccessMode(chAccessData.accessMode)))
492 {
493 log<level::DEBUG>("Invalid access mode specified");
494 return IPMI_CC_INVALID_FIELD_REQUEST;
495 }
496
497 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
498 channelLock{*channelMutex};
499
500 if (checkAndReloadVolatileData() != 0)
501 {
502 return IPMI_CC_UNSPECIFIED_ERROR;
503 }
504
505 if (setFlag & setAccessMode)
506 {
507 channelData[chNum].chAccess.chVolatileData.accessMode =
508 chAccessData.accessMode;
509 }
510 if (setFlag & setUserAuthEnabled)
511 {
512 channelData[chNum].chAccess.chVolatileData.userAuthDisabled =
513 chAccessData.userAuthDisabled;
514 }
515 if (setFlag & setMsgAuthEnabled)
516 {
517 channelData[chNum].chAccess.chVolatileData.perMsgAuthDisabled =
518 chAccessData.perMsgAuthDisabled;
519 }
520 if (setFlag & setAlertingEnabled)
521 {
522 channelData[chNum].chAccess.chVolatileData.alertingDisabled =
523 chAccessData.alertingDisabled;
524 }
525 if (setFlag & setPrivLimit)
526 {
527 channelData[chNum].chAccess.chVolatileData.privLimit =
528 chAccessData.privLimit;
529 }
530
531 // Write Volatile data to file
532 if (writeChannelVolatileData() != 0)
533 {
534 log<level::DEBUG>("Failed to update the channel volatile data");
535 return IPMI_CC_UNSPECIFIED_ERROR;
536 }
537 return IPMI_CC_OK;
538}
539
540ipmi_ret_t
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530541 ChannelConfig::getChannelAccessPersistData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530542 ChannelAccess& chAccessData)
543{
544 if (!isValidChannel(chNum))
545 {
546 log<level::DEBUG>("Invalid channel");
547 return IPMI_CC_INVALID_FIELD_REQUEST;
548 }
549
550 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
551 {
552 log<level::DEBUG>("Session-less channel doesn't have access data.");
553 return IPMI_CC_ACTION_NOT_SUPPORTED_FOR_CHANNEL;
554 }
555
556 if (checkAndReloadNVData() != 0)
557 {
558 return IPMI_CC_UNSPECIFIED_ERROR;
559 }
560
561 std::copy_n(reinterpret_cast<uint8_t*>(
562 &channelData[chNum].chAccess.chNonVolatileData),
563 sizeof(channelData[chNum].chAccess.chNonVolatileData),
564 reinterpret_cast<uint8_t*>(&chAccessData));
565
566 return IPMI_CC_OK;
567}
568
569ipmi_ret_t ChannelConfig::setChannelAccessPersistData(
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530570 const uint8_t chNum, const ChannelAccess& chAccessData,
571 const uint8_t setFlag)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530572{
573 if (!isValidChannel(chNum))
574 {
575 log<level::DEBUG>("Invalid channel");
576 return IPMI_CC_INVALID_FIELD_REQUEST;
577 }
578
579 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
580 {
581 log<level::DEBUG>("Session-less channel doesn't have access data.");
582 return IPMI_CC_ACTION_NOT_SUPPORTED_FOR_CHANNEL;
583 }
584
585 if ((setFlag & setAccessMode) &&
586 (!isValidAccessMode(chAccessData.accessMode)))
587 {
588 log<level::DEBUG>("Invalid access mode specified");
589 return IPMI_CC_INVALID_FIELD_REQUEST;
590 }
591
592 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
593 channelLock{*channelMutex};
594
595 if (checkAndReloadNVData() != 0)
596 {
597 return IPMI_CC_UNSPECIFIED_ERROR;
598 }
599
600 if (setFlag & setAccessMode)
601 {
602 channelData[chNum].chAccess.chNonVolatileData.accessMode =
603 chAccessData.accessMode;
604 }
605 if (setFlag & setUserAuthEnabled)
606 {
607 channelData[chNum].chAccess.chNonVolatileData.userAuthDisabled =
608 chAccessData.userAuthDisabled;
609 }
610 if (setFlag & setMsgAuthEnabled)
611 {
612 channelData[chNum].chAccess.chNonVolatileData.perMsgAuthDisabled =
613 chAccessData.perMsgAuthDisabled;
614 }
615 if (setFlag & setAlertingEnabled)
616 {
617 channelData[chNum].chAccess.chNonVolatileData.alertingDisabled =
618 chAccessData.alertingDisabled;
619 }
620 if (setFlag & setPrivLimit)
621 {
AppaRao Puli9613ed72018-09-01 23:46:44 +0530622 // Send Update to network channel config interfaces over dbus
623 std::string intfName = convertToNetInterface(channelData[chNum].chName);
624 std::string privStr = convertToPrivLimitString(chAccessData.privLimit);
625 std::string networkIntfObj =
626 std::string(networkIntfObjectBasePath) + "/" + intfName;
627 try
628 {
629 if (0 != setDbusProperty(bus, networkIntfServiceName,
630 networkIntfObj, networkChConfigIntfName,
631 privilegePropertyString, privStr))
632 {
633 log<level::DEBUG>("Network interface does not exist",
634 entry("INTERFACE:%s", intfName.c_str()));
635 return IPMI_CC_UNSPECIFIED_ERROR;
636 }
637 }
638 catch (const sdbusplus::exception::SdBusError& e)
639 {
640 log<level::ERR>("Exception: Network interface does not exist");
641 return IPMI_CC_INVALID_FIELD_REQUEST;
642 }
643 signalFlag |= (1 << chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530644 channelData[chNum].chAccess.chNonVolatileData.privLimit =
645 chAccessData.privLimit;
646 }
647
648 // Write persistent data to file
649 if (writeChannelPersistData() != 0)
650 {
651 log<level::DEBUG>("Failed to update the presist data file");
652 return IPMI_CC_UNSPECIFIED_ERROR;
653 }
654 return IPMI_CC_OK;
655}
656
657ipmi_ret_t
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530658 ChannelConfig::getChannelAuthTypeSupported(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530659 uint8_t& authTypeSupported)
660{
661 if (!isValidChannel(chNum))
662 {
663 log<level::DEBUG>("Invalid channel");
664 return IPMI_CC_INVALID_FIELD_REQUEST;
665 }
666
667 authTypeSupported = channelData[chNum].chInfo.authTypeSupported;
668 return IPMI_CC_OK;
669}
670
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530671ipmi_ret_t ChannelConfig::getChannelEnabledAuthType(const uint8_t chNum,
672 const uint8_t priv,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530673 EAuthType& authType)
674{
675 if (!isValidChannel(chNum))
676 {
677 log<level::DEBUG>("Invalid channel");
678 return IPMI_CC_INVALID_FIELD_REQUEST;
679 }
680
681 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
682 {
683 log<level::DEBUG>("Sessionless channel doesn't have access data.");
684 return IPMI_CC_INVALID_FIELD_REQUEST;
685 }
686
687 if (!isValidPrivLimit(priv))
688 {
689 log<level::DEBUG>("Invalid privilege specified.");
690 return IPMI_CC_INVALID_FIELD_REQUEST;
691 }
692
693 // TODO: Hardcoded for now. Need to implement.
694 authType = EAuthType::none;
695
696 return IPMI_CC_OK;
697}
698
699std::time_t ChannelConfig::getUpdatedFileTime(const std::string& fileName)
700{
701 struct stat fileStat;
702 if (stat(fileName.c_str(), &fileStat) != 0)
703 {
704 log<level::DEBUG>("Error in getting last updated time stamp");
705 return -EIO;
706 }
707 return fileStat.st_mtime;
708}
709
710EChannelAccessMode
711 ChannelConfig::convertToAccessModeIndex(const std::string& mode)
712{
713 auto iter = std::find(accessModeList.begin(), accessModeList.end(), mode);
714 if (iter == accessModeList.end())
715 {
716 log<level::ERR>("Invalid access mode.",
717 entry("MODE_STR=%s", mode.c_str()));
718 throw std::invalid_argument("Invalid access mode.");
719 }
720
721 return static_cast<EChannelAccessMode>(
722 std::distance(accessModeList.begin(), iter));
723}
724
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530725std::string ChannelConfig::convertToAccessModeString(const uint8_t value)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530726{
727 if (accessModeList.size() <= value)
728 {
729 log<level::ERR>("Invalid access mode.", entry("MODE_IDX=%d", value));
730 throw std::invalid_argument("Invalid access mode.");
731 }
732
733 return accessModeList.at(value);
734}
735
736CommandPrivilege
737 ChannelConfig::convertToPrivLimitIndex(const std::string& value)
738{
739 auto iter = std::find(privList.begin(), privList.end(), value);
740 if (iter == privList.end())
741 {
742 log<level::ERR>("Invalid privilege.",
743 entry("PRIV_STR=%s", value.c_str()));
744 throw std::invalid_argument("Invalid privilege.");
745 }
746
747 return static_cast<CommandPrivilege>(std::distance(privList.begin(), iter));
748}
749
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530750std::string ChannelConfig::convertToPrivLimitString(const uint8_t value)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530751{
752 if (privList.size() <= value)
753 {
754 log<level::ERR>("Invalid privilege.", entry("PRIV_IDX=%d", value));
755 throw std::invalid_argument("Invalid privilege.");
756 }
757
758 return privList.at(value);
759}
760
761EChannelSessSupported
762 ChannelConfig::convertToSessionSupportIndex(const std::string& value)
763{
764 auto iter =
765 std::find(sessionSupportList.begin(), sessionSupportList.end(), value);
766 if (iter == sessionSupportList.end())
767 {
768 log<level::ERR>("Invalid session supported.",
769 entry("SESS_STR=%s", value.c_str()));
770 throw std::invalid_argument("Invalid session supported.");
771 }
772
773 return static_cast<EChannelSessSupported>(
774 std::distance(sessionSupportList.begin(), iter));
775}
776
777EChannelMediumType
778 ChannelConfig::convertToMediumTypeIndex(const std::string& value)
779{
780 std::unordered_map<std::string, EChannelMediumType>::iterator it =
781 mediumTypeMap.find(value);
782 if (it == mediumTypeMap.end())
783 {
784 log<level::ERR>("Invalid medium type.",
785 entry("MEDIUM_STR=%s", value.c_str()));
786 throw std::invalid_argument("Invalid medium type.");
787 }
788
789 return static_cast<EChannelMediumType>(it->second);
790}
791
792EChannelProtocolType
793 ChannelConfig::convertToProtocolTypeIndex(const std::string& value)
794{
795 std::unordered_map<std::string, EChannelProtocolType>::iterator it =
796 protocolTypeMap.find(value);
797 if (it == protocolTypeMap.end())
798 {
799 log<level::ERR>("Invalid protocol type.",
800 entry("PROTO_STR=%s", value.c_str()));
801 throw std::invalid_argument("Invalid protocol type.");
802 }
803
804 return static_cast<EChannelProtocolType>(it->second);
805}
806
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530807uint8_t ChannelConfig::convertToChannelIndexNumber(const uint8_t chNum)
ssekarf4b2b092018-07-25 18:49:08 +0530808{
809
810 // TODO: There is limitation in current design. we cannot detect exact
811 // LAN interface(eth0 or eth1) so Implementation may be updated
812 // when there is any design update to figure out all the interfaces
813 // independently based on the message.
814
815 static uint8_t curChannel = 0xFF;
816
817 if (curChannel == 0xFF)
818 {
819 auto it = interfaceMap.find(getInterfaceIndex());
820 if (it == interfaceMap.end())
821 {
822 log<level::ERR>("Invalid Interface type ",
823 entry("InterfaceIndex: %d", getInterfaceIndex()));
824 throw std::invalid_argument("Invalid interface type.");
825 }
826
827 for (auto& channel : channelData)
828 {
829 std::string& interfaceName = it->second;
830 if (channel.chName == interfaceName)
831 {
832 curChannel = channel.chID;
833 break;
834 }
835 }
836 }
Richard Marian Thomaiyara39208e2018-12-08 17:27:11 +0530837 return ((chNum == currentChNum) ? curChannel : chNum);
ssekarf4b2b092018-07-25 18:49:08 +0530838}
839
AppaRao Puli9613ed72018-09-01 23:46:44 +0530840std::string ChannelConfig::convertToNetInterface(const std::string& value)
841{
842 auto it = channelToInterfaceMap.find(value);
843 if (it == channelToInterfaceMap.end())
844 {
845 log<level::DEBUG>("Invalid channel name.",
846 entry("NAME:%s", value.c_str()));
847 throw std::invalid_argument("Invalid channel name.");
848 }
849
850 return it->second;
851}
852
AppaRao Puli071f3f22018-05-24 16:45:30 +0530853Json ChannelConfig::readJsonFile(const std::string& configFile)
854{
855 std::ifstream jsonFile(configFile);
856 if (!jsonFile.good())
857 {
858 log<level::ERR>("JSON file not found");
859 return nullptr;
860 }
861
862 Json data = nullptr;
863 try
864 {
865 data = Json::parse(jsonFile, nullptr, false);
866 }
867 catch (Json::parse_error& e)
868 {
869 log<level::DEBUG>("Corrupted channel config.",
870 entry("MSG: %s", e.what()));
871 throw std::runtime_error("Corrupted channel config file");
872 }
873
874 return data;
875}
876
877int ChannelConfig::writeJsonFile(const std::string& configFile,
878 const Json& jsonData)
879{
880 std::ofstream jsonFile(configFile);
881 if (!jsonFile.good())
882 {
883 log<level::ERR>("JSON file not found");
884 return -EIO;
885 }
886
887 // Write JSON to file
888 jsonFile << jsonData;
889
890 jsonFile.flush();
891 return 0;
892}
893
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530894void ChannelConfig::setDefaultChannelConfig(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530895 const std::string& chName)
896{
897 channelData[chNum].chName = chName;
898 channelData[chNum].chID = chNum;
899 channelData[chNum].isChValid = false;
900 channelData[chNum].activeSessCount = 0;
901
902 channelData[chNum].chInfo.mediumType = defaultMediumType;
903 channelData[chNum].chInfo.protocolType = defaultProtocolType;
904 channelData[chNum].chInfo.sessionSupported = defaultSessionSupported;
905 channelData[chNum].chInfo.isIpmi = defaultIsIpmiState;
906 channelData[chNum].chInfo.authTypeSupported = defaultAuthType;
907}
908
909int ChannelConfig::loadChannelConfig()
910{
911 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
912 channelLock{*channelMutex};
913
914 Json data = readJsonFile(channelConfigDefaultFilename);
915 if (data == nullptr)
916 {
917 log<level::DEBUG>("Error in opening IPMI Channel data file");
918 return -EIO;
919 }
920
921 try
922 {
923 // Fill in global structure
924 for (uint8_t chNum = 0; chNum < maxIpmiChannels; chNum++)
925 {
926 std::fill(reinterpret_cast<uint8_t*>(&channelData[chNum]),
927 reinterpret_cast<uint8_t*>(&channelData[chNum]) +
928 sizeof(ChannelData),
929 0);
930 std::string chKey = std::to_string(chNum);
931 Json jsonChData = data[chKey].get<Json>();
932 if (jsonChData.is_null())
933 {
934 log<level::WARNING>(
935 "Channel not configured so loading default.",
936 entry("CHANNEL_NUM:%d", chNum));
937 // If user didn't want to configure specific channel (say
938 // reserved channel), then load that index with default values.
939 std::string chName(defaultChannelName);
940 setDefaultChannelConfig(chNum, chName);
941 }
942 else
943 {
944 std::string chName = jsonChData[nameString].get<std::string>();
945 channelData[chNum].chName = chName;
946 channelData[chNum].chID = chNum;
947 channelData[chNum].isChValid =
948 jsonChData[isValidString].get<bool>();
949 channelData[chNum].activeSessCount =
950 jsonChData.value(activeSessionsString, 0);
Vernon Mauery58317122018-11-28 11:02:43 -0800951 channelData[chNum].maxTransferSize =
952 jsonChData.value(maxTransferSizeString, smallChannelSize);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530953 Json jsonChInfo = jsonChData[channelInfoString].get<Json>();
954 if (jsonChInfo.is_null())
955 {
956 log<level::ERR>("Invalid/corrupted channel config file");
957 return -EBADMSG;
958 }
959 else
960 {
961 std::string medTypeStr =
962 jsonChInfo[mediumTypeString].get<std::string>();
963 channelData[chNum].chInfo.mediumType = static_cast<uint8_t>(
964 convertToMediumTypeIndex(medTypeStr));
965 std::string protoTypeStr =
966 jsonChInfo[protocolTypeString].get<std::string>();
967 channelData[chNum].chInfo.protocolType =
968 static_cast<uint8_t>(
969 convertToProtocolTypeIndex(protoTypeStr));
970 std::string sessStr =
971 jsonChInfo[sessionSupportedString].get<std::string>();
972 channelData[chNum].chInfo.sessionSupported =
973 static_cast<uint8_t>(
974 convertToSessionSupportIndex(sessStr));
975 channelData[chNum].chInfo.isIpmi =
976 jsonChInfo[isIpmiString].get<bool>();
977 channelData[chNum].chInfo.authTypeSupported =
978 defaultAuthType;
979 }
980 }
981 }
982 }
983 catch (const Json::exception& e)
984 {
985 log<level::DEBUG>("Json Exception caught.", entry("MSG:%s", e.what()));
986 return -EBADMSG;
987 }
988 catch (const std::invalid_argument& e)
989 {
990 log<level::ERR>("Corrupted config.", entry("MSG:%s", e.what()));
991 return -EBADMSG;
992 }
993
994 return 0;
995}
996
997int ChannelConfig::readChannelVolatileData()
998{
999 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1000 channelLock{*channelMutex};
1001
1002 Json data = readJsonFile(channelVolatileDataFilename);
1003 if (data == nullptr)
1004 {
1005 log<level::DEBUG>("Error in opening IPMI Channel data file");
1006 return -EIO;
1007 }
1008
1009 try
1010 {
1011 // Fill in global structure
1012 for (auto it = data.begin(); it != data.end(); ++it)
1013 {
1014 std::string chKey = it.key();
1015 uint8_t chNum = std::stoi(chKey, nullptr, 10);
1016 if ((chNum < 0) || (chNum > maxIpmiChannels))
1017 {
1018 log<level::DEBUG>(
1019 "Invalid channel access entry in config file");
1020 throw std::out_of_range("Out of range - channel number");
1021 }
1022 Json jsonChData = it.value();
1023 if (!jsonChData.is_null())
1024 {
1025 std::string accModeStr =
1026 jsonChData[accessModeString].get<std::string>();
1027 channelData[chNum].chAccess.chVolatileData.accessMode =
1028 static_cast<uint8_t>(convertToAccessModeIndex(accModeStr));
1029 channelData[chNum].chAccess.chVolatileData.userAuthDisabled =
1030 jsonChData[userAuthDisabledString].get<bool>();
1031 channelData[chNum].chAccess.chVolatileData.perMsgAuthDisabled =
1032 jsonChData[perMsgAuthDisabledString].get<bool>();
1033 channelData[chNum].chAccess.chVolatileData.alertingDisabled =
1034 jsonChData[alertingDisabledString].get<bool>();
1035 std::string privStr =
1036 jsonChData[privLimitString].get<std::string>();
1037 channelData[chNum].chAccess.chVolatileData.privLimit =
1038 static_cast<uint8_t>(convertToPrivLimitIndex(privStr));
1039 }
1040 else
1041 {
1042 log<level::ERR>(
1043 "Invalid/corrupted volatile channel access file",
1044 entry("FILE: %s", channelVolatileDataFilename));
1045 throw std::runtime_error(
1046 "Corrupted volatile channel access file");
1047 }
1048 }
1049 }
1050 catch (const Json::exception& e)
1051 {
1052 log<level::DEBUG>("Json Exception caught.", entry("MSG:%s", e.what()));
1053 throw std::runtime_error("Corrupted volatile channel access file");
1054 }
1055 catch (const std::invalid_argument& e)
1056 {
1057 log<level::ERR>("Corrupted config.", entry("MSG:%s", e.what()));
1058 throw std::runtime_error("Corrupted volatile channel access file");
1059 }
1060
1061 // Update the timestamp
1062 voltFileLastUpdatedTime = getUpdatedFileTime(channelVolatileDataFilename);
1063 return 0;
1064}
1065
1066int ChannelConfig::readChannelPersistData()
1067{
1068 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1069 channelLock{*channelMutex};
1070
1071 Json data = readJsonFile(channelNvDataFilename);
1072 if (data == nullptr)
1073 {
1074 log<level::DEBUG>("Error in opening IPMI Channel data file");
1075 return -EIO;
1076 }
1077
1078 try
1079 {
1080 // Fill in global structure
1081 for (auto it = data.begin(); it != data.end(); ++it)
1082 {
1083 std::string chKey = it.key();
1084 uint8_t chNum = std::stoi(chKey, nullptr, 10);
1085 if ((chNum < 0) || (chNum > maxIpmiChannels))
1086 {
1087 log<level::DEBUG>(
1088 "Invalid channel access entry in config file");
1089 throw std::out_of_range("Out of range - channel number");
1090 }
1091 Json jsonChData = it.value();
1092 if (!jsonChData.is_null())
1093 {
1094 std::string accModeStr =
1095 jsonChData[accessModeString].get<std::string>();
1096 channelData[chNum].chAccess.chNonVolatileData.accessMode =
1097 static_cast<uint8_t>(convertToAccessModeIndex(accModeStr));
1098 channelData[chNum].chAccess.chNonVolatileData.userAuthDisabled =
1099 jsonChData[userAuthDisabledString].get<bool>();
1100 channelData[chNum]
1101 .chAccess.chNonVolatileData.perMsgAuthDisabled =
1102 jsonChData[perMsgAuthDisabledString].get<bool>();
1103 channelData[chNum].chAccess.chNonVolatileData.alertingDisabled =
1104 jsonChData[alertingDisabledString].get<bool>();
1105 std::string privStr =
1106 jsonChData[privLimitString].get<std::string>();
1107 channelData[chNum].chAccess.chNonVolatileData.privLimit =
1108 static_cast<uint8_t>(convertToPrivLimitIndex(privStr));
1109 }
1110 else
1111 {
1112 log<level::ERR>("Invalid/corrupted nv channel access file",
1113 entry("FILE:%s", channelNvDataFilename));
1114 throw std::runtime_error("Corrupted nv channel access file");
1115 }
1116 }
1117 }
1118 catch (const Json::exception& e)
1119 {
1120 log<level::DEBUG>("Json Exception caught.", entry("MSG:%s", e.what()));
1121 throw std::runtime_error("Corrupted nv channel access file");
1122 }
1123 catch (const std::invalid_argument& e)
1124 {
1125 log<level::ERR>("Corrupted config.", entry("MSG: %s", e.what()));
1126 throw std::runtime_error("Corrupted nv channel access file");
1127 }
1128
1129 // Update the timestamp
1130 nvFileLastUpdatedTime = getUpdatedFileTime(channelNvDataFilename);
1131 return 0;
1132}
1133
1134int ChannelConfig::writeChannelVolatileData()
1135{
1136 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1137 channelLock{*channelMutex};
1138 Json outData;
1139
1140 try
1141 {
1142 for (uint8_t chNum = 0; chNum < maxIpmiChannels; chNum++)
1143 {
1144 if (getChannelSessionSupport(chNum) != EChannelSessSupported::none)
1145 {
1146 Json jsonObj;
1147 std::string chKey = std::to_string(chNum);
1148 std::string accModeStr = convertToAccessModeString(
1149 channelData[chNum].chAccess.chVolatileData.accessMode);
1150 jsonObj[accessModeString] = accModeStr;
1151 jsonObj[userAuthDisabledString] =
1152 channelData[chNum].chAccess.chVolatileData.userAuthDisabled;
1153 jsonObj[perMsgAuthDisabledString] =
1154 channelData[chNum]
1155 .chAccess.chVolatileData.perMsgAuthDisabled;
1156 jsonObj[alertingDisabledString] =
1157 channelData[chNum].chAccess.chVolatileData.alertingDisabled;
1158 std::string privStr = convertToPrivLimitString(
1159 channelData[chNum].chAccess.chVolatileData.privLimit);
1160 jsonObj[privLimitString] = privStr;
1161
1162 outData[chKey] = jsonObj;
1163 }
1164 }
1165 }
1166 catch (const std::invalid_argument& e)
1167 {
1168 log<level::ERR>("Corrupted config.", entry("MSG: %s", e.what()));
1169 return -EINVAL;
1170 }
1171
1172 if (writeJsonFile(channelVolatileDataFilename, outData) != 0)
1173 {
1174 log<level::DEBUG>("Error in write JSON data to file");
1175 return -EIO;
1176 }
1177
1178 // Update the timestamp
1179 voltFileLastUpdatedTime = getUpdatedFileTime(channelVolatileDataFilename);
1180 return 0;
1181}
1182
1183int ChannelConfig::writeChannelPersistData()
1184{
1185 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1186 channelLock{*channelMutex};
1187 Json outData;
1188
1189 try
1190 {
1191 for (uint8_t chNum = 0; chNum < maxIpmiChannels; chNum++)
1192 {
1193 if (getChannelSessionSupport(chNum) != EChannelSessSupported::none)
1194 {
1195 Json jsonObj;
1196 std::string chKey = std::to_string(chNum);
1197 std::string accModeStr = convertToAccessModeString(
1198 channelData[chNum].chAccess.chNonVolatileData.accessMode);
1199 jsonObj[accessModeString] = accModeStr;
1200 jsonObj[userAuthDisabledString] =
1201 channelData[chNum]
1202 .chAccess.chNonVolatileData.userAuthDisabled;
1203 jsonObj[perMsgAuthDisabledString] =
1204 channelData[chNum]
1205 .chAccess.chNonVolatileData.perMsgAuthDisabled;
1206 jsonObj[alertingDisabledString] =
1207 channelData[chNum]
1208 .chAccess.chNonVolatileData.alertingDisabled;
1209 std::string privStr = convertToPrivLimitString(
1210 channelData[chNum].chAccess.chNonVolatileData.privLimit);
1211 jsonObj[privLimitString] = privStr;
1212
1213 outData[chKey] = jsonObj;
1214 }
1215 }
1216 }
1217 catch (const std::invalid_argument& e)
1218 {
1219 log<level::ERR>("Corrupted config.", entry("MSG: %s", e.what()));
1220 return -EINVAL;
1221 }
1222
1223 if (writeJsonFile(channelNvDataFilename, outData) != 0)
1224 {
1225 log<level::DEBUG>("Error in write JSON data to file");
1226 return -EIO;
1227 }
1228
1229 // Update the timestamp
1230 nvFileLastUpdatedTime = getUpdatedFileTime(channelNvDataFilename);
1231 return 0;
1232}
1233
1234int ChannelConfig::checkAndReloadNVData()
1235{
1236 std::time_t updateTime = getUpdatedFileTime(channelNvDataFilename);
1237 int ret = 0;
1238 if (updateTime != nvFileLastUpdatedTime || updateTime == -EIO)
1239 {
1240 try
1241 {
1242 ret = readChannelPersistData();
1243 }
1244 catch (const std::exception& e)
1245 {
1246 log<level::ERR>("Exception caught in readChannelPersistData.",
1247 entry("MSG=%s", e.what()));
1248 ret = -EIO;
1249 }
1250 }
1251 return ret;
1252}
1253
1254int ChannelConfig::checkAndReloadVolatileData()
1255{
1256 std::time_t updateTime = getUpdatedFileTime(channelVolatileDataFilename);
1257 int ret = 0;
1258 if (updateTime != voltFileLastUpdatedTime || updateTime == -EIO)
1259 {
1260 try
1261 {
1262 ret = readChannelVolatileData();
1263 }
1264 catch (const std::exception& e)
1265 {
1266 log<level::ERR>("Exception caught in readChannelVolatileData.",
1267 entry("MSG=%s", e.what()));
1268 ret = -EIO;
1269 }
1270 }
1271 return ret;
1272}
1273
AppaRao Puli9613ed72018-09-01 23:46:44 +05301274int ChannelConfig::setDbusProperty(sdbusplus::bus::bus& bus,
1275 const std::string& service,
1276 const std::string& objPath,
1277 const std::string& interface,
1278 const std::string& property,
1279 const DbusVariant& value)
1280{
1281 try
1282 {
1283 auto method =
1284 bus.new_method_call(service.c_str(), objPath.c_str(),
1285 "org.freedesktop.DBus.Properties", "Set");
1286
1287 method.append(interface, property, value);
1288
1289 auto reply = bus.call(method);
1290 }
1291 catch (const sdbusplus::exception::SdBusError& e)
1292 {
1293 log<level::DEBUG>("set-property failed",
1294 entry("SERVICE:%s", service.c_str()),
1295 entry("OBJPATH:%s", objPath.c_str()),
1296 entry("INTERFACE:%s", interface.c_str()),
1297 entry("PROP:%s", property.c_str()));
1298 return -EIO;
1299 }
1300
1301 return 0;
1302}
1303
1304int ChannelConfig::getDbusProperty(sdbusplus::bus::bus& bus,
1305 const std::string& service,
1306 const std::string& objPath,
1307 const std::string& interface,
1308 const std::string& property,
1309 DbusVariant& value)
1310{
1311 try
1312 {
1313 auto method =
1314 bus.new_method_call(service.c_str(), objPath.c_str(),
1315 "org.freedesktop.DBus.Properties", "Get");
1316
1317 method.append(interface, property);
1318
1319 auto reply = bus.call(method);
1320 reply.read(value);
1321 }
1322 catch (const sdbusplus::exception::SdBusError& e)
1323 {
1324 log<level::DEBUG>("get-property failed",
1325 entry("SERVICE:%s", service.c_str()),
1326 entry("OBJPATH:%s", objPath.c_str()),
1327 entry("INTERFACE:%s", interface.c_str()),
1328 entry("PROP:%s", property.c_str()));
1329 return -EIO;
1330 }
1331 return 0;
1332}
1333
1334int ChannelConfig::syncNetworkChannelConfig()
1335{
1336 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1337 channelLock{*channelMutex};
1338 bool isUpdated = false;
1339 for (uint8_t chNum = 0; chNum < maxIpmiChannels; chNum++)
1340 {
1341 if (getChannelSessionSupport(chNum) != EChannelSessSupported::none)
1342 {
1343 std::string intfPrivStr;
1344 try
1345 {
1346 std::string intfName =
1347 convertToNetInterface(channelData[chNum].chName);
1348 std::string networkIntfObj =
1349 std::string(networkIntfObjectBasePath) + "/" + intfName;
1350 DbusVariant variant;
1351 if (0 != getDbusProperty(bus, networkIntfServiceName,
1352 networkIntfObj,
1353 networkChConfigIntfName,
1354 privilegePropertyString, variant))
1355 {
1356 log<level::DEBUG>("Network interface does not exist",
1357 entry("INTERFACE:%s", intfName.c_str()));
1358 continue;
1359 }
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001360 intfPrivStr = variant_ns::get<std::string>(variant);
AppaRao Puli9613ed72018-09-01 23:46:44 +05301361 }
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001362 catch (const variant_ns::bad_variant_access& e)
AppaRao Puli9613ed72018-09-01 23:46:44 +05301363 {
1364 log<level::DEBUG>(
1365 "exception: Network interface does not exist");
1366 continue;
1367 }
1368 catch (const sdbusplus::exception::SdBusError& e)
1369 {
1370 log<level::DEBUG>(
1371 "exception: Network interface does not exist");
1372 continue;
1373 }
1374
1375 uint8_t intfPriv =
1376 static_cast<uint8_t>(convertToPrivLimitIndex(intfPrivStr));
1377 if (channelData[chNum].chAccess.chNonVolatileData.privLimit !=
1378 intfPriv)
1379 {
1380 isUpdated = true;
1381 channelData[chNum].chAccess.chNonVolatileData.privLimit =
1382 intfPriv;
1383 channelData[chNum].chAccess.chVolatileData.privLimit = intfPriv;
1384 }
1385 }
1386 }
1387
1388 if (isUpdated)
1389 {
1390 // Write persistent data to file
1391 if (writeChannelPersistData() != 0)
1392 {
1393 log<level::DEBUG>("Failed to update the persistent data file");
1394 return -EIO;
1395 }
1396 // Write Volatile data to file
1397 if (writeChannelVolatileData() != 0)
1398 {
1399 log<level::DEBUG>("Failed to update the channel volatile data");
1400 return -EIO;
1401 }
1402 }
1403
1404 return 0;
1405}
1406
AppaRao Puli071f3f22018-05-24 16:45:30 +05301407void ChannelConfig::initChannelPersistData()
1408{
1409 /* Always read the channel config */
1410 if (loadChannelConfig() != 0)
1411 {
1412 log<level::ERR>("Failed to read channel config file");
1413 throw std::ios_base::failure("Failed to load channel configuration");
1414 }
1415
1416 /* Populate the channel persist data */
1417 if (readChannelPersistData() != 0)
1418 {
1419 // Copy default NV data to RW location
1420 std::experimental::filesystem::copy_file(channelAccessDefaultFilename,
1421 channelNvDataFilename);
1422
1423 // Load the channel access NV data
1424 if (readChannelPersistData() != 0)
1425 {
1426 log<level::ERR>("Failed to read channel access NV data");
1427 throw std::ios_base::failure(
1428 "Failed to read channel access NV configuration");
1429 }
1430 }
1431
1432 // First check the volatile data file
1433 // If not present, load the default values
1434 if (readChannelVolatileData() != 0)
1435 {
1436 // Copy default volatile data to temporary location
1437 // NV file(channelNvDataFilename) must have created by now.
1438 std::experimental::filesystem::copy_file(channelNvDataFilename,
1439 channelVolatileDataFilename);
1440
1441 // Load the channel access volatile data
1442 if (readChannelVolatileData() != 0)
1443 {
1444 log<level::ERR>("Failed to read channel access volatile data");
1445 throw std::ios_base::failure(
1446 "Failed to read channel access volatile configuration");
1447 }
1448 }
AppaRao Puli9613ed72018-09-01 23:46:44 +05301449
1450 // Synchronize the channel config(priv) with network channel
1451 // configuration(priv) over dbus
1452 if (syncNetworkChannelConfig() != 0)
1453 {
1454 log<level::ERR>(
1455 "Failed to synchronize data with network channel config over dbus");
1456 throw std::ios_base::failure(
1457 "Failed to synchronize data with network channel config over dbus");
1458 }
1459
1460 log<level::DEBUG>("Successfully completed channel data initialization.");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301461 return;
1462}
1463
1464} // namespace ipmi