blob: be707535fcdce1e63896e78e48a798540bfae53d [file] [log] [blame]
Tom Joseph4e57ada2016-08-10 06:51:12 -05001#include "message_parsers.hpp"
2
3#include <iostream>
4#include <memory>
5
6#include "endian.hpp"
7#include "main.hpp"
8#include "message.hpp"
9#include "sessions_manager.hpp"
10
11namespace message
12{
13
14namespace parser
15{
16
17std::tuple<std::unique_ptr<Message>, SessionHeader> unflatten(
18 std::vector<uint8_t>& inPacket)
19{
20 // Check if the packet has atleast the size of the RMCP Header
21 if (inPacket.size() < sizeof(BasicHeader_t))
22 {
23 throw std::runtime_error("RMCP Header missing");
24 }
25
26 auto rmcpHeaderPtr = reinterpret_cast<BasicHeader_t*>(inPacket.data());
27
28 // Verify if the fields in the RMCP header conforms to the specification
29 if ((rmcpHeaderPtr->version != RMCP_VERSION) ||
30 (rmcpHeaderPtr->rmcpSeqNum != RMCP_SEQ) ||
31 (rmcpHeaderPtr->classOfMsg != RMCP_MESSAGE_CLASS_IPMI))
32 {
33 throw std::runtime_error("RMCP Header is invalid");
34 }
35
36 // Read the Session Header and invoke the parser corresponding to the
37 // header type
38 switch (static_cast<SessionHeader>(rmcpHeaderPtr->format.formatType))
39 {
40 case SessionHeader::IPMI15:
41 {
42 return std::make_tuple(ipmi15parser::unflatten(inPacket),
43 SessionHeader::IPMI15);
44 }
45 case SessionHeader::IPMI20:
46 {
47 return std::make_tuple(ipmi20parser::unflatten(inPacket),
48 SessionHeader::IPMI20);
49 }
50 default:
51 {
52 throw std::runtime_error("Invalid Session Header");
53 }
54 }
55}
56
57std::vector<uint8_t> flatten(Message& outMessage,
58 SessionHeader authType,
59 session::Session& session)
60{
61 // Call the flatten routine based on the header type
62 switch (authType)
63 {
64 case SessionHeader::IPMI15:
65 {
66 return ipmi15parser::flatten(outMessage, session);
67 }
68 case SessionHeader::IPMI20:
69 {
70 return ipmi20parser::flatten(outMessage, session);
71 }
72 default:
73 {
74 return {};
75 }
76 }
77}
78
79} // namespace parser
80
81namespace ipmi15parser
82{
83
84std::unique_ptr<Message> unflatten(std::vector<uint8_t>& inPacket)
85{
86 // Check if the packet has atleast the Session Header
87 if (inPacket.size() < sizeof(SessionHeader_t))
88 {
89 throw std::runtime_error("IPMI1.5 Session Header Missing");
90 }
91
92 auto message = std::make_unique<Message>();
93
94 auto header = reinterpret_cast<SessionHeader_t*>(inPacket.data());
95
96 message->payloadType = PayloadType::IPMI;
Tom Joseph6a560762017-01-10 15:30:28 +053097 message->bmcSessionID = endian::from_ipmi(header->sessId);
98 message->sessionSeqNum = endian::from_ipmi(header->sessSeqNum);
Tom Joseph4e57ada2016-08-10 06:51:12 -050099 message->isPacketEncrypted = false;
100 message->isPacketAuthenticated = false;
101
102 auto payloadLen = header->payloadLength;
103
104 (message->payload).assign(inPacket.data() + sizeof(SessionHeader_t),
105 inPacket.data() + sizeof(SessionHeader_t) +
106 payloadLen);
107
108 return message;
109}
110
111std::vector<uint8_t> flatten(Message& outMessage, session::Session& session)
112{
113 std::vector<uint8_t> packet(sizeof(SessionHeader_t));
114
115 // Insert Session Header into the Packet
116 auto header = reinterpret_cast<SessionHeader_t*>(packet.data());
117 header->base.version = parser::RMCP_VERSION;
118 header->base.reserved = 0x00;
119 header->base.rmcpSeqNum = parser::RMCP_SEQ;
120 header->base.classOfMsg = parser::RMCP_MESSAGE_CLASS_IPMI;
121 header->base.format.formatType =
122 static_cast<uint8_t>(parser::SessionHeader::IPMI15);
123 header->sessSeqNum = 0;
Tom Joseph6a560762017-01-10 15:30:28 +0530124 header->sessId = endian::to_ipmi(outMessage.rcSessionID);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500125
126 header->payloadLength = static_cast<uint8_t>(outMessage.payload.size());
127
128 // Insert the Payload into the Packet
129 packet.insert(packet.end(), outMessage.payload.begin(),
130 outMessage.payload.end());
131
132 // Insert the Session Trailer
133 packet.resize(packet.size() + sizeof(SessionTrailer_t));
134 auto trailer = reinterpret_cast<SessionTrailer_t*>(packet.data() +
135 packet.size());
136 trailer->legacyPad = 0x00;
137
138 return packet;
139}
140
141} // namespace ipmi15parser
142
143namespace ipmi20parser
144{
145
146std::unique_ptr<Message> unflatten(std::vector<uint8_t>& inPacket)
147{
148 // Check if the packet has atleast the Session Header
149 if (inPacket.size() < sizeof(SessionHeader_t))
150 {
151 throw std::runtime_error("IPMI2.0 Session Header Missing");
152 }
153
154 auto message = std::make_unique<Message>();
155
156 auto header = reinterpret_cast<SessionHeader_t*>(inPacket.data());
157
158 message->payloadType = static_cast<PayloadType>
159 (header->payloadType & 0x3F);
Tom Joseph6a560762017-01-10 15:30:28 +0530160 message->bmcSessionID = endian::from_ipmi(header->sessId);
161 message->sessionSeqNum = endian::from_ipmi(header->sessSeqNum);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500162 message->isPacketEncrypted =
163 ((header->payloadType & PAYLOAD_ENCRYPT_MASK) ? true : false);
164 message->isPacketAuthenticated =
165 ((header->payloadType & PAYLOAD_AUTH_MASK) ? true : false);
166
Tom Joseph6a560762017-01-10 15:30:28 +0530167 auto payloadLen = endian::from_ipmi(header->payloadLength);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500168 message->payload.assign(inPacket.begin() + sizeof(SessionHeader_t),
169 inPacket.begin() + sizeof(SessionHeader_t) +
170 payloadLen);
171
Tom Joseph5fa487c2017-01-20 12:42:39 +0530172 if (message->isPacketAuthenticated)
Tom Joseph64703f42017-01-10 17:03:48 +0530173 {
Tom Joseph027dfc22017-01-26 13:30:53 +0530174 if (!(internal::verifyPacketIntegrity(inPacket,
175 *(message.get()),
176 payloadLen)))
Tom Joseph64703f42017-01-10 17:03:48 +0530177 {
178 throw std::runtime_error("Packet Integrity check failed");
179 }
180 }
181
Tom Joseph4e57ada2016-08-10 06:51:12 -0500182 return message;
183}
184
185std::vector<uint8_t> flatten(Message& outMessage, session::Session& session)
186{
187 std::vector<uint8_t> packet(sizeof(SessionHeader_t));
188
189 SessionHeader_t* header = reinterpret_cast<SessionHeader_t*>(packet.data());
190 header->base.version = parser::RMCP_VERSION;
191 header->base.reserved = 0x00;
192 header->base.rmcpSeqNum = parser::RMCP_SEQ;
193 header->base.classOfMsg = parser::RMCP_MESSAGE_CLASS_IPMI;
194 header->base.format.formatType =
195 static_cast<uint8_t>(parser::SessionHeader::IPMI20);
196 header->payloadType = static_cast<uint8_t>(outMessage.payloadType);
Tom Joseph6a560762017-01-10 15:30:28 +0530197 header->sessId = endian::to_ipmi(outMessage.rcSessionID);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500198
199 // Add session sequence number
200 internal::addSequenceNumber(packet, session);
201
202 // Add Payload
Tom Josephc0f5b5d2017-02-09 17:47:50 +0530203 header->payloadLength = endian::to_ipmi<uint16_t>(outMessage.payload.size());
Tom Joseph4e57ada2016-08-10 06:51:12 -0500204 // Insert the Payload into the Packet
205 packet.insert(packet.end(), outMessage.payload.begin(),
206 outMessage.payload.end());
207
Tom Joseph5fa487c2017-01-20 12:42:39 +0530208 if (outMessage.isPacketAuthenticated)
Tom Joseph64703f42017-01-10 17:03:48 +0530209 {
210 internal::addIntegrityData(packet, outMessage);
211 }
212
Tom Joseph4e57ada2016-08-10 06:51:12 -0500213 return packet;
214}
215
216namespace internal
217{
218
219void addSequenceNumber(std::vector<uint8_t>& packet, session::Session& session)
220{
221 SessionHeader_t* header = reinterpret_cast<SessionHeader_t*>(packet.data());
222
223 if (header->sessId == session::SESSION_ZERO)
224 {
225 header->sessSeqNum = 0x00;
226 }
227 else
228 {
229 auto seqNum = session.sequenceNums.increment();
Tom Joseph6a560762017-01-10 15:30:28 +0530230 header->sessSeqNum = endian::to_ipmi(seqNum);
Tom Joseph4e57ada2016-08-10 06:51:12 -0500231 }
232}
233
Tom Joseph64703f42017-01-10 17:03:48 +0530234bool verifyPacketIntegrity(const std::vector<uint8_t>& packet,
Tom Joseph027dfc22017-01-26 13:30:53 +0530235 const Message& message,
236 size_t payloadLen)
Tom Joseph64703f42017-01-10 17:03:48 +0530237{
Tom Joseph64703f42017-01-10 17:03:48 +0530238 /*
239 * Padding bytes are added to cause the number of bytes in the data range
240 * covered by the AuthCode(Integrity Data) field to be a multiple of 4 bytes
241 * .If present each integrity Pad byte is set to FFh. The following logic
242 * calculates the number of padding bytes added in the IPMI packet.
243 */
Tom Joseph5a2d3ce2017-02-01 20:18:37 +0530244 auto paddingLen = 4 - ((payloadLen + 2) & 3);
Tom Joseph64703f42017-01-10 17:03:48 +0530245
246 auto sessTrailerPos = sizeof(SessionHeader_t) + payloadLen + paddingLen;
247
248 auto trailer = reinterpret_cast<const SessionTrailer_t*>
249 (packet.data() + sessTrailerPos);
250
251 // Check trailer->padLength against paddingLen, both should match up,
252 // return false if the lengths don't match
Tom Joseph5fa487c2017-01-20 12:42:39 +0530253 if (trailer->padLength != paddingLen)
Tom Joseph64703f42017-01-10 17:03:48 +0530254 {
255 return false;
256 }
257
258 auto session = (std::get<session::Manager&>(singletonPool).getSession(
259 message.bmcSessionID)).lock();
260
261 auto integrityAlgo = session->getIntegrityAlgo();
262
263 // Check if Integrity data length is as expected, check integrity data
264 // length is same as the length expected for the Integrity Algorithm that
265 // was negotiated during the session open process.
Tom Joseph5fa487c2017-01-20 12:42:39 +0530266 if ((packet.size() - sessTrailerPos - sizeof(SessionTrailer_t)) !=
267 integrityAlgo->authCodeLength)
Tom Joseph64703f42017-01-10 17:03:48 +0530268 {
269 return false;
270 }
271
272 auto integrityIter = packet.cbegin();
273 std::advance(integrityIter, sessTrailerPos + sizeof(SessionTrailer_t));
274
275 // The integrity data is calculated from the AuthType/Format field up to and
276 // including the field that immediately precedes the AuthCode field itself.
277 size_t length = packet.size() - integrityAlgo->authCodeLength -
278 message::parser::RMCP_SESSION_HEADER_SIZE;
279
280 return integrityAlgo->verifyIntegrityData(packet, length, integrityIter);
281}
282
283void addIntegrityData(std::vector<uint8_t>& packet,
284 const Message& message)
285{
286 auto payloadLen = message.payload.size();
287
288 // The following logic calculates the number of padding bytes to be added to
289 // IPMI packet. If needed each integrity Pad byte is set to FFh.
Tom Joseph5a2d3ce2017-02-01 20:18:37 +0530290 auto paddingLen = 4 - ((payloadLen + 2) & 3);
Tom Joseph64703f42017-01-10 17:03:48 +0530291 packet.insert(packet.end(), paddingLen, 0xFF);
292
293 packet.resize(packet.size() + sizeof(SessionTrailer_t));
294
295 auto trailer = reinterpret_cast<SessionTrailer_t*>
296 (packet.data() + packet.size() -
297 sizeof(SessionTrailer_t));
298
299 trailer->padLength = paddingLen;
300 trailer->nextHeader = parser::RMCP_MESSAGE_CLASS_IPMI;
301
302 auto session = (std::get<session::Manager&>(singletonPool).getSession(
303 message.bmcSessionID)).lock();
304
305 auto integrityData = session->getIntegrityAlgo()->
306 generateIntegrityData(packet);
307
308 packet.insert(packet.end(), integrityData.begin(), integrityData.end());
309}
310
Tom Joseph4e57ada2016-08-10 06:51:12 -0500311} // namespace internal
312
313} // namespace ipmi20parser
314
315} // namespace message