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 #pragma once
17 #include <openssl/crypto.h>
18 
19 #include <array>
20 #include <ipmid/api.hpp>
21 #include <string>
22 
23 namespace ipmi
24 {
25 
26 static constexpr uint8_t maxIpmiChannels = 16;
27 static constexpr uint8_t currentChNum = 0xE;
28 static constexpr uint8_t invalidChannel = 0xff;
29 static constexpr const uint8_t ccActionNotSupportedForChannel = 0x82;
30 static constexpr const uint8_t ccAccessModeNotSupportedForChannel = 0x83;
31 
32 /**
33  * @array of privilege levels
34  */
35 extern const std::array<std::string, PRIVILEGE_OEM + 1> privList;
36 
37 /**
38  * @enum Channel Protocol Type (refer spec sec 6.4)
39  */
40 enum class EChannelProtocolType : uint8_t
41 {
42     na = 0x00,
43     ipmbV10 = 0x01,
44     icmbV11 = 0x02,
45     reserved = 0x03,
46     ipmiSmbus = 0x04,
47     kcs = 0x05,
48     smic = 0x06,
49     bt10 = 0x07,
50     bt15 = 0x08,
51     tMode = 0x09,
52     oem = 0x1C,
53 };
54 
55 /**
56  * @enum Channel Medium Type (refer spec sec 6.5)
57  */
58 enum class EChannelMediumType : uint8_t
59 {
60     reserved = 0x00,
61     ipmb = 0x01,
62     icmbV10 = 0x02,
63     icmbV09 = 0x03,
64     lan8032 = 0x04,
65     serial = 0x05,
66     otherLan = 0x06,
67     pciSmbus = 0x07,
68     smbusV11 = 0x08,
69     smbusV20 = 0x09,
70     usbV1x = 0x0A,
71     usbV2x = 0x0B,
72     systemInterface = 0x0C,
73     oem = 0x60,
74     unknown = 0x82,
75 };
76 
77 /**
78  * @enum Channel Session Type (refer spec sec 22.24 -
79  * response data byte 5)
80  */
81 enum class EChannelSessSupported : uint8_t
82 {
83     none = 0,
84     single = 1,
85     multi = 2,
86     any = 3,
87 };
88 
89 /**
90  * @enum Channel Access Mode (refer spec sec 6.6)
91  */
92 enum class EChannelAccessMode : uint8_t
93 {
94     disabled = 0,
95     preboot = 1,
96     alwaysAvail = 2,
97     shared = 3,
98 };
99 
100 /**
101  * @enum Authentication Types (refer spec sec 13.6 - IPMI
102  * Session Header)
103  */
104 enum class EAuthType : uint8_t
105 {
106     none = (1 << 0x0),
107     md2 = (1 << 0x1),
108     md5 = (1 << 0x2),
109     reserved = (1 << 0x3),
110     straightPasswd = (1 << 0x4),
111     oem = (1 << 0x5),
112 };
113 
114 // TODO: Remove duplicate 'PayloadType' definition from netipmid's message.hpp
115 // to phosphor-ipmi-host/include
116 /**
117  * @enum Payload Types (refer spec sec 13.27.3)
118  */
119 enum class PayloadType : uint8_t
120 {
121     IPMI = 0x00,
122     SOL = 0x01,
123     OPEN_SESSION_REQUEST = 0x10,
124     OPEN_SESSION_RESPONSE = 0x11,
125     RAKP1 = 0x12,
126     RAKP2 = 0x13,
127     RAKP3 = 0x14,
128     RAKP4 = 0x15,
129     INVALID = 0xFF,
130 };
131 
132 /**
133  * @enum Access mode for channel access set/get (refer spec
134  * sec 22.22 - request byte 2[7:6])
135  */
136 typedef enum
137 {
138     doNotSet = 0x00,
139     nvData = 0x01,
140     activeData = 0x02,
141     reserved = 0x03,
142 } EChannelActionType;
143 
144 /**
145  * @enum Access set flag to determine changes that has to be updated
146  * in channel access data configuration.
147  */
148 enum AccessSetFlag
149 {
150     setAccessMode = (1 << 0),
151     setUserAuthEnabled = (1 << 1),
152     setMsgAuthEnabled = (1 << 2),
153     setAlertingEnabled = (1 << 3),
154     setPrivLimit = (1 << 4),
155 };
156 
157 /** @struct ChannelAccess
158  *
159  *  Structure to store channel access related information, defined in IPMI
160  * specification and used in Get / Set channel access (refer spec sec 22.22
161  * & 22.23)
162  */
163 struct ChannelAccess
164 {
165     uint8_t accessMode;
166     bool userAuthDisabled;
167     bool perMsgAuthDisabled;
168     bool alertingDisabled;
169     uint8_t privLimit;
170 };
171 
172 /** @struct ChannelInfo
173  *
174  *  Structure to store data about channel information, which identifies each
175  *  channel type and information as defined in IPMI specification. (refer spec
176  * sec 22.22 & 22.23)
177  */
178 struct ChannelInfo
179 {
180     uint8_t mediumType;
181     uint8_t protocolType;
182     uint8_t sessionSupported;
183     bool isIpmi; // Is session IPMI
184     // This is used in Get LAN Configuration parameter.
185     // This holds the supported AuthTypes for a given channel.
186     uint8_t authTypeSupported;
187 };
188 
189 /** @brief determines valid channel
190  *
191  *  @param[in] chNum- channel number
192  *
193  *  @return true if valid, false otherwise
194  */
195 bool isValidChannel(const uint8_t chNum);
196 
197 /** @brief determines whether channel device exist
198  *
199  *  @param[in] chNum - channel number
200  *
201  *  @return true if valid, false otherwise
202  */
203 bool doesDeviceExist(const uint8_t chNum);
204 
205 /** @brief determines whether privilege limit is valid
206  *
207  *  @param[in] privLimit - Privilege limit
208  *
209  *  @return true if valid, false otherwise
210  */
211 bool isValidPrivLimit(const uint8_t privLimit);
212 
213 /** @brief determines whether access mode  is valid
214  *
215  *  @param[in] accessMode - Access mode
216  *
217  *  @return true if valid, false otherwise
218  */
219 bool isValidAccessMode(const uint8_t accessMode);
220 
221 /** @brief determines valid authentication type based on channel number
222  *
223  *  @param[in] chNum - channel number
224  *  @param[in] authType - authentication type
225  *
226  *  @return true if valid, false otherwise
227  */
228 bool isValidAuthType(const uint8_t chNum, const EAuthType& authType);
229 
230 /** @brief determines supported session type of a channel
231  *
232  *  @param[in] chNum - channel number
233  *
234  *  @return EChannelSessSupported - supported session type
235  */
236 EChannelSessSupported getChannelSessionSupport(const uint8_t chNum);
237 
238 /** @brief determines number of active sessions on a channel
239  *
240  *  @param[in] chNum - channel number
241  *
242  *  @return numer of active sessions
243  */
244 int getChannelActiveSessions(const uint8_t chNum);
245 
246 /** @brief determines maximum transfer size for a channel
247  *
248  *  @param[in] chNum - channel number
249  *
250  *  @return maximum bytes that can be transferred on this channel
251  */
252 size_t getChannelMaxTransferSize(uint8_t chNum);
253 
254 /** @brief initializes channel management
255  *
256  *  @return ccSuccess for success, others for failure.
257  */
258 Cc ipmiChannelInit();
259 
260 /** @brief provides channel info details
261  *
262  *  @param[in] chNum - channel number
263  *  @param[out] chInfo - channel info details
264  *
265  *  @return ccSuccess for success, others for failure.
266  */
267 Cc getChannelInfo(const uint8_t chNum, ChannelInfo& chInfo);
268 
269 /** @brief provides channel access data
270  *
271  *  @param[in] chNum - channel number
272  *  @param[out] chAccessData -channel access data
273  *
274  *  @return ccSuccess for success, others for failure.
275  */
276 Cc getChannelAccessData(const uint8_t chNum, ChannelAccess& chAccessData);
277 
278 /** @brief provides function to convert current channel number (0xE)
279  *
280  *  @param[in] chNum - channel number as requested in commands.
281  *  @param[in] devChannel - channel number as provided by device (not 0xE)
282  *
283  *  @return same channel number or proper channel number for current channel
284  * number (0xE).
285  */
286 static inline uint8_t convertCurrentChannelNum(const uint8_t chNum,
287                                                const uint8_t devChannel)
288 {
289     if (chNum == currentChNum)
290     {
291         return devChannel;
292     }
293     return chNum;
294 }
295 
296 /** @brief to set channel access data
297  *
298  *  @param[in] chNum - channel number
299  *  @param[in] chAccessData - channel access data
300  *  @param[in] setFlag - flag to indicate updatable fields
301  *
302  *  @return ccSuccess for success, others for failure.
303  */
304 Cc setChannelAccessData(const uint8_t chNum, const ChannelAccess& chAccessData,
305                         const uint8_t setFlag);
306 
307 /** @brief to get channel access data persistent data
308  *
309  *  @param[in] chNum - channel number
310  *  @param[out] chAccessData - channel access data
311  *
312  *  @return ccSuccess for success, others for failure.
313  */
314 Cc getChannelAccessPersistData(const uint8_t chNum,
315                                ChannelAccess& chAccessData);
316 
317 /** @brief to set channel access data persistent data
318  *
319  *  @param[in] chNum - channel number
320  *  @param[in] chAccessData - channel access data
321  *  @param[in] setFlag - flag to indicate updatable fields
322  *
323  *  @return ccSuccess for success, others for failure.
324  */
325 Cc setChannelAccessPersistData(const uint8_t chNum,
326                                const ChannelAccess& chAccessData,
327                                const uint8_t setFlag);
328 
329 /** @brief provides supported authentication type for the channel
330  *
331  *  @param[in] chNum - channel number
332  *  @param[out] authTypeSupported - supported authentication type
333  *
334  *  @return ccSuccess for success, others for failure.
335  */
336 Cc getChannelAuthTypeSupported(const uint8_t chNum, uint8_t& authTypeSupported);
337 
338 /** @brief provides enabled authentication type for the channel
339  *
340  *  @param[in] chNum - channel number
341  *  @param[in] priv - privilege
342  *  @param[out] authType - enabled authentication type
343  *
344  *  @return ccSuccess for success, others for failure.
345  */
346 Cc getChannelEnabledAuthType(const uint8_t chNum, const uint8_t priv,
347                              EAuthType& authType);
348 
349 /** @brief Retrieves the LAN channel name from the IPMI channel number
350  *
351  *  @param[in] chNum - IPMI channel number
352  *
353  *  @return the LAN channel name (i.e. eth0)
354  */
355 std::string getChannelName(const uint8_t chNum);
356 
357 /** @brief Retrieves the LAN channel number from the IPMI channel name
358  *
359  *  @param[in] chName - IPMI channel name (i.e. eth0)
360  *
361  *  @return the LAN channel number
362  */
363 uint8_t getChannelByName(const std::string& chName);
364 
365 /** @brief determines whether payload type is valid
366  *
367  *	@param[in] payload type - Payload Type
368  *
369  *	@return true if valid, false otherwise
370  */
371 bool isValidPayloadType(const PayloadType payloadType);
372 
373 } // namespace ipmi
374