blob: 861d69fbc22036c795289ecfcc1357b3fc7113d7 [file] [log] [blame]
Tom Josephe6361a22016-08-10 06:56:25 -05001#include "message_handler.hpp"
2
Vernon Mauery9e801a22018-10-12 13:20:49 -07003#include "command_table.hpp"
4#include "main.hpp"
5#include "message.hpp"
6#include "message_parsers.hpp"
7#include "sessions_manager.hpp"
8
Tom Josephe6361a22016-08-10 06:56:25 -05009#include <sys/socket.h>
10
Vernon Maueryfc37e592018-12-19 14:55:15 -080011#include <phosphor-logging/log.hpp>
George Liubc8958f2022-07-04 09:29:49 +080012
13#include <memory>
Tom Josephe6361a22016-08-10 06:56:25 -050014#include <string>
15#include <vector>
16
Vernon Maueryfc37e592018-12-19 14:55:15 -080017using namespace phosphor::logging;
18
Tom Josephe6361a22016-08-10 06:56:25 -050019namespace message
20{
Vernon Mauery8d6f2002018-11-07 09:55:53 -080021using namespace phosphor::logging;
Tom Josephe6361a22016-08-10 06:56:25 -050022
Vernon Mauery8d6f2002018-11-07 09:55:53 -080023bool Handler::receive()
Tom Josephe6361a22016-08-10 06:56:25 -050024{
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 Maueryfc37e592018-12-19 14:55:15 -080034 log<level::ERR>("Error in Read", entry("STATUS=%x", readStatus));
Vernon Mauery8d6f2002018-11-07 09:55:53 -080035 return false;
Tom Josephe6361a22016-08-10 06:56:25 -050036 }
37
38 // Unflatten the packet
Vernon Mauery8d6f2002018-11-07 09:55:53 -080039 std::tie(inMessage, sessionHeader) = parser::unflatten(packet);
Tom Josephe6361a22016-08-10 06:56:25 -050040
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030041 return true;
42}
43
44void Handler::updSessionData(std::shared_ptr<Message>& inMessage)
45{
Vernon Mauery2085ae02021-06-10 11:51:00 -070046 auto session = session::Manager::get().getSession(inMessage->bmcSessionID);
Tom Josephe6361a22016-08-10 06:56:25 -050047
Vernon Mauery8d6f2002018-11-07 09:55:53 -080048 sessionID = inMessage->bmcSessionID;
49 inMessage->rcSessionID = session->getRCSessionID();
Tom Josephe6361a22016-08-10 06:56:25 -050050 session->updateLastTransactionTime();
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053051 session->channelPtr = channel;
52 session->remotePort(channel->getPort());
Rajashekar Gade Reddy9979e992020-02-07 19:18:34 +053053 uint32_t ipAddr = 0;
54 channel->getRemoteAddress(ipAddr);
55 session->remoteIPAddr(ipAddr);
Tom Josephe6361a22016-08-10 06:56:25 -050056}
57
Vernon Mauery8d6f2002018-11-07 09:55:53 -080058Handler::~Handler()
59{
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030060 try
Vernon Mauery8d6f2002018-11-07 09:55:53 -080061 {
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030062#ifdef RMCP_PING
63 if (ClassOfMsg::ASF == inMessage->rmcpMsgClass)
Vernon Mauery8d6f2002018-11-07 09:55:53 -080064 {
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030065 sendASF();
Vernon Mauery8d6f2002018-11-07 09:55:53 -080066 }
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030067 else
68#endif // RMCP_PING
Vernon Mauery8d6f2002018-11-07 09:55:53 -080069 {
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030070 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 Mauery8d6f2002018-11-07 09:55:53 -080080 }
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030081 }
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 Mauery8d6f2002018-11-07 09:55:53 -080087 }
88}
89
90void Handler::processIncoming()
91{
92 // Read the incoming IPMI packet
93 if (!receive())
94 {
95 return;
96 }
97
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +030098#ifdef RMCP_PING
Vernon Mauery8d6f2002018-11-07 09:55:53 -080099 // Execute the Command, possibly asynchronously
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +0300100 if (ClassOfMsg::ASF != inMessage->rmcpMsgClass)
101#endif // RMCP_PING
102 {
103 updSessionData(inMessage);
104 executeCommand();
105 }
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800106
107 // send happens during the destructor if a payload was set
108}
109
110void Handler::executeCommand()
Tom Josephe6361a22016-08-10 06:56:25 -0500111{
112 // Get the CommandID to map into the command table
Vernon Mauery7f268e42018-10-26 10:26:01 -0700113 auto command = inMessage->getCommand();
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700114 if (inMessage->payloadType == PayloadType::IPMI)
Tom Josephe6361a22016-08-10 06:56:25 -0500115 {
Vernon Mauery2085ae02021-06-10 11:51:00 -0700116 auto session = session::Manager::get().getSession(sessionID);
Richard Marian Thomaiyar8af90eb2019-03-03 15:13:33 +0530117 // Process PayloadType::IPMI only if ipmi is enabled or for sessionless
118 // or for session establisbment command
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530119 if (this->sessionID == session::sessionZero ||
Richard Marian Thomaiyar8af90eb2019-03-03 15:13:33 +0530120 session->sessionUserPrivAccess.ipmiEnabled)
Tom Josephe6361a22016-08-10 06:56:25 -0500121 {
Richard Marian Thomaiyar8af90eb2019-03-03 15:13:33 +0530122 if (inMessage->payload.size() <
123 (sizeof(LAN::header::Request) + sizeof(LAN::trailer::Request)))
124 {
125 return;
126 }
Tom Josephe6361a22016-08-10 06:56:25 -0500127
Richard Marian Thomaiyar8af90eb2019-03-03 15:13:33 +0530128 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 Mauery2085ae02021-06-10 11:51:00 -0700132 command::Table::get().executeCommand(command, inPayload,
133 shared_from_this());
Richard Marian Thomaiyar8af90eb2019-03-03 15:13:33 +0530134 }
135 else
136 {
137 std::vector<uint8_t> payload{IPMI_CC_INSUFFICIENT_PRIVILEGE};
138 outPayload = std::move(payload);
139 }
Tom Josephe6361a22016-08-10 06:56:25 -0500140 }
141 else
142 {
Vernon Mauery2085ae02021-06-10 11:51:00 -0700143 command::Table::get().executeCommand(command, inMessage->payload,
144 shared_from_this());
Tom Josephe6361a22016-08-10 06:56:25 -0500145 }
Tom Josephe6361a22016-08-10 06:56:25 -0500146}
147
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +0300148void 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
158void 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 Maueryd999ffc2018-10-25 09:16:05 -0700168void Handler::send(std::shared_ptr<Message> outMessage)
Tom Josephe6361a22016-08-10 06:56:25 -0500169{
Vernon Mauery2085ae02021-06-10 11:51:00 -0700170 auto session = session::Manager::get().getSession(sessionID);
Tom Josephe6361a22016-08-10 06:56:25 -0500171
172 // Flatten the packet
Vernon Mauery224f36a2018-10-25 08:52:23 -0700173 auto packet = parser::flatten(outMessage, sessionHeader, session);
Tom Josephe6361a22016-08-10 06:56:25 -0500174
Tom Joseph04b30382017-04-03 01:06:51 +0530175 // Write the packet
Kirill Pakhomovde7dd5c2021-02-27 18:45:22 +0300176 writeData(packet);
Tom Josephe6361a22016-08-10 06:56:25 -0500177}
178
Tom Josephff848492017-03-31 10:43:48 +0530179void Handler::setChannelInSession() const
180{
Vernon Mauery2085ae02021-06-10 11:51:00 -0700181 auto session = session::Manager::get().getSession(sessionID);
Tom Josephff848492017-03-31 10:43:48 +0530182
183 session->channelPtr = channel;
184}
185
Vernon Mauery70fd29c2017-11-30 13:11:43 -0800186void Handler::sendSOLPayload(const std::vector<uint8_t>& input)
Tom Joseph22596f22017-03-31 10:52:27 +0530187{
Vernon Mauery2085ae02021-06-10 11:51:00 -0700188 auto session = session::Manager::get().getSession(sessionID);
Tom Joseph22596f22017-03-31 10:52:27 +0530189
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700190 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 Joseph22596f22017-03-31 10:52:27 +0530197
198 send(outMessage);
199}
200
Vernon Mauery9e801a22018-10-12 13:20:49 -0700201void Handler::sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd,
Tom Joseph63d3e492017-03-31 11:01:08 +0530202 const std::vector<uint8_t>& output)
203{
Vernon Mauery2085ae02021-06-10 11:51:00 -0700204 auto session = session::Manager::get().getSession(sessionID);
Tom Joseph63d3e492017-03-31 11:01:08 +0530205
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700206 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 Joseph63d3e492017-03-31 11:01:08 +0530212
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700213 outMessage->payload.resize(sizeof(LAN::header::Request) + output.size() +
214 sizeof(LAN::trailer::Request));
Tom Joseph63d3e492017-03-31 11:01:08 +0530215
Vernon Mauery9e801a22018-10-12 13:20:49 -0700216 auto respHeader =
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700217 reinterpret_cast<LAN::header::Request*>(outMessage->payload.data());
Tom Joseph63d3e492017-03-31 11:01:08 +0530218
219 // Add IPMI LAN Message Request Header
220 respHeader->rsaddr = LAN::requesterBMCAddress;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700221 respHeader->netfn = (netfn << 0x02);
222 respHeader->cs = crc8bit(&(respHeader->rsaddr), 2);
Tom Joseph63d3e492017-03-31 11:01:08 +0530223 respHeader->rqaddr = LAN::responderBMCAddress;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700224 respHeader->rqseq = 0;
225 respHeader->cmd = cmd;
Tom Joseph63d3e492017-03-31 11:01:08 +0530226
227 auto assembledSize = sizeof(LAN::header::Request);
228
229 // Copy the output by the execution of the command
Vernon Mauery9e801a22018-10-12 13:20:49 -0700230 std::copy(output.begin(), output.end(),
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700231 outMessage->payload.begin() + assembledSize);
Tom Joseph63d3e492017-03-31 11:01:08 +0530232 assembledSize += output.size();
233
234 // Add the IPMI LAN Message Trailer
Vernon Mauery9e801a22018-10-12 13:20:49 -0700235 auto trailer = reinterpret_cast<LAN::trailer::Request*>(
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700236 outMessage->payload.data() + assembledSize);
Tom Joseph63d3e492017-03-31 11:01:08 +0530237
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 Mauery9e801a22018-10-12 13:20:49 -0700246} // namespace message