1 /*
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 #pragma once
18 #include "channel_layer.hpp"
19 
20 #include <boost/interprocess/sync/file_lock.hpp>
21 #include <boost/interprocess/sync/named_recursive_mutex.hpp>
22 #include <cstdint>
23 #include <ctime>
24 #include <nlohmann/json.hpp>
25 #include <sdbusplus/bus.hpp>
26 
27 namespace ipmi
28 {
29 
30 using Json = nlohmann::json;
31 
32 using DbusVariant =
33     sdbusplus::message::variant<std::vector<std::string>, std::string, bool>;
34 
35 using DbusChObjProperties = std::vector<std::pair<std::string, DbusVariant>>;
36 
37 static constexpr const char* ipmiChannelMutex = "ipmi_channel_mutex";
38 static constexpr const char* ipmiChMutexCleanupLockFile =
39     "/var/lib/ipmi/ipmi_channel_mutex_cleanup";
40 
41 /** @struct ChannelAccessData
42  *
43  *  Structure to store both non-volatile and volatile channel access information
44  *  as used by IPMI specification (refer spec sec 22.22 to 22.24)
45  */
46 struct ChannelAccessData
47 {
48     ChannelAccess chNonVolatileData;
49     ChannelAccess chVolatileData;
50 };
51 
52 /** @struct ChannelData
53  *
54  *  Structure for channel information - base structure to get all information
55  * about the channel.(refer spec sec 22.22 to 22.24)
56  */
57 struct ChannelData
58 {
59     std::string chName;
60     uint8_t chID;
61     bool isChValid;
62     uint8_t activeSessCount;
63     ChannelInfo chInfo;
64     ChannelAccessData chAccess;
65 };
66 
67 class ChannelConfig;
68 
69 ChannelConfig& getChannelConfigObject();
70 
71 class ChannelConfig
72 {
73   public:
74     ChannelConfig(const ChannelConfig&) = delete;
75     ChannelConfig& operator=(const ChannelConfig&) = delete;
76     ChannelConfig(ChannelConfig&&) = delete;
77     ChannelConfig& operator=(ChannelConfig&&) = delete;
78 
79     ~ChannelConfig();
80     ChannelConfig();
81 
82     /** @brief determines valid channel
83      *
84      *  @param[in] chNum - channel number
85      *
86      *  @return true if valid, false otherwise
87      */
88     bool isValidChannel(const uint8_t chNum);
89 
90     /** @brief determines valid authentication type
91      *
92      *  @param[in] chNum - channel number
93      *  @param[in] authType - authentication type
94      *
95      *  @return true if valid, false otherwise
96      */
97     bool isValidAuthType(const uint8_t chNum, const EAuthType& authType);
98 
99     /** @brief determines supported session type of a channel
100      *
101      *  @param[in] chNum - channel number
102      *
103      *  @return EChannelSessSupported - supported session type
104      */
105     EChannelSessSupported getChannelSessionSupport(const uint8_t chNum);
106 
107     /** @brief determines number of active sessions on a channel
108      *
109      *  @param[in] chNum - channel number
110      *
111      *  @return numer of active sessions
112      */
113     int getChannelActiveSessions(const uint8_t chNum);
114 
115     /** @brief provides channel info details
116      *
117      *  @param[in] chNum - channel number
118      *  @param[out] chInfo - channel info details
119      *
120      *  @return IPMI_CC_OK for success, others for failure.
121      */
122     ipmi_ret_t getChannelInfo(const uint8_t chNum, ChannelInfo& chInfo);
123 
124     /** @brief provides channel access data
125      *
126      *  @param[in] chNum - channel number
127      *  @param[out] chAccessData - channel access data
128      *
129      *  @return IPMI_CC_OK for success, others for failure.
130      */
131     ipmi_ret_t getChannelAccessData(const uint8_t chNum,
132                                     ChannelAccess& chAccessData);
133 
134     /** @brief to set channel access data
135      *
136      *  @param[in] chNum - channel number
137      *  @param[in] chAccessData - channel access data
138      *  @param[in] setFlag - flag to indicate updatable fields
139      *
140      *  @return IPMI_CC_OK for success, others for failure.
141      */
142     ipmi_ret_t setChannelAccessData(const uint8_t chNum,
143                                     const ChannelAccess& chAccessData,
144                                     const uint8_t setFlag);
145 
146     /** @brief to get channel access data persistent data
147      *
148      *  @param[in] chNum - channel number
149      *  @param[out] chAccessData - channel access data
150      *
151      *  @return IPMI_CC_OK for success, others for failure.
152      */
153     ipmi_ret_t getChannelAccessPersistData(const uint8_t chNum,
154                                            ChannelAccess& chAccessData);
155 
156     /** @brief to set channel access data persistent data
157      *
158      *  @param[in] chNum - channel number
159      *  @param[in] chAccessData - channel access data
160      *  @param[in] setFlag - flag to indicate updatable fields
161      *
162      *  @return IPMI_CC_OK for success, others for failure.
163      */
164     ipmi_ret_t setChannelAccessPersistData(const uint8_t chNum,
165                                            const ChannelAccess& chAccessData,
166                                            const uint8_t setFlag);
167 
168     /** @brief provides supported authentication type for the channel
169      *
170      *  @param[in] chNum - channel number
171      *  @param[out] authTypeSupported - supported authentication type
172      *
173      *  @return IPMI_CC_OK for success, others for failure.
174      */
175     ipmi_ret_t getChannelAuthTypeSupported(const uint8_t chNum,
176                                            uint8_t& authTypeSupported);
177 
178     /** @brief provides enabled authentication type for the channel
179      *
180      *  @param[in] chNum - channel number
181      *  @param[in] priv - privilege
182      *  @param[out] authType - enabled authentication type
183      *
184      *  @return IPMI_CC_OK for success, others for failure.
185      */
186     ipmi_ret_t getChannelEnabledAuthType(const uint8_t chNum,
187                                          const uint8_t priv,
188                                          EAuthType& authType);
189 
190     /** @brief conver to channel privilege from system privilege
191      *
192      *  @param[in] value - privilege value
193      *
194      *  @return Channel privilege
195      */
196     CommandPrivilege convertToPrivLimitIndex(const std::string& value);
197 
198     /** @brief function to write persistent channel configuration to config file
199      *
200      *  @return 0 for success, -errno for failure.
201      */
202     int writeChannelPersistData();
203 
204     /** @brief function to write volatile channel configuration to config file
205      *
206      *  @return 0 for success, -errno for failure.
207      */
208     int writeChannelVolatileData();
209 
210     /** @brief function to get channel data based on channel number
211      *
212      *  @param[in] chNum - channel number
213      *
214      *  @return 0 for success, -errno for failure.
215      */
216     ChannelData* getChannelDataPtr(const uint8_t chNum);
217 
218     uint32_t signalFlag = 0;
219 
220     std::unique_ptr<boost::interprocess::named_recursive_mutex> channelMutex{
221         nullptr};
222 
223   private:
224     ChannelData channelData[maxIpmiChannels];
225     std::time_t nvFileLastUpdatedTime;
226     std::time_t voltFileLastUpdatedTime;
227     std::time_t getUpdatedFileTime(const std::string& fileName);
228     boost::interprocess::file_lock mutexCleanupLock;
229     sdbusplus::bus::bus bus;
230     bool signalHndlrObjectState = false;
231     boost::interprocess::file_lock sigHndlrLock;
232 
233     /** @brief function to initialize persistent channel configuration
234      *
235      */
236     void initChannelPersistData();
237 
238     /** @brief function to set default channel configuration based on channel
239      * number
240      *
241      *  @param[in] chNum - channel number
242      *  @param[in] chName - channel name
243      */
244     void setDefaultChannelConfig(const uint8_t chNum,
245                                  const std::string& chName);
246 
247     /** @brief function to load all channel configuration
248      *
249      *  @return 0 for success, -errno for failure.
250      */
251     int loadChannelConfig();
252 
253     /** @brief function to read persistent channel data
254      *
255      *  @return 0 for success, -errno for failure.
256      */
257     int readChannelPersistData();
258 
259     /** @brief function to read volatile channel data
260      *
261      *  @return 0 for success, -errno for failure.
262      */
263     int readChannelVolatileData();
264 
265     /** @brief function to check and reload persistent channel data
266      *
267      *  @return 0 for success, -errno for failure.
268      */
269     int checkAndReloadNVData();
270 
271     /** @brief function to check and reload volatile channel data
272      *
273      *  @return 0 for success, -errno for failure.
274      */
275     int checkAndReloadVolatileData();
276 
277     /** @brief function to sync channel privilege with system network channel
278      * privilege
279      *
280      *  @return 0 for success, -errno for failure.
281      */
282     int syncNetworkChannelConfig();
283 
284     /** @brief function to set D-Bus property value
285      *
286      *  @param[in] bus - bus
287      *  @param[in] service - service name
288      *  @param[in] objPath - object path
289      *  @param[in] interface - interface
290      *  @param[in] property - property name
291      *  @param[in] value - property value
292      *
293      *  @return 0 for success, -errno for failure.
294      */
295     int setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
296                         const std::string& objPath,
297                         const std::string& interface,
298                         const std::string& property, const DbusVariant& value);
299 
300     /** @brief function to get D-Bus property value
301      *
302      *  @param[in] bus - bus
303      *  @param[in] service - service name
304      *  @param[in] objPath - object path
305      *  @param[in] interface - interface
306      *  @param[in] property - property name
307      *  @param[out] value - property value
308      *
309      *  @return 0 for success, -errno for failure.
310      */
311     int getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
312                         const std::string& objPath,
313                         const std::string& interface,
314                         const std::string& property, DbusVariant& value);
315 
316     /** @brief function to read json config file
317      *
318      *  @param[in] configFile - configuration file name
319      *
320      *  @return Json object
321      */
322     Json readJsonFile(const std::string& configFile);
323 
324     /** @brief function to write json config file
325      *
326      *  @param[in] configFile - configuration file name
327      *  @param[in] jsonData - json object
328      *
329      *  @return 0 for success, -errno for failure.
330      */
331     int writeJsonFile(const std::string& configFile, const Json& jsonData);
332 
333     /** @brief function to convert system access mode to Channel access mode
334      * type
335      *
336      *  @param[in] mode - access mode in string
337      *
338      *  @return Channel access mode.
339      */
340     EChannelAccessMode convertToAccessModeIndex(const std::string& mode);
341 
342     /** @brief function to convert access mode value to string
343      *
344      *  @param[in] value - acess mode value
345      *
346      *  @return access mode in string
347      */
348     std::string convertToAccessModeString(const uint8_t value);
349 
350     /** @brief function to convert privilege value to string
351      *
352      *  @param[in] value - privilege value
353      *
354      *  @return privilege in string
355      */
356     std::string convertToPrivLimitString(const uint8_t value);
357 
358     /** @brief function to convert session support string to value type
359      *
360      *  @param[in] value - session support type in string
361      *
362      *  @return support session type
363      */
364     EChannelSessSupported
365         convertToSessionSupportIndex(const std::string& value);
366 
367     /** @brief function to convert medium type string to value type
368      *
369      *  @param[in] value - medium type in string
370      *
371      *  @return channel medium type
372      */
373     EChannelMediumType convertToMediumTypeIndex(const std::string& value);
374 
375     /** @brief function to convert protocol type string to value type
376      *
377      *  @param[in] value - protocol type in string
378      *
379      *  @return channel protocol  type
380      */
381     EChannelProtocolType convertToProtocolTypeIndex(const std::string& value);
382 
383     /** @brief function to convert channel number to channel index
384      *
385      *  @param[in] chNum - channel number
386      *
387      *  @return channel index
388      */
389     uint8_t convertToChannelIndexNumber(const uint8_t chNum);
390 
391     /** @brief function to convert channel name to network interface name
392      *
393      *  @param[in] value - channel interface name - ipmi centric
394      *
395      *  @return network channel interface name
396      */
397     std::string convertToNetInterface(const std::string& value);
398 };
399 
400 } // namespace ipmi
401