blob: 5738c43d89fade13f16ee2d48a51ddbd10ca0e46 [file] [log] [blame]
Tom Joseph4e57ada2016-08-10 06:51:12 -05001#pragma once
2
3#include "message.hpp"
4#include "session.hpp"
5
Andrew Geissler9d9b7632020-05-17 09:18:05 -05006#include <cstddef>
7
Tom Joseph4e57ada2016-08-10 06:51:12 -05008namespace message
9{
10
11namespace parser
12{
13
14constexpr size_t RMCP_VERSION = 6;
15
16// RMCP Messages with class=IPMI should be sent with an RMCP Sequence
17// Number of FFh to indicate that an RMCP ACK message should not be
18// generated by the message receiver.
19constexpr size_t RMCP_SEQ = 0xFF;
20
21// RMCP Message Class 7h is for IPMI
22constexpr size_t RMCP_MESSAGE_CLASS_IPMI = 7;
23
Tom Joseph64703f42017-01-10 17:03:48 +053024// RMCP Session Header Size
25constexpr size_t RMCP_SESSION_HEADER_SIZE = 4;
26
Tom Joseph1efcb492017-01-31 16:56:47 +053027// Maximum payload size
28constexpr size_t MAX_PAYLOAD_SIZE = 255;
29
Tom Joseph4e57ada2016-08-10 06:51:12 -050030enum class SessionHeader
31{
32 IPMI15 = 0x00,
33 IPMI20 = 0x06,
34 INVALID = 0xFF,
35};
36
37struct BasicHeader_t
38{
39 // RMCP Header
40 uint8_t version;
41 uint8_t reserved;
42 uint8_t rmcpSeqNum;
43 uint8_t classOfMsg;
44
45 // IPMI partial session header
46 union
47 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070048 uint8_t reserved1 : 4;
49 uint8_t authType : 4;
Tom Joseph4e57ada2016-08-10 06:51:12 -050050 uint8_t formatType;
51 } format;
52} __attribute__((packed));
53
Tom Joseph3563f8f2017-05-08 15:42:54 +053054/**
Tom Joseph4e57ada2016-08-10 06:51:12 -050055 * @brief Unflatten an incoming packet and prepare the IPMI message
56 *
57 * @param[in] inPacket - Incoming IPMI packet
58 *
59 * @return A tuple with IPMI message and the session header type to sent the
60 * response packet. In case of success incoming message and session
61 * header type. In case of failure nullptr and session header type
62 * would be invalid.
63 */
Vernon Maueryd999ffc2018-10-25 09:16:05 -070064std::tuple<std::shared_ptr<Message>, SessionHeader>
Vernon Mauery9e801a22018-10-12 13:20:49 -070065 unflatten(std::vector<uint8_t>& inPacket);
Tom Joseph4e57ada2016-08-10 06:51:12 -050066
Tom Joseph3563f8f2017-05-08 15:42:54 +053067/**
Tom Joseph4e57ada2016-08-10 06:51:12 -050068 * @brief Flatten an IPMI message and generate the IPMI packet with the
69 * session header
70 *
71 * @param[in] outMessage - IPMI message to be flattened
72 * @param[in] authType - Session header type to be added to the IPMI
73 * packet
74 *
75 * @return IPMI packet on success
76 */
Vernon Maueryd999ffc2018-10-25 09:16:05 -070077std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage,
Vernon Mauery224f36a2018-10-25 08:52:23 -070078 SessionHeader authType,
79 std::shared_ptr<session::Session> session);
Tom Joseph4e57ada2016-08-10 06:51:12 -050080
81} // namespace parser
82
83namespace ipmi15parser
84{
85
86struct SessionHeader_t
87{
88 struct parser::BasicHeader_t base;
89 uint32_t sessSeqNum;
90 uint32_t sessId;
91 // <Optional Field: AuthCode>
92 uint8_t payloadLength;
93} __attribute__((packed));
94
95struct SessionTrailer_t
96{
97 uint8_t legacyPad;
98} __attribute__((packed));
99
Tom Joseph3563f8f2017-05-08 15:42:54 +0530100/**
Tom Joseph4e57ada2016-08-10 06:51:12 -0500101 * @brief Unflatten an incoming packet and prepare the IPMI message
102 *
103 * @param[in] inPacket - Incoming IPMI packet
104 *
105 * @return IPMI message in the packet on success
106 */
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700107std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500108
Tom Joseph3563f8f2017-05-08 15:42:54 +0530109/**
Tom Joseph4e57ada2016-08-10 06:51:12 -0500110 * @brief Flatten an IPMI message and generate the IPMI packet with the
111 * session header
112 *
113 * @param[in] outMessage - IPMI message to be flattened
114 *
115 * @return IPMI packet on success
116 */
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700117std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage,
Vernon Mauery224f36a2018-10-25 08:52:23 -0700118 std::shared_ptr<session::Session> session);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500119
120} // namespace ipmi15parser
121
122namespace ipmi20parser
123{
124
125constexpr size_t MAX_INTEGRITY_DATA_LENGTH = 12;
126constexpr size_t PAYLOAD_ENCRYPT_MASK = 0x80;
127constexpr size_t PAYLOAD_AUTH_MASK = 0x40;
128
129struct SessionHeader_t
130{
131 struct parser::BasicHeader_t base;
132
133 uint8_t payloadType;
134
135 uint32_t sessId;
136 uint32_t sessSeqNum;
137 uint16_t payloadLength;
138} __attribute__((packed));
139
140struct SessionTrailer_t
141{
142 // Integrity Pad
143 uint8_t padLength;
144 uint8_t nextHeader;
Tom Joseph4e57ada2016-08-10 06:51:12 -0500145} __attribute__((packed));
146
Tom Joseph3563f8f2017-05-08 15:42:54 +0530147/**
Tom Joseph4e57ada2016-08-10 06:51:12 -0500148 * @brief Unflatten an incoming packet and prepare the IPMI message
149 *
150 * @param[in] inPacket - Incoming IPMI packet
151 *
152 * @return IPMI message in the packet on success
153 */
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700154std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500155
Tom Joseph3563f8f2017-05-08 15:42:54 +0530156/**
Tom Joseph4e57ada2016-08-10 06:51:12 -0500157 * @brief Flatten an IPMI message and generate the IPMI packet with the
158 * session header
159 *
160 * @param[in] outMessage - IPMI message to be flattened
161 *
162 * @return IPMI packet on success
163 */
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700164std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage,
Vernon Mauery224f36a2018-10-25 08:52:23 -0700165 std::shared_ptr<session::Session> session);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500166
167namespace internal
168{
169
Tom Joseph3563f8f2017-05-08 15:42:54 +0530170/**
Tom Joseph4e57ada2016-08-10 06:51:12 -0500171 * @brief Add sequence number to the message
172 *
173 * @param[in] packet - outgoing packet to which to add sequence number
174 * @param[in] session - session handle
175 *
176 */
Vernon Mauery224f36a2018-10-25 08:52:23 -0700177void addSequenceNumber(std::vector<uint8_t>& packet,
178 std::shared_ptr<session::Session> session);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500179
Tom Joseph3563f8f2017-05-08 15:42:54 +0530180/**
Tom Joseph64703f42017-01-10 17:03:48 +0530181 * @brief Verify the integrity data of the incoming IPMI packet
182 *
183 * @param[in] packet - Incoming IPMI packet
Tom Joseph027dfc22017-01-26 13:30:53 +0530184 * @param[in] message - IPMI Message populated from the incoming packet
185 * @param[in] payloadLen - Length of the IPMI payload
Tom Joseph64703f42017-01-10 17:03:48 +0530186 *
187 */
188bool verifyPacketIntegrity(const std::vector<uint8_t>& packet,
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700189 const std::shared_ptr<Message> message,
190 size_t payloadLen);
Tom Joseph64703f42017-01-10 17:03:48 +0530191
Tom Joseph3563f8f2017-05-08 15:42:54 +0530192/**
Tom Joseph64703f42017-01-10 17:03:48 +0530193 * @brief Add Integrity data to the outgoing IPMI packet
194 *
195 * @param[in] packet - Outgoing IPMI packet
Tom Joseph1404bca2017-01-26 14:17:02 +0530196 * @param[in] message - IPMI Message populated for the outgoing packet
197 * @param[in] payloadLen - Length of the IPMI payload
Tom Joseph64703f42017-01-10 17:03:48 +0530198 */
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700199void addIntegrityData(std::vector<uint8_t>& packet,
200 const std::shared_ptr<Message> message,
Tom Joseph1404bca2017-01-26 14:17:02 +0530201 size_t payloadLen);
Tom Joseph64703f42017-01-10 17:03:48 +0530202
Tom Joseph3563f8f2017-05-08 15:42:54 +0530203/**
Tom Joseph78478a82017-01-26 14:24:29 +0530204 * @brief Decrypt the encrypted payload in the incoming IPMI packet
205 *
206 * @param[in] packet - Incoming IPMI packet
207 * @param[in] message - IPMI Message populated from the incoming packet
208 * @param[in] payloadLen - Length of encrypted IPMI payload
209 *
210 * @return on successful completion, return the plain text payload
211 */
212std::vector<uint8_t> decryptPayload(const std::vector<uint8_t>& packet,
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700213 const std::shared_ptr<Message> message,
214 size_t payloadLen);
Tom Joseph78478a82017-01-26 14:24:29 +0530215
Tom Joseph3563f8f2017-05-08 15:42:54 +0530216/**
Tom Joseph75362832017-01-26 15:17:04 +0530217 * @brief Encrypt the plain text payload for the outgoing IPMI packet
218 *
219 * @param[in] message - IPMI Message populated for the outgoing packet
220 *
221 * @return on successful completion, return the encrypted payload
222 */
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700223std::vector<uint8_t> encryptPayload(std::shared_ptr<Message> message);
Tom Joseph75362832017-01-26 15:17:04 +0530224
Tom Joseph4e57ada2016-08-10 06:51:12 -0500225} // namespace internal
226
227} // namespace ipmi20parser
228
229} // namespace message