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 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 11 | #include <memory> |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 16 | using namespace phosphor::logging; |
| 17 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 18 | namespace message |
| 19 | { |
| 20 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 21 | std::shared_ptr<Message> Handler::receive() |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 22 | { |
| 23 | std::vector<uint8_t> packet; |
| 24 | auto readStatus = 0; |
| 25 | |
| 26 | // Read the packet |
| 27 | std::tie(readStatus, packet) = channel->read(); |
| 28 | |
| 29 | // Read of the packet failed |
| 30 | if (readStatus < 0) |
| 31 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 32 | log<level::ERR>("Error in Read", entry("STATUS=%x", readStatus)); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 33 | return nullptr; |
| 34 | } |
| 35 | |
| 36 | // Unflatten the packet |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 37 | std::shared_ptr<Message> message; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 38 | std::tie(message, sessionHeader) = parser::unflatten(packet); |
| 39 | |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 40 | auto session = std::get<session::Manager&>(singletonPool) |
| 41 | .getSession(message->bmcSessionID); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 42 | |
| 43 | sessionID = message->bmcSessionID; |
| 44 | message->rcSessionID = session->getRCSessionID(); |
| 45 | session->updateLastTransactionTime(); |
| 46 | |
| 47 | return message; |
| 48 | } |
| 49 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 50 | std::shared_ptr<Message> |
| 51 | Handler::executeCommand(std::shared_ptr<Message> inMessage) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 52 | { |
| 53 | // Get the CommandID to map into the command table |
Vernon Mauery | 7f268e4 | 2018-10-26 10:26:01 -0700 | [diff] [blame^] | 54 | auto command = inMessage->getCommand(); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 55 | std::vector<uint8_t> output{}; |
| 56 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 57 | if (inMessage->payloadType == PayloadType::IPMI) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 58 | { |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 59 | if (inMessage->payload.size() < |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 60 | (sizeof(LAN::header::Request) + sizeof(LAN::trailer::Request))) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 61 | { |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 65 | auto start = inMessage->payload.begin() + sizeof(LAN::header::Request); |
| 66 | auto end = inMessage->payload.end() - sizeof(LAN::trailer::Request); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 67 | std::vector<uint8_t> inPayload(start, end); |
| 68 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 69 | output = std::get<command::Table&>(singletonPool) |
| 70 | .executeCommand(command, inPayload, *this); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 71 | } |
| 72 | else |
| 73 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 74 | output = std::get<command::Table&>(singletonPool) |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 75 | .executeCommand(command, inMessage->payload, *this); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 76 | } |
Vernon Mauery | 7f268e4 | 2018-10-26 10:26:01 -0700 | [diff] [blame^] | 77 | return inMessage->createResponse(output); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 80 | void Handler::send(std::shared_ptr<Message> outMessage) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 81 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 82 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 83 | std::get<session::Manager&>(singletonPool).getSession(sessionID); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 84 | |
| 85 | // Flatten the packet |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 86 | auto packet = parser::flatten(outMessage, sessionHeader, session); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 87 | |
Tom Joseph | 04b3038 | 2017-04-03 01:06:51 +0530 | [diff] [blame] | 88 | // Write the packet |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 89 | auto writeStatus = channel->write(packet); |
| 90 | if (writeStatus < 0) |
| 91 | { |
Tom Joseph | 04b3038 | 2017-04-03 01:06:51 +0530 | [diff] [blame] | 92 | throw std::runtime_error("Error in writing to socket"); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 93 | } |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 96 | void Handler::setChannelInSession() const |
| 97 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 98 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 99 | std::get<session::Manager&>(singletonPool).getSession(sessionID); |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 100 | |
| 101 | session->channelPtr = channel; |
| 102 | } |
| 103 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 104 | void Handler::sendSOLPayload(const std::vector<uint8_t>& input) |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 105 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 106 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 107 | std::get<session::Manager&>(singletonPool).getSession(sessionID); |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 108 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 109 | auto outMessage = std::make_shared<Message>(); |
| 110 | outMessage->payloadType = PayloadType::SOL; |
| 111 | outMessage->payload = input; |
| 112 | outMessage->isPacketEncrypted = session->isCryptAlgoEnabled(); |
| 113 | outMessage->isPacketAuthenticated = session->isIntegrityAlgoEnabled(); |
| 114 | outMessage->rcSessionID = session->getRCSessionID(); |
| 115 | outMessage->bmcSessionID = sessionID; |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 116 | |
| 117 | send(outMessage); |
| 118 | } |
| 119 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 120 | void Handler::sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd, |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 121 | const std::vector<uint8_t>& output) |
| 122 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 123 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 124 | std::get<session::Manager&>(singletonPool).getSession(sessionID); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 125 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 126 | auto outMessage = std::make_shared<Message>(); |
| 127 | outMessage->payloadType = PayloadType::IPMI; |
| 128 | outMessage->isPacketEncrypted = session->isCryptAlgoEnabled(); |
| 129 | outMessage->isPacketAuthenticated = session->isIntegrityAlgoEnabled(); |
| 130 | outMessage->rcSessionID = session->getRCSessionID(); |
| 131 | outMessage->bmcSessionID = sessionID; |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 132 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 133 | outMessage->payload.resize(sizeof(LAN::header::Request) + output.size() + |
| 134 | sizeof(LAN::trailer::Request)); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 135 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 136 | auto respHeader = |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 137 | reinterpret_cast<LAN::header::Request*>(outMessage->payload.data()); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 138 | |
| 139 | // Add IPMI LAN Message Request Header |
| 140 | respHeader->rsaddr = LAN::requesterBMCAddress; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 141 | respHeader->netfn = (netfn << 0x02); |
| 142 | respHeader->cs = crc8bit(&(respHeader->rsaddr), 2); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 143 | respHeader->rqaddr = LAN::responderBMCAddress; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 144 | respHeader->rqseq = 0; |
| 145 | respHeader->cmd = cmd; |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 146 | |
| 147 | auto assembledSize = sizeof(LAN::header::Request); |
| 148 | |
| 149 | // Copy the output by the execution of the command |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 150 | std::copy(output.begin(), output.end(), |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 151 | outMessage->payload.begin() + assembledSize); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 152 | assembledSize += output.size(); |
| 153 | |
| 154 | // Add the IPMI LAN Message Trailer |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 155 | auto trailer = reinterpret_cast<LAN::trailer::Request*>( |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 156 | outMessage->payload.data() + assembledSize); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 157 | |
| 158 | // Calculate the checksum for the field rqaddr in the header to the |
| 159 | // command data, 3 corresponds to size of the fields before rqaddr( rsaddr, |
| 160 | // netfn, cs). |
| 161 | trailer->checksum = crc8bit(&respHeader->rqaddr, assembledSize - 3); |
| 162 | |
| 163 | send(outMessage); |
| 164 | } |
| 165 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 166 | } // namespace message |