summaryrefslogtreecommitdiffstats
path: root/message_parsers.hpp
blob: 489eb2328be69229f5b0021977be44065d8eb6bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#pragma once

#include "message.hpp"
#include "session.hpp"

namespace message
{

namespace parser
{

constexpr size_t RMCP_VERSION = 6;

// RMCP Messages with class=IPMI should be sent with an RMCP Sequence
// Number of FFh to indicate that an RMCP ACK message should not be
// generated by the message receiver.
constexpr size_t RMCP_SEQ = 0xFF;

// RMCP Message Class 7h is for IPMI
constexpr size_t RMCP_MESSAGE_CLASS_IPMI = 7;

// RMCP Session Header Size
constexpr size_t RMCP_SESSION_HEADER_SIZE = 4;

// Maximum payload size
constexpr size_t MAX_PAYLOAD_SIZE = 255;

enum class SessionHeader
{
    IPMI15 = 0x00,
    IPMI20 = 0x06,
    INVALID = 0xFF,
};

struct BasicHeader_t
{
    // RMCP Header
    uint8_t version;
    uint8_t reserved;
    uint8_t rmcpSeqNum;
    uint8_t classOfMsg;

    // IPMI partial session header
    union
    {
        uint8_t reserved1 : 4;
        uint8_t authType : 4;
        uint8_t formatType;
    } format;
} __attribute__((packed));

/**
 * @brief Unflatten an incoming packet and prepare the IPMI message
 *
 * @param[in] inPacket - Incoming IPMI packet
 *
 * @return A tuple with IPMI message and the session header type to sent the
 *         response packet. In case of success incoming message and session
 *         header type. In case of failure nullptr and session header type
 *         would be invalid.
 */
std::tuple<std::shared_ptr<Message>, SessionHeader>
    unflatten(std::vector<uint8_t>& inPacket);

/**
 * @brief Flatten an IPMI message and generate the IPMI packet with the
 *        session header
 *
 * @param[in] outMessage - IPMI message to be flattened
 * @param[in] authType - Session header type to be added to the IPMI
 *                       packet
 *
 * @return IPMI packet on success
 */
std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage,
                             SessionHeader authType,
                             std::shared_ptr<session::Session> session);

} // namespace parser

namespace ipmi15parser
{

struct SessionHeader_t
{
    struct parser::BasicHeader_t base;
    uint32_t sessSeqNum;
    uint32_t sessId;
    // <Optional Field: AuthCode>
    uint8_t payloadLength;
} __attribute__((packed));

struct SessionTrailer_t
{
    uint8_t legacyPad;
} __attribute__((packed));

/**
 * @brief Unflatten an incoming packet and prepare the IPMI message
 *
 * @param[in] inPacket - Incoming IPMI packet
 *
 * @return IPMI message in the packet on success
 */
std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket);

/**
 * @brief Flatten an IPMI message and generate the IPMI packet with the
 *        session header
 *
 * @param[in] outMessage - IPMI message to be flattened
 *
 * @return IPMI packet on success
 */
std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage,
                             std::shared_ptr<session::Session> session);

} // namespace ipmi15parser

namespace ipmi20parser
{

constexpr size_t MAX_INTEGRITY_DATA_LENGTH = 12;
constexpr size_t PAYLOAD_ENCRYPT_MASK = 0x80;
constexpr size_t PAYLOAD_AUTH_MASK = 0x40;

struct SessionHeader_t
{
    struct parser::BasicHeader_t base;

    uint8_t payloadType;

    uint32_t sessId;
    uint32_t sessSeqNum;
    uint16_t payloadLength;
} __attribute__((packed));

struct SessionTrailer_t
{
    // Integrity Pad
    uint8_t padLength;
    uint8_t nextHeader;
} __attribute__((packed));

/**
 * @brief Unflatten an incoming packet and prepare the IPMI message
 *
 * @param[in] inPacket - Incoming IPMI packet
 *
 * @return IPMI message in the packet on success
 */
std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket);

/**
 * @brief Flatten an IPMI message and generate the IPMI packet with the
 *        session header
 *
 * @param[in] outMessage - IPMI message to be flattened
 *
 * @return IPMI packet on success
 */
std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage,
                             std::shared_ptr<session::Session> session);

namespace internal
{

/**
 * @brief Add sequence number to the message
 *
 * @param[in] packet - outgoing packet to which to add sequence number
 * @param[in] session - session handle
 *
 */
void addSequenceNumber(std::vector<uint8_t>& packet,
                       std::shared_ptr<session::Session> session);

/**
 * @brief Verify the integrity data of the incoming IPMI packet
 *
 * @param[in] packet - Incoming IPMI packet
 * @param[in] message - IPMI Message populated from the incoming packet
 * @param[in] payloadLen - Length of the IPMI payload
 *
 */
bool verifyPacketIntegrity(const std::vector<uint8_t>& packet,
                           const std::shared_ptr<Message> message,
                           size_t payloadLen);

/**
 * @brief Add Integrity data to the outgoing IPMI packet
 *
 * @param[in] packet - Outgoing IPMI packet
 * @param[in] message - IPMI Message populated for the outgoing packet
 * @param[in] payloadLen - Length of the IPMI payload
 */
void addIntegrityData(std::vector<uint8_t>& packet,
                      const std::shared_ptr<Message> message,
                      size_t payloadLen);

/**
 * @brief Decrypt the encrypted payload in the incoming IPMI packet
 *
 * @param[in] packet - Incoming IPMI packet
 * @param[in] message - IPMI Message populated from the incoming packet
 * @param[in] payloadLen - Length of encrypted IPMI payload
 *
 * @return on successful completion, return the plain text payload
 */
std::vector<uint8_t> decryptPayload(const std::vector<uint8_t>& packet,
                                    const std::shared_ptr<Message> message,
                                    size_t payloadLen);

/**
 * @brief Encrypt the plain text payload for the outgoing IPMI packet
 *
 * @param[in] message - IPMI Message populated for the outgoing packet
 *
 * @return on successful completion, return the encrypted payload
 */
std::vector<uint8_t> encryptPayload(std::shared_ptr<Message> message);

} // namespace internal

} // namespace ipmi20parser

} // namespace message
OpenPOWER on IntegriCloud