blob: 841cce59702eed1424d0cb96c89571663ff9b7af [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"
Johnathan Manteyfd61fc32021-04-08 11:05:38 -070020#include "user_layer.hpp"
AppaRao Puli071f3f22018-05-24 16:45:30 +053021
Johnathan Mantey0a2abc82021-02-18 12:39:12 -080022#include <ifaddrs.h>
AppaRao Puli071f3f22018-05-24 16:45:30 +053023#include <sys/stat.h>
Johnathan Mantey0a2abc82021-02-18 12:39:12 -080024#include <sys/types.h>
AppaRao Puli071f3f22018-05-24 16:45:30 +053025#include <unistd.h>
26
27#include <boost/interprocess/sync/scoped_lock.hpp>
George Liu42f64ef2024-02-05 15:03:18 +080028#include <ipmid/utils.hpp>
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -070029#include <phosphor-logging/lg2.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050030#include <sdbusplus/bus/match.hpp>
31#include <sdbusplus/server/object.hpp>
32
AppaRao Puli071f3f22018-05-24 16:45:30 +053033#include <cerrno>
AppaRao Puli9613ed72018-09-01 23:46:44 +053034#include <exception>
Patrick Williams3d8d7932022-06-16 12:01:28 -050035#include <filesystem>
AppaRao Puli071f3f22018-05-24 16:45:30 +053036#include <fstream>
AppaRao Puli071f3f22018-05-24 16:45:30 +053037#include <unordered_map>
38
39namespace ipmi
40{
41
42using namespace phosphor::logging;
43
44static constexpr const char* channelAccessDefaultFilename =
45 "/usr/share/ipmi-providers/channel_access.json";
46static constexpr const char* channelConfigDefaultFilename =
47 "/usr/share/ipmi-providers/channel_config.json";
48static constexpr const char* channelNvDataFilename =
49 "/var/lib/ipmi/channel_access_nv.json";
50static constexpr const char* channelVolatileDataFilename =
51 "/run/ipmi/channel_access_volatile.json";
52
AppaRao Puli9613ed72018-09-01 23:46:44 +053053// TODO: Get the service name dynamically..
54static constexpr const char* networkIntfServiceName =
55 "xyz.openbmc_project.Network";
56static constexpr const char* networkIntfObjectBasePath =
57 "/xyz/openbmc_project/network";
58static constexpr const char* networkChConfigIntfName =
59 "xyz.openbmc_project.Channel.ChannelAccess";
60static constexpr const char* privilegePropertyString = "MaxPrivilege";
61static constexpr const char* dBusPropertiesInterface =
62 "org.freedesktop.DBus.Properties";
63static constexpr const char* propertiesChangedSignal = "PropertiesChanged";
Willy Tuac05aa12021-11-16 21:15:58 -080064static constexpr const char* interfaceAddedSignal = "InterfacesAdded";
65static constexpr const char* interfaceRemovedSignal = "InterfacesRemoved";
AppaRao Puli9613ed72018-09-01 23:46:44 +053066
AppaRao Puli071f3f22018-05-24 16:45:30 +053067// STRING DEFINES: Should sync with key's in JSON
68static constexpr const char* nameString = "name";
69static constexpr const char* isValidString = "is_valid";
70static constexpr const char* activeSessionsString = "active_sessions";
Vernon Mauery58317122018-11-28 11:02:43 -080071static constexpr const char* maxTransferSizeString = "max_transfer_size";
AppaRao Puli071f3f22018-05-24 16:45:30 +053072static constexpr const char* channelInfoString = "channel_info";
73static constexpr const char* mediumTypeString = "medium_type";
74static constexpr const char* protocolTypeString = "protocol_type";
75static constexpr const char* sessionSupportedString = "session_supported";
76static constexpr const char* isIpmiString = "is_ipmi";
Johnathan Manteyfd61fc32021-04-08 11:05:38 -070077static constexpr const char* isManagementNIC = "is_management_nic";
AppaRao Puli071f3f22018-05-24 16:45:30 +053078static constexpr const char* authTypeSupportedString = "auth_type_supported";
79static constexpr const char* accessModeString = "access_mode";
80static constexpr const char* userAuthDisabledString = "user_auth_disabled";
81static constexpr const char* perMsgAuthDisabledString = "per_msg_auth_disabled";
82static constexpr const char* alertingDisabledString = "alerting_disabled";
83static constexpr const char* privLimitString = "priv_limit";
84static constexpr const char* authTypeEnabledString = "auth_type_enabled";
85
86// Default values
87static constexpr const char* defaultChannelName = "RESERVED";
88static constexpr const uint8_t defaultMediumType =
89 static_cast<uint8_t>(EChannelMediumType::reserved);
90static constexpr const uint8_t defaultProtocolType =
91 static_cast<uint8_t>(EChannelProtocolType::reserved);
92static constexpr const uint8_t defaultSessionSupported =
93 static_cast<uint8_t>(EChannelSessSupported::none);
94static constexpr const uint8_t defaultAuthType =
95 static_cast<uint8_t>(EAuthType::none);
96static constexpr const bool defaultIsIpmiState = false;
Vernon Mauery58317122018-11-28 11:02:43 -080097static constexpr size_t smallChannelSize = 64;
AppaRao Puli071f3f22018-05-24 16:45:30 +053098
Lei YU4b0ddb62019-01-25 16:43:50 +080099std::unique_ptr<sdbusplus::bus::match_t> chPropertiesSignal
100 __attribute__((init_priority(101)));
AppaRao Puli9613ed72018-09-01 23:46:44 +0530101
Willy Tuac05aa12021-11-16 21:15:58 -0800102std::unique_ptr<sdbusplus::bus::match_t> chInterfaceAddedSignal
103 __attribute__((init_priority(101)));
104
105std::unique_ptr<sdbusplus::bus::match_t> chInterfaceRemovedSignal
106 __attribute__((init_priority(101)));
107
AppaRao Puli071f3f22018-05-24 16:45:30 +0530108// String mappings use in JSON config file
109static std::unordered_map<std::string, EChannelMediumType> mediumTypeMap = {
110 {"reserved", EChannelMediumType::reserved},
111 {"ipmb", EChannelMediumType::ipmb},
112 {"icmb-v1.0", EChannelMediumType::icmbV10},
113 {"icmb-v0.9", EChannelMediumType::icmbV09},
114 {"lan-802.3", EChannelMediumType::lan8032},
115 {"serial", EChannelMediumType::serial},
116 {"other-lan", EChannelMediumType::otherLan},
117 {"pci-smbus", EChannelMediumType::pciSmbus},
118 {"smbus-v1.0", EChannelMediumType::smbusV11},
119 {"smbus-v2.0", EChannelMediumType::smbusV20},
120 {"usb-1x", EChannelMediumType::usbV1x},
121 {"usb-2x", EChannelMediumType::usbV2x},
122 {"system-interface", EChannelMediumType::systemInterface},
123 {"oem", EChannelMediumType::oem},
124 {"unknown", EChannelMediumType::unknown}};
125
ssekarf4b2b092018-07-25 18:49:08 +0530126static std::unordered_map<EInterfaceIndex, std::string> interfaceMap = {
Richard Marian Thomaiyar43cb1282018-12-08 17:22:53 +0530127 {interfaceKCS, "SMS"},
Richard Marian Thomaiyar73906b92019-01-04 23:48:02 +0530128 {interfaceLAN1, "eth0"},
ssekarf4b2b092018-07-25 18:49:08 +0530129 {interfaceUnknown, "unknown"}};
130
AppaRao Puli071f3f22018-05-24 16:45:30 +0530131static std::unordered_map<std::string, EChannelProtocolType> protocolTypeMap = {
132 {"na", EChannelProtocolType::na},
133 {"ipmb-1.0", EChannelProtocolType::ipmbV10},
134 {"icmb-2.0", EChannelProtocolType::icmbV11},
135 {"reserved", EChannelProtocolType::reserved},
136 {"ipmi-smbus", EChannelProtocolType::ipmiSmbus},
137 {"kcs", EChannelProtocolType::kcs},
138 {"smic", EChannelProtocolType::smic},
139 {"bt-10", EChannelProtocolType::bt10},
140 {"bt-15", EChannelProtocolType::bt15},
141 {"tmode", EChannelProtocolType::tMode},
142 {"oem", EChannelProtocolType::oem}};
143
144static std::array<std::string, 4> accessModeList = {
145 "disabled", "pre-boot", "always_available", "shared"};
146
147static std::array<std::string, 4> sessionSupportList = {
148 "session-less", "single-session", "multi-session", "session-based"};
149
Sumanth Bhate4e633e2019-05-14 12:13:57 +0000150const std::array<std::string, PRIVILEGE_OEM + 1> privList = {
AppaRao Puli071f3f22018-05-24 16:45:30 +0530151 "priv-reserved", "priv-callback", "priv-user",
152 "priv-operator", "priv-admin", "priv-oem"};
153
Richard Marian Thomaiyar55768e32019-03-02 22:54:37 +0530154std::string ChannelConfig::getChannelName(const uint8_t chNum)
Johnathan Mantey74a21022018-12-13 13:17:56 -0800155{
156 if (!isValidChannel(chNum))
157 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700158 lg2::error("Invalid channel number: {CHANNEL_ID}", "CHANNEL_ID", chNum);
Johnathan Mantey74a21022018-12-13 13:17:56 -0800159 throw std::invalid_argument("Invalid channel number");
160 }
161
162 return channelData[chNum].chName;
163}
164
165int ChannelConfig::convertToChannelNumberFromChannelName(
166 const std::string& chName)
167{
168 for (const auto& it : channelData)
169 {
170 if (it.chName == chName)
171 {
172 return it.chID;
173 }
174 }
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700175 lg2::error("Invalid channel name: {CHANNEL}", "CHANNEL", chName);
Johnathan Mantey74a21022018-12-13 13:17:56 -0800176 throw std::invalid_argument("Invalid channel name");
177
178 return -1;
179}
180
181std::string ChannelConfig::getChannelNameFromPath(const std::string& path)
AppaRao Puli9613ed72018-09-01 23:46:44 +0530182{
Peter Foley1214d6c2023-11-01 11:12:42 -0400183 const size_t length = strlen(networkIntfObjectBasePath);
Richard Marian Thomaiyarbbbc3952020-01-17 12:13:28 +0530184 if (((length + 1) >= path.size()) ||
185 path.compare(0, length, networkIntfObjectBasePath))
AppaRao Puli9613ed72018-09-01 23:46:44 +0530186 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700187 lg2::error("Invalid object path: {PATH}", "PATH", path);
Richard Marian Thomaiyarbbbc3952020-01-17 12:13:28 +0530188 throw std::invalid_argument("Invalid object path");
AppaRao Puli9613ed72018-09-01 23:46:44 +0530189 }
Richard Marian Thomaiyarbbbc3952020-01-17 12:13:28 +0530190 std::string chName(path, length + 1);
Johnathan Mantey74a21022018-12-13 13:17:56 -0800191 return chName;
AppaRao Puli9613ed72018-09-01 23:46:44 +0530192}
193
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800194void ChannelConfig::processChAccessPropChange(
195 const std::string& path, const DbusChObjProperties& chProperties)
AppaRao Puli9613ed72018-09-01 23:46:44 +0530196{
197 // Get interface name from path. ex: '/xyz/openbmc_project/network/eth0'
Johnathan Mantey74a21022018-12-13 13:17:56 -0800198 std::string chName;
AppaRao Puli9613ed72018-09-01 23:46:44 +0530199 try
200 {
Johnathan Mantey74a21022018-12-13 13:17:56 -0800201 chName = getChannelNameFromPath(path);
AppaRao Puli9613ed72018-09-01 23:46:44 +0530202 }
203 catch (const std::invalid_argument& e)
204 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700205 lg2::error("Exception: {MSG}", "MSG", e.what());
AppaRao Puli9613ed72018-09-01 23:46:44 +0530206 return;
207 }
208
209 // Get the MaxPrivilege property value from the signal
210 std::string intfPrivStr;
211 std::string propName;
212 for (const auto& prop : chProperties)
213 {
214 if (prop.first == privilegePropertyString)
215 {
216 propName = privilegePropertyString;
Vernon Maueryf442e112019-04-09 11:44:36 -0700217 intfPrivStr = std::get<std::string>(prop.second);
AppaRao Puli9613ed72018-09-01 23:46:44 +0530218 break;
219 }
220 }
221
222 if (propName != privilegePropertyString)
223 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700224 lg2::error("Unknown signal caught.");
AppaRao Puli9613ed72018-09-01 23:46:44 +0530225 return;
226 }
227
228 if (intfPrivStr.empty())
229 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700230 lg2::error("Invalid privilege string for intf {INTF}", "INTF", chName);
AppaRao Puli9613ed72018-09-01 23:46:44 +0530231 return;
232 }
233
234 uint8_t intfPriv = 0;
Johnathan Mantey74a21022018-12-13 13:17:56 -0800235 int chNum;
AppaRao Puli9613ed72018-09-01 23:46:44 +0530236 try
237 {
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800238 intfPriv = static_cast<uint8_t>(convertToPrivLimitIndex(intfPrivStr));
Johnathan Mantey74a21022018-12-13 13:17:56 -0800239 chNum = convertToChannelNumberFromChannelName(chName);
AppaRao Puli9613ed72018-09-01 23:46:44 +0530240 }
241 catch (const std::invalid_argument& e)
242 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700243 lg2::error("Exception: {MSG}", "MSG", e.what());
AppaRao Puli9613ed72018-09-01 23:46:44 +0530244 return;
245 }
246
247 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800248 channelLock{*channelMutex};
AppaRao Puli9613ed72018-09-01 23:46:44 +0530249 // skip updating the values, if this property change originated from IPMI.
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800250 if (signalFlag & (1 << chNum))
AppaRao Puli9613ed72018-09-01 23:46:44 +0530251 {
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800252 signalFlag &= ~(1 << chNum);
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700253 lg2::debug("Request originated from IPMI so ignoring signal");
AppaRao Puli9613ed72018-09-01 23:46:44 +0530254 return;
255 }
256
257 // Update both volatile & Non-volatile, if there is mismatch.
258 // as property change other than IPMI, has to update both volatile &
259 // non-volatile data.
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800260 checkAndReloadVolatileData();
261 checkAndReloadNVData();
262 if (channelData[chNum].chAccess.chNonVolatileData.privLimit != intfPriv)
AppaRao Puli9613ed72018-09-01 23:46:44 +0530263 {
264 // Update NV data
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800265 channelData[chNum].chAccess.chNonVolatileData.privLimit = intfPriv;
266 if (writeChannelPersistData() != 0)
AppaRao Puli9613ed72018-09-01 23:46:44 +0530267 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700268 lg2::error("Failed to update the persist data file");
AppaRao Puli9613ed72018-09-01 23:46:44 +0530269 return;
270 }
271
272 // Update Volatile data
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800273 if (channelData[chNum].chAccess.chVolatileData.privLimit != intfPriv)
AppaRao Puli9613ed72018-09-01 23:46:44 +0530274 {
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800275 channelData[chNum].chAccess.chVolatileData.privLimit = intfPriv;
276 if (writeChannelVolatileData() != 0)
AppaRao Puli9613ed72018-09-01 23:46:44 +0530277 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700278 lg2::error("Failed to update the volatile data file");
AppaRao Puli9613ed72018-09-01 23:46:44 +0530279 return;
280 }
281 }
282 }
283
284 return;
285}
286
AppaRao Puli071f3f22018-05-24 16:45:30 +0530287ChannelConfig& getChannelConfigObject()
288{
289 static ChannelConfig channelConfig;
290 return channelConfig;
291}
292
AppaRao Puli9613ed72018-09-01 23:46:44 +0530293ChannelConfig::~ChannelConfig()
294{
295 if (signalHndlrObjectState)
296 {
297 chPropertiesSignal.reset();
Willy Tuac05aa12021-11-16 21:15:58 -0800298 chInterfaceAddedSignal.reset();
299 chInterfaceRemovedSignal.reset();
AppaRao Puli9613ed72018-09-01 23:46:44 +0530300 sigHndlrLock.unlock();
301 }
302}
303
AppaRao Puli071f3f22018-05-24 16:45:30 +0530304ChannelConfig::ChannelConfig() : bus(ipmid_get_sd_bus_connection())
305{
306 std::ofstream mutexCleanUpFile;
307 mutexCleanUpFile.open(ipmiChMutexCleanupLockFile,
308 std::ofstream::out | std::ofstream::app);
309 if (!mutexCleanUpFile.good())
310 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700311 lg2::debug("Unable to open mutex cleanup file");
AppaRao Puli071f3f22018-05-24 16:45:30 +0530312 return;
313 }
314 mutexCleanUpFile.close();
315 mutexCleanupLock =
316 boost::interprocess::file_lock(ipmiChMutexCleanupLockFile);
317 if (mutexCleanupLock.try_lock())
318 {
319 boost::interprocess::named_recursive_mutex::remove(ipmiChannelMutex);
320 channelMutex =
321 std::make_unique<boost::interprocess::named_recursive_mutex>(
322 boost::interprocess::open_or_create, ipmiChannelMutex);
323 mutexCleanupLock.lock_sharable();
324 }
325 else
326 {
327 mutexCleanupLock.lock_sharable();
328 channelMutex =
329 std::make_unique<boost::interprocess::named_recursive_mutex>(
330 boost::interprocess::open_or_create, ipmiChannelMutex);
331 }
332
333 initChannelPersistData();
AppaRao Puli9613ed72018-09-01 23:46:44 +0530334
335 sigHndlrLock = boost::interprocess::file_lock(channelNvDataFilename);
George Liu1a2e1502022-07-08 12:20:19 +0800336 // Register it for single object and single process either netipmid /
AppaRao Puli9613ed72018-09-01 23:46:44 +0530337 // host-ipmid
338 if (chPropertiesSignal == nullptr && sigHndlrLock.try_lock())
339 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700340 lg2::debug("Registering channel signal handler.");
AppaRao Puli9613ed72018-09-01 23:46:44 +0530341 chPropertiesSignal = std::make_unique<sdbusplus::bus::match_t>(
342 bus,
343 sdbusplus::bus::match::rules::path_namespace(
344 networkIntfObjectBasePath) +
345 sdbusplus::bus::match::rules::type::signal() +
346 sdbusplus::bus::match::rules::member(propertiesChangedSignal) +
347 sdbusplus::bus::match::rules::interface(
348 dBusPropertiesInterface) +
349 sdbusplus::bus::match::rules::argN(0, networkChConfigIntfName),
Patrick Williams5d82f472022-07-22 19:26:53 -0500350 [&](sdbusplus::message_t& msg) {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500351 DbusChObjProperties props;
352 std::string iface;
353 std::string path = msg.get_path();
354 msg.read(iface, props);
355 processChAccessPropChange(path, props);
Patrick Williams369824e2023-10-20 11:18:23 -0500356 });
AppaRao Puli9613ed72018-09-01 23:46:44 +0530357 signalHndlrObjectState = true;
Willy Tuac05aa12021-11-16 21:15:58 -0800358
359 chInterfaceAddedSignal = std::make_unique<sdbusplus::bus::match_t>(
360 bus,
361 sdbusplus::bus::match::rules::type::signal() +
362 sdbusplus::bus::match::rules::member(interfaceAddedSignal) +
363 sdbusplus::bus::match::rules::argNpath(
364 0, std::string(networkIntfObjectBasePath) + "/"),
Patrick Williams5d82f472022-07-22 19:26:53 -0500365 [&](sdbusplus::message_t&) { initChannelPersistData(); });
Willy Tuac05aa12021-11-16 21:15:58 -0800366
367 chInterfaceRemovedSignal = std::make_unique<sdbusplus::bus::match_t>(
368 bus,
369 sdbusplus::bus::match::rules::type::signal() +
370 sdbusplus::bus::match::rules::member(interfaceRemovedSignal) +
371 sdbusplus::bus::match::rules::argNpath(
372 0, std::string(networkIntfObjectBasePath) + "/"),
Patrick Williams5d82f472022-07-22 19:26:53 -0500373 [&](sdbusplus::message_t&) { initChannelPersistData(); });
AppaRao Puli9613ed72018-09-01 23:46:44 +0530374 }
375}
376
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530377bool ChannelConfig::isValidChannel(const uint8_t chNum)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530378{
Meera-Kattac1789482021-05-18 09:53:26 +0000379 if (chNum >= maxIpmiChannels)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530380 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700381 lg2::debug("Invalid channel ID - Out of range");
AppaRao Puli071f3f22018-05-24 16:45:30 +0530382 return false;
383 }
384
385 if (channelData[chNum].isChValid == false)
386 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700387 lg2::debug("Channel is not valid");
AppaRao Puli071f3f22018-05-24 16:45:30 +0530388 }
389
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800390 return channelData[chNum].isChValid;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530391}
392
393EChannelSessSupported
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530394 ChannelConfig::getChannelSessionSupport(const uint8_t chNum)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530395{
396 EChannelSessSupported chSessSupport =
397 (EChannelSessSupported)channelData[chNum].chInfo.sessionSupported;
398 return chSessSupport;
399}
400
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530401bool ChannelConfig::isValidAuthType(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530402 const EAuthType& authType)
403{
404 if ((authType < EAuthType::md2) || (authType > EAuthType::oem))
405 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700406 lg2::debug("Invalid authentication type");
AppaRao Puli071f3f22018-05-24 16:45:30 +0530407 return false;
408 }
409
410 uint8_t authTypeSupported = channelData[chNum].chInfo.authTypeSupported;
411 if (!(authTypeSupported & (1 << static_cast<uint8_t>(authType))))
412 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700413 lg2::debug("Authentication type is not supported.");
AppaRao Puli071f3f22018-05-24 16:45:30 +0530414 return false;
415 }
416
417 return true;
418}
419
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530420int ChannelConfig::getChannelActiveSessions(const uint8_t chNum)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530421{
422 // TODO: TEMPORARY FIX
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800423 // Channels active session count is managed separately
AppaRao Puli071f3f22018-05-24 16:45:30 +0530424 // by monitoring channel session which includes LAN and
425 // RAKP layer changes. This will be updated, once the
426 // authentication part is implemented.
427 return channelData[chNum].activeSessCount;
428}
429
Vernon Mauery58317122018-11-28 11:02:43 -0800430size_t ChannelConfig::getChannelMaxTransferSize(uint8_t chNum)
431{
432 return channelData[chNum].maxTransferSize;
433}
434
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000435Cc ChannelConfig::getChannelInfo(const uint8_t chNum, ChannelInfo& chInfo)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530436{
437 if (!isValidChannel(chNum))
438 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700439 lg2::debug("Invalid channel");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000440 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530441 }
442
443 std::copy_n(reinterpret_cast<uint8_t*>(&channelData[chNum].chInfo),
444 sizeof(channelData[chNum].chInfo),
445 reinterpret_cast<uint8_t*>(&chInfo));
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000446 return ccSuccess;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530447}
448
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000449Cc ChannelConfig::getChannelAccessData(const uint8_t chNum,
450 ChannelAccess& chAccessData)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530451{
452 if (!isValidChannel(chNum))
453 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700454 lg2::debug("Invalid channel");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000455 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530456 }
457
458 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
459 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700460 lg2::debug("Session-less channel doesn't have access data.");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000461 return ccActionNotSupportedForChannel;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530462 }
463
464 if (checkAndReloadVolatileData() != 0)
465 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000466 return ccUnspecifiedError;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530467 }
468
469 std::copy_n(
470 reinterpret_cast<uint8_t*>(&channelData[chNum].chAccess.chVolatileData),
471 sizeof(channelData[chNum].chAccess.chVolatileData),
472 reinterpret_cast<uint8_t*>(&chAccessData));
473
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000474 return ccSuccess;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530475}
476
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000477Cc ChannelConfig::setChannelAccessData(const uint8_t chNum,
478 const ChannelAccess& chAccessData,
479 const uint8_t setFlag)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530480{
481 if (!isValidChannel(chNum))
482 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700483 lg2::debug("Invalid channel");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000484 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530485 }
486
487 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
488 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700489 lg2::debug("Session-less channel doesn't have access data.");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000490 return ccActionNotSupportedForChannel;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530491 }
492
jayaprakash Mutyala0e2dbee2019-12-26 13:03:04 +0000493 if ((setFlag & setAccessMode) &&
494 (!isValidAccessMode(chAccessData.accessMode)))
AppaRao Puli071f3f22018-05-24 16:45:30 +0530495 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700496 lg2::debug("Invalid access mode specified");
jayaprakash Mutyala0e2dbee2019-12-26 13:03:04 +0000497 return ccAccessModeNotSupportedForChannel;
498 }
499 if ((setFlag & setPrivLimit) && (!isValidPrivLimit(chAccessData.privLimit)))
500 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700501 lg2::debug("Invalid privilege limit specified");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000502 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530503 }
504
505 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
506 channelLock{*channelMutex};
507
508 if (checkAndReloadVolatileData() != 0)
509 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000510 return ccUnspecifiedError;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530511 }
512
513 if (setFlag & setAccessMode)
514 {
515 channelData[chNum].chAccess.chVolatileData.accessMode =
516 chAccessData.accessMode;
517 }
518 if (setFlag & setUserAuthEnabled)
519 {
520 channelData[chNum].chAccess.chVolatileData.userAuthDisabled =
521 chAccessData.userAuthDisabled;
522 }
523 if (setFlag & setMsgAuthEnabled)
524 {
525 channelData[chNum].chAccess.chVolatileData.perMsgAuthDisabled =
526 chAccessData.perMsgAuthDisabled;
527 }
528 if (setFlag & setAlertingEnabled)
529 {
530 channelData[chNum].chAccess.chVolatileData.alertingDisabled =
531 chAccessData.alertingDisabled;
532 }
533 if (setFlag & setPrivLimit)
534 {
535 channelData[chNum].chAccess.chVolatileData.privLimit =
536 chAccessData.privLimit;
537 }
538
539 // Write Volatile data to file
540 if (writeChannelVolatileData() != 0)
541 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700542 lg2::debug("Failed to update the channel volatile data");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000543 return ccUnspecifiedError;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530544 }
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000545 return ccSuccess;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530546}
547
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000548Cc ChannelConfig::getChannelAccessPersistData(const uint8_t chNum,
549 ChannelAccess& chAccessData)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530550{
551 if (!isValidChannel(chNum))
552 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700553 lg2::debug("Invalid channel");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000554 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530555 }
556
557 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
558 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700559 lg2::debug("Session-less channel doesn't have access data.");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000560 return ccActionNotSupportedForChannel;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530561 }
562
563 if (checkAndReloadNVData() != 0)
564 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000565 return ccUnspecifiedError;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530566 }
567
568 std::copy_n(reinterpret_cast<uint8_t*>(
569 &channelData[chNum].chAccess.chNonVolatileData),
570 sizeof(channelData[chNum].chAccess.chNonVolatileData),
571 reinterpret_cast<uint8_t*>(&chAccessData));
572
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000573 return ccSuccess;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530574}
575
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000576Cc ChannelConfig::setChannelAccessPersistData(const uint8_t chNum,
577 const ChannelAccess& chAccessData,
578 const uint8_t setFlag)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530579{
580 if (!isValidChannel(chNum))
581 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700582 lg2::debug("Invalid channel");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000583 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530584 }
585
586 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
587 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700588 lg2::debug("Session-less channel doesn't have access data.");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000589 return ccActionNotSupportedForChannel;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530590 }
591
jayaprakash Mutyala0e2dbee2019-12-26 13:03:04 +0000592 if ((setFlag & setAccessMode) &&
593 (!isValidAccessMode(chAccessData.accessMode)))
AppaRao Puli071f3f22018-05-24 16:45:30 +0530594 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700595 lg2::debug("Invalid access mode specified");
jayaprakash Mutyala0e2dbee2019-12-26 13:03:04 +0000596 return ccAccessModeNotSupportedForChannel;
597 }
598 if ((setFlag & setPrivLimit) && (!isValidPrivLimit(chAccessData.privLimit)))
599 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700600 lg2::debug("Invalid privilege limit specified");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000601 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530602 }
603
604 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
605 channelLock{*channelMutex};
606
607 if (checkAndReloadNVData() != 0)
608 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000609 return ccUnspecifiedError;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530610 }
611
612 if (setFlag & setAccessMode)
613 {
614 channelData[chNum].chAccess.chNonVolatileData.accessMode =
615 chAccessData.accessMode;
616 }
617 if (setFlag & setUserAuthEnabled)
618 {
619 channelData[chNum].chAccess.chNonVolatileData.userAuthDisabled =
620 chAccessData.userAuthDisabled;
621 }
622 if (setFlag & setMsgAuthEnabled)
623 {
624 channelData[chNum].chAccess.chNonVolatileData.perMsgAuthDisabled =
625 chAccessData.perMsgAuthDisabled;
626 }
627 if (setFlag & setAlertingEnabled)
628 {
629 channelData[chNum].chAccess.chNonVolatileData.alertingDisabled =
630 chAccessData.alertingDisabled;
631 }
632 if (setFlag & setPrivLimit)
633 {
AppaRao Puli9613ed72018-09-01 23:46:44 +0530634 // Send Update to network channel config interfaces over dbus
AppaRao Puli9613ed72018-09-01 23:46:44 +0530635 std::string privStr = convertToPrivLimitString(chAccessData.privLimit);
Richard Marian Thomaiyar73906b92019-01-04 23:48:02 +0530636 std::string networkIntfObj = std::string(networkIntfObjectBasePath) +
637 "/" + channelData[chNum].chName;
AppaRao Puli9613ed72018-09-01 23:46:44 +0530638 try
639 {
Johnathan Manteyf92261d2018-12-10 15:49:34 -0800640 if (0 != setDbusProperty(networkIntfServiceName, networkIntfObj,
641 networkChConfigIntfName,
AppaRao Puli9613ed72018-09-01 23:46:44 +0530642 privilegePropertyString, privStr))
643 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700644 lg2::debug("Network interface '{INTERFACE}' does not exist",
645 "INTERFACE", channelData[chNum].chName);
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000646 return ccUnspecifiedError;
AppaRao Puli9613ed72018-09-01 23:46:44 +0530647 }
648 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500649 catch (const sdbusplus::exception_t& e)
AppaRao Puli9613ed72018-09-01 23:46:44 +0530650 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700651 lg2::error("Exception: Network interface does not exist");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000652 return ccInvalidFieldRequest;
AppaRao Puli9613ed72018-09-01 23:46:44 +0530653 }
654 signalFlag |= (1 << chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530655 channelData[chNum].chAccess.chNonVolatileData.privLimit =
656 chAccessData.privLimit;
657 }
658
659 // Write persistent data to file
660 if (writeChannelPersistData() != 0)
661 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700662 lg2::debug("Failed to update the presist data file");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000663 return ccUnspecifiedError;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530664 }
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000665 return ccSuccess;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530666}
667
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000668Cc ChannelConfig::getChannelAuthTypeSupported(const uint8_t chNum,
669 uint8_t& authTypeSupported)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530670{
671 if (!isValidChannel(chNum))
672 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700673 lg2::debug("Invalid channel");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000674 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530675 }
676
677 authTypeSupported = channelData[chNum].chInfo.authTypeSupported;
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000678 return ccSuccess;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530679}
680
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000681Cc ChannelConfig::getChannelEnabledAuthType(const uint8_t chNum,
682 const uint8_t priv,
683 EAuthType& authType)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530684{
685 if (!isValidChannel(chNum))
686 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700687 lg2::debug("Invalid channel");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000688 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530689 }
690
691 if (getChannelSessionSupport(chNum) == EChannelSessSupported::none)
692 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700693 lg2::debug("Sessionless channel doesn't have access data.");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000694 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530695 }
696
697 if (!isValidPrivLimit(priv))
698 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700699 lg2::debug("Invalid privilege specified.");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000700 return ccInvalidFieldRequest;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530701 }
702
703 // TODO: Hardcoded for now. Need to implement.
704 authType = EAuthType::none;
705
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000706 return ccSuccess;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530707}
708
709std::time_t ChannelConfig::getUpdatedFileTime(const std::string& fileName)
710{
711 struct stat fileStat;
712 if (stat(fileName.c_str(), &fileStat) != 0)
713 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700714 lg2::debug("Error in getting last updated time stamp");
AppaRao Puli071f3f22018-05-24 16:45:30 +0530715 return -EIO;
716 }
717 return fileStat.st_mtime;
718}
719
720EChannelAccessMode
721 ChannelConfig::convertToAccessModeIndex(const std::string& mode)
722{
723 auto iter = std::find(accessModeList.begin(), accessModeList.end(), mode);
724 if (iter == accessModeList.end())
725 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700726 lg2::error("Invalid access mode: {MODE_STR}", "MODE_STR", mode);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530727 throw std::invalid_argument("Invalid access mode.");
728 }
729
730 return static_cast<EChannelAccessMode>(
731 std::distance(accessModeList.begin(), iter));
732}
733
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530734std::string ChannelConfig::convertToAccessModeString(const uint8_t value)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530735{
736 if (accessModeList.size() <= value)
737 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700738 lg2::error("Invalid access mode: {MODE_IDX}", "MODE_IDX", value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530739 throw std::invalid_argument("Invalid access mode.");
740 }
741
742 return accessModeList.at(value);
743}
744
745CommandPrivilege
746 ChannelConfig::convertToPrivLimitIndex(const std::string& value)
747{
748 auto iter = std::find(privList.begin(), privList.end(), value);
749 if (iter == privList.end())
750 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700751 lg2::error("Invalid privilege: {PRIV_STR}", "PRIV_STR", value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530752 throw std::invalid_argument("Invalid privilege.");
753 }
754
755 return static_cast<CommandPrivilege>(std::distance(privList.begin(), iter));
756}
757
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530758std::string ChannelConfig::convertToPrivLimitString(const uint8_t value)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530759{
760 if (privList.size() <= value)
761 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700762 lg2::error("Invalid privilege: {PRIV_IDX.", "PRIV_IDX", value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530763 throw std::invalid_argument("Invalid privilege.");
764 }
765
766 return privList.at(value);
767}
768
769EChannelSessSupported
770 ChannelConfig::convertToSessionSupportIndex(const std::string& value)
771{
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500772 auto iter = std::find(sessionSupportList.begin(), sessionSupportList.end(),
773 value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530774 if (iter == sessionSupportList.end())
775 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700776 lg2::error("Invalid session supported: {SESS_STR}", "SESS_STR", value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530777 throw std::invalid_argument("Invalid session supported.");
778 }
779
780 return static_cast<EChannelSessSupported>(
781 std::distance(sessionSupportList.begin(), iter));
782}
783
784EChannelMediumType
785 ChannelConfig::convertToMediumTypeIndex(const std::string& value)
786{
787 std::unordered_map<std::string, EChannelMediumType>::iterator it =
788 mediumTypeMap.find(value);
789 if (it == mediumTypeMap.end())
790 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700791 lg2::error("Invalid medium type: {MEDIUM_STR}", "MEDIUM_STR", value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530792 throw std::invalid_argument("Invalid medium type.");
793 }
794
795 return static_cast<EChannelMediumType>(it->second);
796}
797
798EChannelProtocolType
799 ChannelConfig::convertToProtocolTypeIndex(const std::string& value)
800{
801 std::unordered_map<std::string, EChannelProtocolType>::iterator it =
802 protocolTypeMap.find(value);
803 if (it == protocolTypeMap.end())
804 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700805 lg2::error("Invalid protocol type: {PROTO_STR}", "PROTO_STR", value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530806 throw std::invalid_argument("Invalid protocol type.");
807 }
808
809 return static_cast<EChannelProtocolType>(it->second);
810}
811
812Json ChannelConfig::readJsonFile(const std::string& configFile)
813{
814 std::ifstream jsonFile(configFile);
815 if (!jsonFile.good())
816 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700817 lg2::info("JSON file '{FILE_NAME}' not found", "FILE_NAME", configFile);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530818 return nullptr;
819 }
820
821 Json data = nullptr;
822 try
823 {
824 data = Json::parse(jsonFile, nullptr, false);
825 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500826 catch (const Json::parse_error& e)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530827 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700828 lg2::debug("Corrupted channel config: {MSG}", "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +0530829 throw std::runtime_error("Corrupted channel config file");
830 }
831
832 return data;
833}
834
835int ChannelConfig::writeJsonFile(const std::string& configFile,
836 const Json& jsonData)
837{
Richard Marian Thomaiyar687df402019-05-09 00:16:53 +0530838 const std::string tmpFile = configFile + "_tmp";
839 int fd = open(tmpFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_SYNC,
840 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
841 if (fd < 0)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530842 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700843 lg2::error("Error in creating json file '{FILE_NAME}'", "FILE_NAME",
844 tmpFile);
Richard Marian Thomaiyar687df402019-05-09 00:16:53 +0530845 return -EIO;
846 }
847 const auto& writeData = jsonData.dump();
848 if (write(fd, writeData.c_str(), writeData.size()) !=
849 static_cast<ssize_t>(writeData.size()))
850 {
851 close(fd);
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700852 lg2::error("Error in writing configuration file '{FILE_NAME}'",
853 "FILE_NAME", tmpFile);
Richard Marian Thomaiyar687df402019-05-09 00:16:53 +0530854 return -EIO;
855 }
856 close(fd);
857
858 if (std::rename(tmpFile.c_str(), configFile.c_str()) != 0)
859 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700860 lg2::error("Error in renaming temporary data file '{FILE_NAME}'",
861 "FILE_NAME", tmpFile);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530862 return -EIO;
863 }
864
AppaRao Puli071f3f22018-05-24 16:45:30 +0530865 return 0;
866}
867
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530868void ChannelConfig::setDefaultChannelConfig(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530869 const std::string& chName)
870{
871 channelData[chNum].chName = chName;
872 channelData[chNum].chID = chNum;
873 channelData[chNum].isChValid = false;
874 channelData[chNum].activeSessCount = 0;
Johnathan Manteyfd61fc32021-04-08 11:05:38 -0700875 channelData[chNum].isManagementNIC = false;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530876
877 channelData[chNum].chInfo.mediumType = defaultMediumType;
878 channelData[chNum].chInfo.protocolType = defaultProtocolType;
879 channelData[chNum].chInfo.sessionSupported = defaultSessionSupported;
880 channelData[chNum].chInfo.isIpmi = defaultIsIpmiState;
881 channelData[chNum].chInfo.authTypeSupported = defaultAuthType;
882}
883
Johnathan Manteyfd61fc32021-04-08 11:05:38 -0700884uint8_t ChannelConfig::getManagementNICID()
885{
886 static bool idFound = false;
887 static uint8_t id = 0;
888
889 if (idFound)
890 {
891 return id;
892 }
893
894 for (uint8_t chIdx = 0; chIdx < maxIpmiChannels; chIdx++)
895 {
896 if (channelData[chIdx].isManagementNIC)
897 {
898 id = chIdx;
899 idFound = true;
900 break;
901 }
902 }
903
904 if (!idFound)
905 {
906 id = static_cast<uint8_t>(EChannelID::chanLan1);
907 idFound = true;
908 }
909 return id;
910}
911
AppaRao Puli071f3f22018-05-24 16:45:30 +0530912int ChannelConfig::loadChannelConfig()
913{
914 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
915 channelLock{*channelMutex};
916
917 Json data = readJsonFile(channelConfigDefaultFilename);
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800918 if (data.empty())
AppaRao Puli071f3f22018-05-24 16:45:30 +0530919 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700920 lg2::debug("Error in opening IPMI Channel data file");
AppaRao Puli071f3f22018-05-24 16:45:30 +0530921 return -EIO;
922 }
923
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800924 channelData.fill(ChannelProperties{});
925
Johnathan Mantey0a2abc82021-02-18 12:39:12 -0800926 // Collect the list of NIC interfaces connected to the BMC. Use this
927 // information to only add IPMI channels that have active NIC interfaces.
Snehalatha Venkatesh55f5d532021-07-13 11:06:36 +0000928 struct ifaddrs *ifaddr = nullptr, *ifa = nullptr;
Johnathan Mantey0a2abc82021-02-18 12:39:12 -0800929 if (int err = getifaddrs(&ifaddr); err < 0)
930 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700931 lg2::debug("Unable to acquire network interfaces");
Johnathan Mantey0a2abc82021-02-18 12:39:12 -0800932 return -EIO;
933 }
934
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800935 for (int chNum = 0; chNum < maxIpmiChannels; chNum++)
AppaRao Puli071f3f22018-05-24 16:45:30 +0530936 {
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800937 try
AppaRao Puli071f3f22018-05-24 16:45:30 +0530938 {
AppaRao Puli071f3f22018-05-24 16:45:30 +0530939 std::string chKey = std::to_string(chNum);
940 Json jsonChData = data[chKey].get<Json>();
941 if (jsonChData.is_null())
942 {
AppaRao Puli071f3f22018-05-24 16:45:30 +0530943 // If user didn't want to configure specific channel (say
944 // reserved channel), then load that index with default values.
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800945 setDefaultChannelConfig(chNum, defaultChannelName);
946 continue;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530947 }
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800948 Json jsonChInfo = jsonChData[channelInfoString].get<Json>();
949 if (jsonChInfo.is_null())
AppaRao Puli071f3f22018-05-24 16:45:30 +0530950 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -0700951 lg2::error("Invalid/corrupted channel config file");
Johnathan Mantey0a2abc82021-02-18 12:39:12 -0800952 freeifaddrs(ifaddr);
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800953 return -EBADMSG;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530954 }
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800955
Johnathan Mantey0a2abc82021-02-18 12:39:12 -0800956 bool channelFound = true;
957 // Confirm the LAN channel is present
958 if (jsonChInfo[mediumTypeString].get<std::string>() == "lan-802.3")
959 {
960 channelFound = false;
961 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
962 {
963 if (jsonChData[nameString].get<std::string>() ==
964 ifa->ifa_name)
965 {
966 channelFound = true;
967 break;
968 }
969 }
970 }
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800971 ChannelProperties& chData = channelData[chNum];
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800972 chData.chID = chNum;
Johnathan Mantey0a2abc82021-02-18 12:39:12 -0800973 chData.chName = jsonChData[nameString].get<std::string>();
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500974 chData.isChValid = channelFound &&
975 jsonChData[isValidString].get<bool>();
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800976 chData.activeSessCount = jsonChData.value(activeSessionsString, 0);
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500977 chData.maxTransferSize = jsonChData.value(maxTransferSizeString,
978 smallChannelSize);
Johnathan Manteyfd61fc32021-04-08 11:05:38 -0700979 if (jsonChData.count(isManagementNIC) != 0)
980 {
981 chData.isManagementNIC =
982 jsonChData[isManagementNIC].get<bool>();
983 }
984
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800985 std::string medTypeStr =
986 jsonChInfo[mediumTypeString].get<std::string>();
987 chData.chInfo.mediumType =
988 static_cast<uint8_t>(convertToMediumTypeIndex(medTypeStr));
989 std::string protoTypeStr =
990 jsonChInfo[protocolTypeString].get<std::string>();
991 chData.chInfo.protocolType =
992 static_cast<uint8_t>(convertToProtocolTypeIndex(protoTypeStr));
993 std::string sessStr =
994 jsonChInfo[sessionSupportedString].get<std::string>();
995 chData.chInfo.sessionSupported =
996 static_cast<uint8_t>(convertToSessionSupportIndex(sessStr));
997 chData.chInfo.isIpmi = jsonChInfo[isIpmiString].get<bool>();
998 chData.chInfo.authTypeSupported = defaultAuthType;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530999 }
Johnathan Mantey4c0435a2018-12-11 13:17:55 -08001000 catch (const Json::exception& e)
1001 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001002 lg2::debug("Json Exception caught: {MSG}", "MSG", e.what());
Johnathan Mantey0a2abc82021-02-18 12:39:12 -08001003 freeifaddrs(ifaddr);
1004
Johnathan Mantey4c0435a2018-12-11 13:17:55 -08001005 return -EBADMSG;
1006 }
1007 catch (const std::invalid_argument& e)
1008 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001009 lg2::error("Corrupted config: {MSG}", "MSG", e.what());
Johnathan Mantey0a2abc82021-02-18 12:39:12 -08001010 freeifaddrs(ifaddr);
Johnathan Mantey4c0435a2018-12-11 13:17:55 -08001011 return -EBADMSG;
1012 }
AppaRao Puli071f3f22018-05-24 16:45:30 +05301013 }
Johnathan Mantey0a2abc82021-02-18 12:39:12 -08001014 freeifaddrs(ifaddr);
AppaRao Puli071f3f22018-05-24 16:45:30 +05301015
1016 return 0;
1017}
1018
1019int ChannelConfig::readChannelVolatileData()
1020{
1021 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1022 channelLock{*channelMutex};
1023
1024 Json data = readJsonFile(channelVolatileDataFilename);
1025 if (data == nullptr)
1026 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001027 lg2::debug("Error in opening IPMI Channel data file");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301028 return -EIO;
1029 }
AppaRao Puli071f3f22018-05-24 16:45:30 +05301030 try
1031 {
1032 // Fill in global structure
1033 for (auto it = data.begin(); it != data.end(); ++it)
1034 {
1035 std::string chKey = it.key();
1036 uint8_t chNum = std::stoi(chKey, nullptr, 10);
Meera-Kattac1789482021-05-18 09:53:26 +00001037 if (chNum >= maxIpmiChannels)
AppaRao Puli071f3f22018-05-24 16:45:30 +05301038 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001039 lg2::debug("Invalid channel access entry in config file");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301040 throw std::out_of_range("Out of range - channel number");
1041 }
1042 Json jsonChData = it.value();
1043 if (!jsonChData.is_null())
1044 {
1045 std::string accModeStr =
1046 jsonChData[accessModeString].get<std::string>();
1047 channelData[chNum].chAccess.chVolatileData.accessMode =
1048 static_cast<uint8_t>(convertToAccessModeIndex(accModeStr));
1049 channelData[chNum].chAccess.chVolatileData.userAuthDisabled =
1050 jsonChData[userAuthDisabledString].get<bool>();
1051 channelData[chNum].chAccess.chVolatileData.perMsgAuthDisabled =
1052 jsonChData[perMsgAuthDisabledString].get<bool>();
1053 channelData[chNum].chAccess.chVolatileData.alertingDisabled =
1054 jsonChData[alertingDisabledString].get<bool>();
1055 std::string privStr =
1056 jsonChData[privLimitString].get<std::string>();
1057 channelData[chNum].chAccess.chVolatileData.privLimit =
1058 static_cast<uint8_t>(convertToPrivLimitIndex(privStr));
1059 }
1060 else
1061 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001062 lg2::error(
1063 "Invalid/corrupted volatile channel access file '{FILE}'",
1064 "FILE", channelVolatileDataFilename);
AppaRao Puli071f3f22018-05-24 16:45:30 +05301065 throw std::runtime_error(
1066 "Corrupted volatile channel access file");
1067 }
1068 }
1069 }
1070 catch (const Json::exception& e)
1071 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001072 lg2::debug("Json Exception caught: {MSG}", "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +05301073 throw std::runtime_error("Corrupted volatile channel access file");
1074 }
1075 catch (const std::invalid_argument& e)
1076 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001077 lg2::error("Corrupted config: {MSG}", "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +05301078 throw std::runtime_error("Corrupted volatile channel access file");
1079 }
1080
1081 // Update the timestamp
1082 voltFileLastUpdatedTime = getUpdatedFileTime(channelVolatileDataFilename);
1083 return 0;
1084}
1085
1086int ChannelConfig::readChannelPersistData()
1087{
1088 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1089 channelLock{*channelMutex};
1090
1091 Json data = readJsonFile(channelNvDataFilename);
1092 if (data == nullptr)
1093 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001094 lg2::debug("Error in opening IPMI Channel data file");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301095 return -EIO;
1096 }
AppaRao Puli071f3f22018-05-24 16:45:30 +05301097 try
1098 {
1099 // Fill in global structure
1100 for (auto it = data.begin(); it != data.end(); ++it)
1101 {
1102 std::string chKey = it.key();
1103 uint8_t chNum = std::stoi(chKey, nullptr, 10);
Meera-Kattac1789482021-05-18 09:53:26 +00001104 if (chNum >= maxIpmiChannels)
AppaRao Puli071f3f22018-05-24 16:45:30 +05301105 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001106 lg2::debug("Invalid channel access entry in config file");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301107 throw std::out_of_range("Out of range - channel number");
1108 }
1109 Json jsonChData = it.value();
1110 if (!jsonChData.is_null())
1111 {
1112 std::string accModeStr =
1113 jsonChData[accessModeString].get<std::string>();
1114 channelData[chNum].chAccess.chNonVolatileData.accessMode =
1115 static_cast<uint8_t>(convertToAccessModeIndex(accModeStr));
1116 channelData[chNum].chAccess.chNonVolatileData.userAuthDisabled =
1117 jsonChData[userAuthDisabledString].get<bool>();
1118 channelData[chNum]
1119 .chAccess.chNonVolatileData.perMsgAuthDisabled =
1120 jsonChData[perMsgAuthDisabledString].get<bool>();
1121 channelData[chNum].chAccess.chNonVolatileData.alertingDisabled =
1122 jsonChData[alertingDisabledString].get<bool>();
1123 std::string privStr =
1124 jsonChData[privLimitString].get<std::string>();
1125 channelData[chNum].chAccess.chNonVolatileData.privLimit =
1126 static_cast<uint8_t>(convertToPrivLimitIndex(privStr));
1127 }
1128 else
1129 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001130 lg2::error("Invalid/corrupted nv channel access file {FILE}",
1131 "FILE", channelNvDataFilename);
AppaRao Puli071f3f22018-05-24 16:45:30 +05301132 throw std::runtime_error("Corrupted nv channel access file");
1133 }
1134 }
1135 }
1136 catch (const Json::exception& e)
1137 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001138 lg2::debug("Json Exception caught: {MSG}", "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +05301139 throw std::runtime_error("Corrupted nv channel access file");
1140 }
1141 catch (const std::invalid_argument& e)
1142 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001143 lg2::error("Corrupted config: {MSG}", "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +05301144 throw std::runtime_error("Corrupted nv channel access file");
1145 }
1146
1147 // Update the timestamp
1148 nvFileLastUpdatedTime = getUpdatedFileTime(channelNvDataFilename);
1149 return 0;
1150}
1151
1152int ChannelConfig::writeChannelVolatileData()
1153{
1154 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1155 channelLock{*channelMutex};
1156 Json outData;
1157
1158 try
1159 {
1160 for (uint8_t chNum = 0; chNum < maxIpmiChannels; chNum++)
1161 {
1162 if (getChannelSessionSupport(chNum) != EChannelSessSupported::none)
1163 {
1164 Json jsonObj;
1165 std::string chKey = std::to_string(chNum);
1166 std::string accModeStr = convertToAccessModeString(
1167 channelData[chNum].chAccess.chVolatileData.accessMode);
1168 jsonObj[accessModeString] = accModeStr;
1169 jsonObj[userAuthDisabledString] =
1170 channelData[chNum].chAccess.chVolatileData.userAuthDisabled;
1171 jsonObj[perMsgAuthDisabledString] =
1172 channelData[chNum]
1173 .chAccess.chVolatileData.perMsgAuthDisabled;
1174 jsonObj[alertingDisabledString] =
1175 channelData[chNum].chAccess.chVolatileData.alertingDisabled;
1176 std::string privStr = convertToPrivLimitString(
1177 channelData[chNum].chAccess.chVolatileData.privLimit);
1178 jsonObj[privLimitString] = privStr;
1179
1180 outData[chKey] = jsonObj;
1181 }
1182 }
1183 }
1184 catch (const std::invalid_argument& e)
1185 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001186 lg2::error("Corrupted config: {MSG}", "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +05301187 return -EINVAL;
1188 }
1189
1190 if (writeJsonFile(channelVolatileDataFilename, outData) != 0)
1191 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001192 lg2::debug("Error in write JSON data to file");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301193 return -EIO;
1194 }
1195
1196 // Update the timestamp
1197 voltFileLastUpdatedTime = getUpdatedFileTime(channelVolatileDataFilename);
1198 return 0;
1199}
1200
1201int ChannelConfig::writeChannelPersistData()
1202{
1203 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1204 channelLock{*channelMutex};
1205 Json outData;
1206
1207 try
1208 {
1209 for (uint8_t chNum = 0; chNum < maxIpmiChannels; chNum++)
1210 {
1211 if (getChannelSessionSupport(chNum) != EChannelSessSupported::none)
1212 {
1213 Json jsonObj;
1214 std::string chKey = std::to_string(chNum);
1215 std::string accModeStr = convertToAccessModeString(
1216 channelData[chNum].chAccess.chNonVolatileData.accessMode);
1217 jsonObj[accessModeString] = accModeStr;
1218 jsonObj[userAuthDisabledString] =
1219 channelData[chNum]
1220 .chAccess.chNonVolatileData.userAuthDisabled;
1221 jsonObj[perMsgAuthDisabledString] =
1222 channelData[chNum]
1223 .chAccess.chNonVolatileData.perMsgAuthDisabled;
1224 jsonObj[alertingDisabledString] =
1225 channelData[chNum]
1226 .chAccess.chNonVolatileData.alertingDisabled;
1227 std::string privStr = convertToPrivLimitString(
1228 channelData[chNum].chAccess.chNonVolatileData.privLimit);
1229 jsonObj[privLimitString] = privStr;
1230
1231 outData[chKey] = jsonObj;
1232 }
1233 }
1234 }
1235 catch (const std::invalid_argument& e)
1236 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001237 lg2::error("Corrupted config: {MSG}", "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +05301238 return -EINVAL;
1239 }
1240
1241 if (writeJsonFile(channelNvDataFilename, outData) != 0)
1242 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001243 lg2::debug("Error in write JSON data to file");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301244 return -EIO;
1245 }
1246
1247 // Update the timestamp
1248 nvFileLastUpdatedTime = getUpdatedFileTime(channelNvDataFilename);
1249 return 0;
1250}
1251
1252int ChannelConfig::checkAndReloadNVData()
1253{
1254 std::time_t updateTime = getUpdatedFileTime(channelNvDataFilename);
1255 int ret = 0;
1256 if (updateTime != nvFileLastUpdatedTime || updateTime == -EIO)
1257 {
1258 try
1259 {
1260 ret = readChannelPersistData();
1261 }
1262 catch (const std::exception& e)
1263 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001264 lg2::error("Exception caught in readChannelPersistData: {MSG}",
1265 "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +05301266 ret = -EIO;
1267 }
1268 }
1269 return ret;
1270}
1271
1272int ChannelConfig::checkAndReloadVolatileData()
1273{
1274 std::time_t updateTime = getUpdatedFileTime(channelVolatileDataFilename);
1275 int ret = 0;
1276 if (updateTime != voltFileLastUpdatedTime || updateTime == -EIO)
1277 {
1278 try
1279 {
1280 ret = readChannelVolatileData();
1281 }
1282 catch (const std::exception& e)
1283 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001284 lg2::error("Exception caught in readChannelVolatileData: {MSG}",
1285 "MSG", e.what());
AppaRao Puli071f3f22018-05-24 16:45:30 +05301286 ret = -EIO;
1287 }
1288 }
1289 return ret;
1290}
1291
Johnathan Manteyf92261d2018-12-10 15:49:34 -08001292int ChannelConfig::setDbusProperty(const std::string& service,
AppaRao Puli9613ed72018-09-01 23:46:44 +05301293 const std::string& objPath,
1294 const std::string& interface,
1295 const std::string& property,
1296 const DbusVariant& value)
1297{
1298 try
1299 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001300 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
1301 "org.freedesktop.DBus.Properties",
1302 "Set");
AppaRao Puli9613ed72018-09-01 23:46:44 +05301303
1304 method.append(interface, property, value);
1305
1306 auto reply = bus.call(method);
1307 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001308 catch (const sdbusplus::exception_t& e)
AppaRao Puli9613ed72018-09-01 23:46:44 +05301309 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001310 lg2::debug(
1311 "set-property {SERVICE}:{OBJPATH}/{INTERFACE}.{PROP} failed: {MSG}",
1312 "SERVICE", service, "OBJPATH", objPath, "INTERFACE", interface,
1313 "PROP", property);
AppaRao Puli9613ed72018-09-01 23:46:44 +05301314 return -EIO;
1315 }
1316
1317 return 0;
1318}
1319
AppaRao Puli9613ed72018-09-01 23:46:44 +05301320int ChannelConfig::syncNetworkChannelConfig()
1321{
1322 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1323 channelLock{*channelMutex};
1324 bool isUpdated = false;
1325 for (uint8_t chNum = 0; chNum < maxIpmiChannels; chNum++)
1326 {
1327 if (getChannelSessionSupport(chNum) != EChannelSessSupported::none)
1328 {
1329 std::string intfPrivStr;
Jiaqing Zhao826bf662022-11-07 14:33:00 +08001330 uint8_t intfPriv = 0;
AppaRao Puli9613ed72018-09-01 23:46:44 +05301331 try
1332 {
AppaRao Puli9613ed72018-09-01 23:46:44 +05301333 std::string networkIntfObj =
Richard Marian Thomaiyar73906b92019-01-04 23:48:02 +05301334 std::string(networkIntfObjectBasePath) + "/" +
1335 channelData[chNum].chName;
George Liu42f64ef2024-02-05 15:03:18 +08001336 auto propValue = ipmi::getDbusProperty(
1337 bus, networkIntfServiceName, networkIntfObj,
1338 networkChConfigIntfName, privilegePropertyString);
1339
1340 intfPrivStr = std::get<std::string>(propValue);
Jiaqing Zhao826bf662022-11-07 14:33:00 +08001341 intfPriv =
1342 static_cast<uint8_t>(convertToPrivLimitIndex(intfPrivStr));
AppaRao Puli9613ed72018-09-01 23:46:44 +05301343 }
Vernon Maueryf442e112019-04-09 11:44:36 -07001344 catch (const std::bad_variant_access& e)
AppaRao Puli9613ed72018-09-01 23:46:44 +05301345 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001346 lg2::debug("Network interface '{INTERFACE}' does not exist",
1347 "INTERFACE", channelData[chNum].chName);
AppaRao Puli9613ed72018-09-01 23:46:44 +05301348 continue;
1349 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001350 catch (const sdbusplus::exception_t& e)
AppaRao Puli9613ed72018-09-01 23:46:44 +05301351 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001352 lg2::debug("Network interface '{INTERFACE}' does not exist",
1353 "INTERFACE", channelData[chNum].chName);
AppaRao Puli9613ed72018-09-01 23:46:44 +05301354 continue;
1355 }
Jiaqing Zhao826bf662022-11-07 14:33:00 +08001356 catch (const std::invalid_argument& e)
1357 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001358 lg2::debug("exception: Invalid privilege");
Jiaqing Zhao826bf662022-11-07 14:33:00 +08001359 continue;
1360 }
AppaRao Puli9613ed72018-09-01 23:46:44 +05301361
AppaRao Puli9613ed72018-09-01 23:46:44 +05301362 if (channelData[chNum].chAccess.chNonVolatileData.privLimit !=
1363 intfPriv)
1364 {
1365 isUpdated = true;
1366 channelData[chNum].chAccess.chNonVolatileData.privLimit =
1367 intfPriv;
1368 channelData[chNum].chAccess.chVolatileData.privLimit = intfPriv;
1369 }
1370 }
1371 }
1372
1373 if (isUpdated)
1374 {
1375 // Write persistent data to file
1376 if (writeChannelPersistData() != 0)
1377 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001378 lg2::debug("Failed to update the persistent data file");
AppaRao Puli9613ed72018-09-01 23:46:44 +05301379 return -EIO;
1380 }
1381 // Write Volatile data to file
1382 if (writeChannelVolatileData() != 0)
1383 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001384 lg2::debug("Failed to update the channel volatile data");
AppaRao Puli9613ed72018-09-01 23:46:44 +05301385 return -EIO;
1386 }
1387 }
1388
1389 return 0;
1390}
1391
AppaRao Puli071f3f22018-05-24 16:45:30 +05301392void ChannelConfig::initChannelPersistData()
1393{
Richard Marian Thomaiyare91474c2019-09-01 23:02:47 +05301394 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1395 channelLock{*channelMutex};
1396
AppaRao Puli071f3f22018-05-24 16:45:30 +05301397 /* Always read the channel config */
1398 if (loadChannelConfig() != 0)
1399 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001400 lg2::error("Failed to read channel config file");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301401 throw std::ios_base::failure("Failed to load channel configuration");
1402 }
1403
1404 /* Populate the channel persist data */
1405 if (readChannelPersistData() != 0)
1406 {
1407 // Copy default NV data to RW location
Patrick Williams3d8d7932022-06-16 12:01:28 -05001408 std::filesystem::copy_file(channelAccessDefaultFilename,
1409 channelNvDataFilename);
AppaRao Puli071f3f22018-05-24 16:45:30 +05301410
1411 // Load the channel access NV data
1412 if (readChannelPersistData() != 0)
1413 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001414 lg2::error("Failed to read channel access NV data");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301415 throw std::ios_base::failure(
1416 "Failed to read channel access NV configuration");
1417 }
1418 }
1419
1420 // First check the volatile data file
1421 // If not present, load the default values
1422 if (readChannelVolatileData() != 0)
1423 {
1424 // Copy default volatile data to temporary location
1425 // NV file(channelNvDataFilename) must have created by now.
Patrick Williams3d8d7932022-06-16 12:01:28 -05001426 std::filesystem::copy_file(channelNvDataFilename,
1427 channelVolatileDataFilename);
AppaRao Puli071f3f22018-05-24 16:45:30 +05301428
1429 // Load the channel access volatile data
1430 if (readChannelVolatileData() != 0)
1431 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001432 lg2::error("Failed to read channel access volatile data");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301433 throw std::ios_base::failure(
1434 "Failed to read channel access volatile configuration");
1435 }
1436 }
AppaRao Puli9613ed72018-09-01 23:46:44 +05301437
1438 // Synchronize the channel config(priv) with network channel
1439 // configuration(priv) over dbus
1440 if (syncNetworkChannelConfig() != 0)
1441 {
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001442 lg2::error(
AppaRao Puli9613ed72018-09-01 23:46:44 +05301443 "Failed to synchronize data with network channel config over dbus");
1444 throw std::ios_base::failure(
1445 "Failed to synchronize data with network channel config over dbus");
1446 }
1447
Vernon Mauery1e3ed2c2024-03-11 12:53:55 -07001448 lg2::debug("Successfully completed channel data initialization.");
AppaRao Puli071f3f22018-05-24 16:45:30 +05301449 return;
1450}
1451
1452} // namespace ipmi