Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 1 | #include "message_handler.hpp" |
| 2 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 3 | #include "command_table.hpp" |
| 4 | #include "main.hpp" |
| 5 | #include "message.hpp" |
| 6 | #include "message_parsers.hpp" |
| 7 | #include "sessions_manager.hpp" |
| 8 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | |
| 11 | #include <iostream> |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 16 | namespace message |
| 17 | { |
| 18 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 19 | std::shared_ptr<Message> Handler::receive() |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 20 | { |
| 21 | std::vector<uint8_t> packet; |
| 22 | auto readStatus = 0; |
| 23 | |
| 24 | // Read the packet |
| 25 | std::tie(readStatus, packet) = channel->read(); |
| 26 | |
| 27 | // Read of the packet failed |
| 28 | if (readStatus < 0) |
| 29 | { |
| 30 | std::cerr << "E> Error in Read : " << std::hex << readStatus << "\n"; |
| 31 | return nullptr; |
| 32 | } |
| 33 | |
| 34 | // Unflatten the packet |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 35 | std::shared_ptr<Message> message; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 36 | std::tie(message, sessionHeader) = parser::unflatten(packet); |
| 37 | |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 38 | auto session = std::get<session::Manager&>(singletonPool) |
| 39 | .getSession(message->bmcSessionID); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 40 | |
| 41 | sessionID = message->bmcSessionID; |
| 42 | message->rcSessionID = session->getRCSessionID(); |
| 43 | session->updateLastTransactionTime(); |
| 44 | |
| 45 | return message; |
| 46 | } |
| 47 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 48 | template <> |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 49 | std::shared_ptr<Message> Handler::createResponse<PayloadType::IPMI>( |
| 50 | std::vector<uint8_t>& output, std::shared_ptr<Message> inMessage) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 51 | { |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 52 | auto outMessage = std::make_shared<Message>(); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 53 | outMessage->payloadType = PayloadType::IPMI; |
| 54 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 55 | outMessage->payload.resize(sizeof(LAN::header::Response) + output.size() + |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 56 | sizeof(LAN::trailer::Response)); |
| 57 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 58 | auto reqHeader = |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 59 | reinterpret_cast<LAN::header::Request*>(inMessage->payload.data()); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 60 | auto respHeader = |
| 61 | reinterpret_cast<LAN::header::Response*>(outMessage->payload.data()); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 62 | |
| 63 | // Add IPMI LAN Message Response Header |
| 64 | respHeader->rqaddr = reqHeader->rqaddr; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 65 | respHeader->netfn = reqHeader->netfn | 0x04; |
| 66 | respHeader->cs = crc8bit(&(respHeader->rqaddr), 2); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 67 | respHeader->rsaddr = reqHeader->rsaddr; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 68 | respHeader->rqseq = reqHeader->rqseq; |
| 69 | respHeader->cmd = reqHeader->cmd; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 70 | |
| 71 | auto assembledSize = sizeof(LAN::header::Response); |
| 72 | |
| 73 | // Copy the output by the execution of the command |
| 74 | std::copy(output.begin(), output.end(), |
| 75 | outMessage->payload.begin() + assembledSize); |
| 76 | assembledSize += output.size(); |
| 77 | |
| 78 | // Add the IPMI LAN Message Trailer |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 79 | auto trailer = reinterpret_cast<LAN::trailer::Response*>( |
| 80 | outMessage->payload.data() + assembledSize); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 81 | trailer->checksum = crc8bit(&respHeader->rsaddr, assembledSize - 3); |
| 82 | |
| 83 | return outMessage; |
| 84 | } |
| 85 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 86 | std::shared_ptr<Message> |
| 87 | Handler::executeCommand(std::shared_ptr<Message> inMessage) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 88 | { |
| 89 | // Get the CommandID to map into the command table |
| 90 | auto command = getCommand(inMessage); |
| 91 | std::vector<uint8_t> output{}; |
| 92 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 93 | if (inMessage->payloadType == PayloadType::IPMI) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 94 | { |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 95 | if (inMessage->payload.size() < |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 96 | (sizeof(LAN::header::Request) + sizeof(LAN::trailer::Request))) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 97 | { |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 101 | auto start = inMessage->payload.begin() + sizeof(LAN::header::Request); |
| 102 | auto end = inMessage->payload.end() - sizeof(LAN::trailer::Request); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 103 | std::vector<uint8_t> inPayload(start, end); |
| 104 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 105 | output = std::get<command::Table&>(singletonPool) |
| 106 | .executeCommand(command, inPayload, *this); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 107 | } |
| 108 | else |
| 109 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 110 | output = std::get<command::Table&>(singletonPool) |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 111 | .executeCommand(command, inMessage->payload, *this); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 114 | std::shared_ptr<Message> outMessage = nullptr; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 115 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 116 | switch (inMessage->payloadType) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 117 | { |
| 118 | case PayloadType::IPMI: |
| 119 | outMessage = createResponse<PayloadType::IPMI>(output, inMessage); |
| 120 | break; |
| 121 | case PayloadType::OPEN_SESSION_REQUEST: |
| 122 | outMessage = createResponse<PayloadType::OPEN_SESSION_RESPONSE>( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 123 | output, inMessage); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 124 | break; |
| 125 | case PayloadType::RAKP1: |
| 126 | outMessage = createResponse<PayloadType::RAKP2>(output, inMessage); |
| 127 | break; |
| 128 | case PayloadType::RAKP3: |
| 129 | outMessage = createResponse<PayloadType::RAKP4>(output, inMessage); |
| 130 | break; |
Tom Joseph | 703adbf | 2017-03-31 10:50:22 +0530 | [diff] [blame] | 131 | case PayloadType::SOL: |
| 132 | return outMessage; |
| 133 | break; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 134 | default: |
| 135 | break; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 138 | outMessage->isPacketEncrypted = inMessage->isPacketEncrypted; |
| 139 | outMessage->isPacketAuthenticated = inMessage->isPacketAuthenticated; |
| 140 | outMessage->rcSessionID = inMessage->rcSessionID; |
| 141 | outMessage->bmcSessionID = inMessage->bmcSessionID; |
Tom Joseph | 8dfa0f0 | 2017-01-10 15:38:20 +0530 | [diff] [blame] | 142 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 143 | return outMessage; |
| 144 | } |
| 145 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 146 | uint32_t Handler::getCommand(std::shared_ptr<Message> message) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 147 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 148 | uint32_t command = 0; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 149 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 150 | command |= (static_cast<uint8_t>(message->payloadType) << 16); |
| 151 | if (message->payloadType == PayloadType::IPMI) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 152 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 153 | command |= |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 154 | ((reinterpret_cast<LAN::header::Request*>(message->payload.data())) |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 155 | ->netfn) |
| 156 | << 8; |
| 157 | command |= |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 158 | (reinterpret_cast<LAN::header::Request*>(message->payload.data())) |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 159 | ->cmd; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return command; |
| 163 | } |
| 164 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 165 | void Handler::send(std::shared_ptr<Message> outMessage) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 166 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 167 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 168 | std::get<session::Manager&>(singletonPool).getSession(sessionID); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 169 | |
| 170 | // Flatten the packet |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame^] | 171 | auto packet = parser::flatten(outMessage, sessionHeader, session); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 172 | |
Tom Joseph | 04b3038 | 2017-04-03 01:06:51 +0530 | [diff] [blame] | 173 | // Write the packet |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 174 | auto writeStatus = channel->write(packet); |
| 175 | if (writeStatus < 0) |
| 176 | { |
Tom Joseph | 04b3038 | 2017-04-03 01:06:51 +0530 | [diff] [blame] | 177 | throw std::runtime_error("Error in writing to socket"); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 178 | } |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 181 | void Handler::setChannelInSession() const |
| 182 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 183 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 184 | std::get<session::Manager&>(singletonPool).getSession(sessionID); |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 185 | |
| 186 | session->channelPtr = channel; |
| 187 | } |
| 188 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 189 | void Handler::sendSOLPayload(const std::vector<uint8_t>& input) |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 190 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 191 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 192 | std::get<session::Manager&>(singletonPool).getSession(sessionID); |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 193 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 194 | auto outMessage = std::make_shared<Message>(); |
| 195 | outMessage->payloadType = PayloadType::SOL; |
| 196 | outMessage->payload = input; |
| 197 | outMessage->isPacketEncrypted = session->isCryptAlgoEnabled(); |
| 198 | outMessage->isPacketAuthenticated = session->isIntegrityAlgoEnabled(); |
| 199 | outMessage->rcSessionID = session->getRCSessionID(); |
| 200 | outMessage->bmcSessionID = sessionID; |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 201 | |
| 202 | send(outMessage); |
| 203 | } |
| 204 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 205 | void Handler::sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd, |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 206 | const std::vector<uint8_t>& output) |
| 207 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 208 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 209 | std::get<session::Manager&>(singletonPool).getSession(sessionID); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 210 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 211 | auto outMessage = std::make_shared<Message>(); |
| 212 | outMessage->payloadType = PayloadType::IPMI; |
| 213 | outMessage->isPacketEncrypted = session->isCryptAlgoEnabled(); |
| 214 | outMessage->isPacketAuthenticated = session->isIntegrityAlgoEnabled(); |
| 215 | outMessage->rcSessionID = session->getRCSessionID(); |
| 216 | outMessage->bmcSessionID = sessionID; |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 217 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 218 | outMessage->payload.resize(sizeof(LAN::header::Request) + output.size() + |
| 219 | sizeof(LAN::trailer::Request)); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 220 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 221 | auto respHeader = |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 222 | reinterpret_cast<LAN::header::Request*>(outMessage->payload.data()); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 223 | |
| 224 | // Add IPMI LAN Message Request Header |
| 225 | respHeader->rsaddr = LAN::requesterBMCAddress; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 226 | respHeader->netfn = (netfn << 0x02); |
| 227 | respHeader->cs = crc8bit(&(respHeader->rsaddr), 2); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 228 | respHeader->rqaddr = LAN::responderBMCAddress; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 229 | respHeader->rqseq = 0; |
| 230 | respHeader->cmd = cmd; |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 231 | |
| 232 | auto assembledSize = sizeof(LAN::header::Request); |
| 233 | |
| 234 | // Copy the output by the execution of the command |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 235 | std::copy(output.begin(), output.end(), |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 236 | outMessage->payload.begin() + assembledSize); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 237 | assembledSize += output.size(); |
| 238 | |
| 239 | // Add the IPMI LAN Message Trailer |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 240 | auto trailer = reinterpret_cast<LAN::trailer::Request*>( |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 241 | outMessage->payload.data() + assembledSize); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 242 | |
| 243 | // Calculate the checksum for the field rqaddr in the header to the |
| 244 | // command data, 3 corresponds to size of the fields before rqaddr( rsaddr, |
| 245 | // netfn, cs). |
| 246 | trailer->checksum = crc8bit(&respHeader->rqaddr, assembledSize - 3); |
| 247 | |
| 248 | send(outMessage); |
| 249 | } |
| 250 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 251 | } // namespace message |