Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "message.hpp" |
| 4 | #include "session.hpp" |
| 5 | |
| 6 | namespace message |
| 7 | { |
| 8 | |
| 9 | namespace parser |
| 10 | { |
| 11 | |
| 12 | constexpr size_t RMCP_VERSION = 6; |
| 13 | |
| 14 | // RMCP Messages with class=IPMI should be sent with an RMCP Sequence |
| 15 | // Number of FFh to indicate that an RMCP ACK message should not be |
| 16 | // generated by the message receiver. |
| 17 | constexpr size_t RMCP_SEQ = 0xFF; |
| 18 | |
| 19 | // RMCP Message Class 7h is for IPMI |
| 20 | constexpr size_t RMCP_MESSAGE_CLASS_IPMI = 7; |
| 21 | |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 22 | // RMCP Session Header Size |
| 23 | constexpr size_t RMCP_SESSION_HEADER_SIZE = 4; |
| 24 | |
Tom Joseph | 1efcb49 | 2017-01-31 16:56:47 +0530 | [diff] [blame] | 25 | // Maximum payload size |
| 26 | constexpr size_t MAX_PAYLOAD_SIZE = 255; |
| 27 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 28 | enum class SessionHeader |
| 29 | { |
| 30 | IPMI15 = 0x00, |
| 31 | IPMI20 = 0x06, |
| 32 | INVALID = 0xFF, |
| 33 | }; |
| 34 | |
| 35 | struct BasicHeader_t |
| 36 | { |
| 37 | // RMCP Header |
| 38 | uint8_t version; |
| 39 | uint8_t reserved; |
| 40 | uint8_t rmcpSeqNum; |
| 41 | uint8_t classOfMsg; |
| 42 | |
| 43 | // IPMI partial session header |
| 44 | union |
| 45 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 46 | uint8_t reserved1 : 4; |
| 47 | uint8_t authType : 4; |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 48 | uint8_t formatType; |
| 49 | } format; |
| 50 | } __attribute__((packed)); |
| 51 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 52 | /** |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 53 | * @brief Unflatten an incoming packet and prepare the IPMI message |
| 54 | * |
| 55 | * @param[in] inPacket - Incoming IPMI packet |
| 56 | * |
| 57 | * @return A tuple with IPMI message and the session header type to sent the |
| 58 | * response packet. In case of success incoming message and session |
| 59 | * header type. In case of failure nullptr and session header type |
| 60 | * would be invalid. |
| 61 | */ |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 62 | std::tuple<std::shared_ptr<Message>, SessionHeader> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 63 | unflatten(std::vector<uint8_t>& inPacket); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 64 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 65 | /** |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 66 | * @brief Flatten an IPMI message and generate the IPMI packet with the |
| 67 | * session header |
| 68 | * |
| 69 | * @param[in] outMessage - IPMI message to be flattened |
| 70 | * @param[in] authType - Session header type to be added to the IPMI |
| 71 | * packet |
| 72 | * |
| 73 | * @return IPMI packet on success |
| 74 | */ |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 75 | std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage, |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 76 | SessionHeader authType, |
| 77 | std::shared_ptr<session::Session> session); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 78 | |
| 79 | } // namespace parser |
| 80 | |
| 81 | namespace ipmi15parser |
| 82 | { |
| 83 | |
| 84 | struct SessionHeader_t |
| 85 | { |
| 86 | struct parser::BasicHeader_t base; |
| 87 | uint32_t sessSeqNum; |
| 88 | uint32_t sessId; |
| 89 | // <Optional Field: AuthCode> |
| 90 | uint8_t payloadLength; |
| 91 | } __attribute__((packed)); |
| 92 | |
| 93 | struct SessionTrailer_t |
| 94 | { |
| 95 | uint8_t legacyPad; |
| 96 | } __attribute__((packed)); |
| 97 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 98 | /** |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 99 | * @brief Unflatten an incoming packet and prepare the IPMI message |
| 100 | * |
| 101 | * @param[in] inPacket - Incoming IPMI packet |
| 102 | * |
| 103 | * @return IPMI message in the packet on success |
| 104 | */ |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 105 | std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 106 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 107 | /** |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 108 | * @brief Flatten an IPMI message and generate the IPMI packet with the |
| 109 | * session header |
| 110 | * |
| 111 | * @param[in] outMessage - IPMI message to be flattened |
| 112 | * |
| 113 | * @return IPMI packet on success |
| 114 | */ |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 115 | std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage, |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 116 | std::shared_ptr<session::Session> session); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 117 | |
| 118 | } // namespace ipmi15parser |
| 119 | |
| 120 | namespace ipmi20parser |
| 121 | { |
| 122 | |
| 123 | constexpr size_t MAX_INTEGRITY_DATA_LENGTH = 12; |
| 124 | constexpr size_t PAYLOAD_ENCRYPT_MASK = 0x80; |
| 125 | constexpr size_t PAYLOAD_AUTH_MASK = 0x40; |
| 126 | |
| 127 | struct SessionHeader_t |
| 128 | { |
| 129 | struct parser::BasicHeader_t base; |
| 130 | |
| 131 | uint8_t payloadType; |
| 132 | |
| 133 | uint32_t sessId; |
| 134 | uint32_t sessSeqNum; |
| 135 | uint16_t payloadLength; |
| 136 | } __attribute__((packed)); |
| 137 | |
| 138 | struct SessionTrailer_t |
| 139 | { |
| 140 | // Integrity Pad |
| 141 | uint8_t padLength; |
| 142 | uint8_t nextHeader; |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 143 | } __attribute__((packed)); |
| 144 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 145 | /** |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 146 | * @brief Unflatten an incoming packet and prepare the IPMI message |
| 147 | * |
| 148 | * @param[in] inPacket - Incoming IPMI packet |
| 149 | * |
| 150 | * @return IPMI message in the packet on success |
| 151 | */ |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 152 | std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 153 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 154 | /** |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 155 | * @brief Flatten an IPMI message and generate the IPMI packet with the |
| 156 | * session header |
| 157 | * |
| 158 | * @param[in] outMessage - IPMI message to be flattened |
| 159 | * |
| 160 | * @return IPMI packet on success |
| 161 | */ |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 162 | std::vector<uint8_t> flatten(std::shared_ptr<Message> outMessage, |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 163 | std::shared_ptr<session::Session> session); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 164 | |
| 165 | namespace internal |
| 166 | { |
| 167 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 168 | /** |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 169 | * @brief Add sequence number to the message |
| 170 | * |
| 171 | * @param[in] packet - outgoing packet to which to add sequence number |
| 172 | * @param[in] session - session handle |
| 173 | * |
| 174 | */ |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 175 | void addSequenceNumber(std::vector<uint8_t>& packet, |
| 176 | std::shared_ptr<session::Session> session); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 177 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 178 | /** |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 179 | * @brief Verify the integrity data of the incoming IPMI packet |
| 180 | * |
| 181 | * @param[in] packet - Incoming IPMI packet |
Tom Joseph | 027dfc2 | 2017-01-26 13:30:53 +0530 | [diff] [blame] | 182 | * @param[in] message - IPMI Message populated from the incoming packet |
| 183 | * @param[in] payloadLen - Length of the IPMI payload |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 184 | * |
| 185 | */ |
| 186 | bool verifyPacketIntegrity(const std::vector<uint8_t>& packet, |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 187 | const std::shared_ptr<Message> message, |
| 188 | size_t payloadLen); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 189 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 190 | /** |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 191 | * @brief Add Integrity data to the outgoing IPMI packet |
| 192 | * |
| 193 | * @param[in] packet - Outgoing IPMI packet |
Tom Joseph | 1404bca | 2017-01-26 14:17:02 +0530 | [diff] [blame] | 194 | * @param[in] message - IPMI Message populated for the outgoing packet |
| 195 | * @param[in] payloadLen - Length of the IPMI payload |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 196 | */ |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 197 | void addIntegrityData(std::vector<uint8_t>& packet, |
| 198 | const std::shared_ptr<Message> message, |
Tom Joseph | 1404bca | 2017-01-26 14:17:02 +0530 | [diff] [blame] | 199 | size_t payloadLen); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 200 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 201 | /** |
Tom Joseph | 78478a8 | 2017-01-26 14:24:29 +0530 | [diff] [blame] | 202 | * @brief Decrypt the encrypted payload in the incoming IPMI packet |
| 203 | * |
| 204 | * @param[in] packet - Incoming IPMI packet |
| 205 | * @param[in] message - IPMI Message populated from the incoming packet |
| 206 | * @param[in] payloadLen - Length of encrypted IPMI payload |
| 207 | * |
| 208 | * @return on successful completion, return the plain text payload |
| 209 | */ |
| 210 | std::vector<uint8_t> decryptPayload(const std::vector<uint8_t>& packet, |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 211 | const std::shared_ptr<Message> message, |
| 212 | size_t payloadLen); |
Tom Joseph | 78478a8 | 2017-01-26 14:24:29 +0530 | [diff] [blame] | 213 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 214 | /** |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 215 | * @brief Encrypt the plain text payload for the outgoing IPMI packet |
| 216 | * |
| 217 | * @param[in] message - IPMI Message populated for the outgoing packet |
| 218 | * |
| 219 | * @return on successful completion, return the encrypted payload |
| 220 | */ |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 221 | std::vector<uint8_t> encryptPayload(std::shared_ptr<Message> message); |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 222 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 223 | } // namespace internal |
| 224 | |
| 225 | } // namespace ipmi20parser |
| 226 | |
| 227 | } // namespace message |