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