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 | |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame^] | 11 | #include <phosphor-logging/lg2.hpp> |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 17 | namespace message |
| 18 | { |
| 19 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 20 | bool Handler::receive() |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 21 | { |
| 22 | std::vector<uint8_t> packet; |
| 23 | auto readStatus = 0; |
| 24 | |
| 25 | // Read the packet |
| 26 | std::tie(readStatus, packet) = channel->read(); |
| 27 | |
| 28 | // Read of the packet failed |
| 29 | if (readStatus < 0) |
| 30 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame^] | 31 | lg2::error("Error in Read status: {STATUS}", "STATUS", readStatus); |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 32 | return false; |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Unflatten the packet |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 36 | std::tie(inMessage, sessionHeader) = parser::unflatten(packet); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 37 | |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 38 | return true; |
| 39 | } |
| 40 | |
| 41 | void Handler::updSessionData(std::shared_ptr<Message>& inMessage) |
| 42 | { |
Lei YU | 0a4dde4 | 2022-07-12 19:42:15 +0800 | [diff] [blame] | 43 | session = session::Manager::get().getSession(inMessage->bmcSessionID); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 44 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 45 | sessionID = inMessage->bmcSessionID; |
| 46 | inMessage->rcSessionID = session->getRCSessionID(); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 47 | session->updateLastTransactionTime(); |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 48 | session->channelPtr = channel; |
| 49 | session->remotePort(channel->getPort()); |
Rajashekar Gade Reddy | 9979e99 | 2020-02-07 19:18:34 +0530 | [diff] [blame] | 50 | uint32_t ipAddr = 0; |
| 51 | channel->getRemoteAddress(ipAddr); |
| 52 | session->remoteIPAddr(ipAddr); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 55 | Handler::~Handler() |
| 56 | { |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 57 | try |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 58 | { |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 59 | #ifdef RMCP_PING |
| 60 | if (ClassOfMsg::ASF == inMessage->rmcpMsgClass) |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 61 | { |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 62 | sendASF(); |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 63 | } |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 64 | else |
| 65 | #endif // RMCP_PING |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 66 | { |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 67 | if (outPayload) |
| 68 | { |
| 69 | std::shared_ptr<Message> outMessage = |
| 70 | inMessage->createResponse(*outPayload); |
| 71 | if (!outMessage) |
| 72 | { |
| 73 | return; |
| 74 | } |
| 75 | send(outMessage); |
| 76 | } |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 77 | } |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 78 | } |
| 79 | catch (const std::exception& e) |
| 80 | { |
| 81 | // send failed, most likely due to a session closure |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame^] | 82 | lg2::info("Async RMCP+ reply failed: {ERROR}", "ERROR", e); |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | void Handler::processIncoming() |
| 87 | { |
| 88 | // Read the incoming IPMI packet |
| 89 | if (!receive()) |
| 90 | { |
| 91 | return; |
| 92 | } |
| 93 | |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 94 | #ifdef RMCP_PING |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 95 | // Execute the Command, possibly asynchronously |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 96 | if (ClassOfMsg::ASF != inMessage->rmcpMsgClass) |
| 97 | #endif // RMCP_PING |
| 98 | { |
| 99 | updSessionData(inMessage); |
| 100 | executeCommand(); |
| 101 | } |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 102 | |
| 103 | // send happens during the destructor if a payload was set |
| 104 | } |
| 105 | |
| 106 | void Handler::executeCommand() |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 107 | { |
| 108 | // Get the CommandID to map into the command table |
Vernon Mauery | 7f268e4 | 2018-10-26 10:26:01 -0700 | [diff] [blame] | 109 | auto command = inMessage->getCommand(); |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 110 | if (inMessage->payloadType == PayloadType::IPMI) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 111 | { |
Richard Marian Thomaiyar | 8af90eb | 2019-03-03 15:13:33 +0530 | [diff] [blame] | 112 | // Process PayloadType::IPMI only if ipmi is enabled or for sessionless |
| 113 | // or for session establisbment command |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 114 | if (this->sessionID == session::sessionZero || |
Richard Marian Thomaiyar | 8af90eb | 2019-03-03 15:13:33 +0530 | [diff] [blame] | 115 | session->sessionUserPrivAccess.ipmiEnabled) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 116 | { |
Richard Marian Thomaiyar | 8af90eb | 2019-03-03 15:13:33 +0530 | [diff] [blame] | 117 | if (inMessage->payload.size() < |
| 118 | (sizeof(LAN::header::Request) + sizeof(LAN::trailer::Request))) |
| 119 | { |
| 120 | return; |
| 121 | } |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 122 | |
Richard Marian Thomaiyar | 8af90eb | 2019-03-03 15:13:33 +0530 | [diff] [blame] | 123 | auto start = |
| 124 | inMessage->payload.begin() + sizeof(LAN::header::Request); |
| 125 | auto end = inMessage->payload.end() - sizeof(LAN::trailer::Request); |
| 126 | std::vector<uint8_t> inPayload(start, end); |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 127 | command::Table::get().executeCommand(command, inPayload, |
| 128 | shared_from_this()); |
Richard Marian Thomaiyar | 8af90eb | 2019-03-03 15:13:33 +0530 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
| 132 | std::vector<uint8_t> payload{IPMI_CC_INSUFFICIENT_PRIVILEGE}; |
| 133 | outPayload = std::move(payload); |
| 134 | } |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 138 | command::Table::get().executeCommand(command, inMessage->payload, |
| 139 | shared_from_this()); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 140 | } |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 143 | void Handler::writeData(const std::vector<uint8_t>& packet) |
| 144 | { |
| 145 | auto writeStatus = channel->write(packet); |
| 146 | if (writeStatus < 0) |
| 147 | { |
| 148 | throw std::runtime_error("Error in writing to socket"); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | #ifdef RMCP_PING |
| 153 | void Handler::sendASF() |
| 154 | { |
| 155 | // Flatten the packet |
| 156 | auto packet = asfparser::flatten(inMessage->asfMsgTag); |
| 157 | |
| 158 | // Write the packet |
| 159 | writeData(packet); |
| 160 | } |
| 161 | #endif // RMCP_PING |
| 162 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 163 | void Handler::send(std::shared_ptr<Message> outMessage) |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 164 | { |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 165 | // Flatten the packet |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 166 | auto packet = parser::flatten(outMessage, sessionHeader, session); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 167 | |
Tom Joseph | 04b3038 | 2017-04-03 01:06:51 +0530 | [diff] [blame] | 168 | // Write the packet |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 169 | writeData(packet); |
Tom Joseph | e6361a2 | 2016-08-10 06:56:25 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 172 | void Handler::setChannelInSession() const |
| 173 | { |
Tom Joseph | ff84849 | 2017-03-31 10:43:48 +0530 | [diff] [blame] | 174 | session->channelPtr = channel; |
| 175 | } |
| 176 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 177 | void Handler::sendSOLPayload(const std::vector<uint8_t>& input) |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 178 | { |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 179 | auto outMessage = std::make_shared<Message>(); |
| 180 | outMessage->payloadType = PayloadType::SOL; |
| 181 | outMessage->payload = input; |
| 182 | outMessage->isPacketEncrypted = session->isCryptAlgoEnabled(); |
| 183 | outMessage->isPacketAuthenticated = session->isIntegrityAlgoEnabled(); |
| 184 | outMessage->rcSessionID = session->getRCSessionID(); |
| 185 | outMessage->bmcSessionID = sessionID; |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 186 | |
| 187 | send(outMessage); |
| 188 | } |
| 189 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 190 | void Handler::sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd, |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 191 | const std::vector<uint8_t>& output) |
| 192 | { |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 193 | auto outMessage = std::make_shared<Message>(); |
| 194 | outMessage->payloadType = PayloadType::IPMI; |
| 195 | outMessage->isPacketEncrypted = session->isCryptAlgoEnabled(); |
| 196 | outMessage->isPacketAuthenticated = session->isIntegrityAlgoEnabled(); |
| 197 | outMessage->rcSessionID = session->getRCSessionID(); |
| 198 | outMessage->bmcSessionID = sessionID; |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 199 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 200 | outMessage->payload.resize(sizeof(LAN::header::Request) + output.size() + |
| 201 | sizeof(LAN::trailer::Request)); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 202 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 203 | auto respHeader = |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 204 | reinterpret_cast<LAN::header::Request*>(outMessage->payload.data()); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 205 | |
| 206 | // Add IPMI LAN Message Request Header |
| 207 | respHeader->rsaddr = LAN::requesterBMCAddress; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 208 | respHeader->netfn = (netfn << 0x02); |
| 209 | respHeader->cs = crc8bit(&(respHeader->rsaddr), 2); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 210 | respHeader->rqaddr = LAN::responderBMCAddress; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 211 | respHeader->rqseq = 0; |
| 212 | respHeader->cmd = cmd; |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 213 | |
| 214 | auto assembledSize = sizeof(LAN::header::Request); |
| 215 | |
| 216 | // Copy the output by the execution of the command |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 217 | std::copy(output.begin(), output.end(), |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 218 | outMessage->payload.begin() + assembledSize); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 219 | assembledSize += output.size(); |
| 220 | |
| 221 | // Add the IPMI LAN Message Trailer |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 222 | auto trailer = reinterpret_cast<LAN::trailer::Request*>( |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 223 | outMessage->payload.data() + assembledSize); |
Tom Joseph | 63d3e49 | 2017-03-31 11:01:08 +0530 | [diff] [blame] | 224 | |
| 225 | // Calculate the checksum for the field rqaddr in the header to the |
| 226 | // command data, 3 corresponds to size of the fields before rqaddr( rsaddr, |
| 227 | // netfn, cs). |
| 228 | trailer->checksum = crc8bit(&respHeader->rqaddr, assembledSize - 3); |
| 229 | |
| 230 | send(outMessage); |
| 231 | } |
| 232 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 233 | } // namespace message |