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