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