blob: e2aafb38017fe91c38c2d2b359795562381fb72a [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
Tom Josephe6361a22016-08-10 06:56:25 -050011#include <memory>
Vernon Maueryfc37e592018-12-19 14:55:15 -080012#include <phosphor-logging/log.hpp>
Tom Josephe6361a22016-08-10 06:56:25 -050013#include <string>
14#include <vector>
15
Vernon Maueryfc37e592018-12-19 14:55:15 -080016using namespace phosphor::logging;
17
Tom Josephe6361a22016-08-10 06:56:25 -050018namespace message
19{
Vernon Mauery8d6f2002018-11-07 09:55:53 -080020using namespace phosphor::logging;
Tom Josephe6361a22016-08-10 06:56:25 -050021
Vernon Mauery8d6f2002018-11-07 09:55:53 -080022bool Handler::receive()
Tom Josephe6361a22016-08-10 06:56:25 -050023{
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 Maueryfc37e592018-12-19 14:55:15 -080033 log<level::ERR>("Error in Read", entry("STATUS=%x", readStatus));
Vernon Mauery8d6f2002018-11-07 09:55:53 -080034 return false;
Tom Josephe6361a22016-08-10 06:56:25 -050035 }
36
37 // Unflatten the packet
Vernon Mauery8d6f2002018-11-07 09:55:53 -080038 std::tie(inMessage, sessionHeader) = parser::unflatten(packet);
Tom Josephe6361a22016-08-10 06:56:25 -050039
Vernon Maueryae1fda42018-10-15 12:55:34 -070040 auto session = std::get<session::Manager&>(singletonPool)
Vernon Mauery8d6f2002018-11-07 09:55:53 -080041 .getSession(inMessage->bmcSessionID);
Tom Josephe6361a22016-08-10 06:56:25 -050042
Vernon Mauery8d6f2002018-11-07 09:55:53 -080043 sessionID = inMessage->bmcSessionID;
44 inMessage->rcSessionID = session->getRCSessionID();
Tom Josephe6361a22016-08-10 06:56:25 -050045 session->updateLastTransactionTime();
46
Vernon Mauery8d6f2002018-11-07 09:55:53 -080047 return true;
Tom Josephe6361a22016-08-10 06:56:25 -050048}
49
Vernon Mauery8d6f2002018-11-07 09:55:53 -080050Handler::~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
73void 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
87void Handler::executeCommand()
Tom Josephe6361a22016-08-10 06:56:25 -050088{
89 // Get the CommandID to map into the command table
Vernon Mauery7f268e42018-10-26 10:26:01 -070090 auto command = inMessage->getCommand();
Vernon Maueryd999ffc2018-10-25 09:16:05 -070091 if (inMessage->payloadType == PayloadType::IPMI)
Tom Josephe6361a22016-08-10 06:56:25 -050092 {
Richard Marian Thomaiyar8af90eb2019-03-03 15:13:33 +053093 auto session =
94 std::get<session::Manager&>(singletonPool).getSession(sessionID);
95 // Process PayloadType::IPMI only if ipmi is enabled or for sessionless
96 // or for session establisbment command
97 if (this->sessionID == session::SESSION_ZERO ||
98 session->sessionUserPrivAccess.ipmiEnabled)
Tom Josephe6361a22016-08-10 06:56:25 -050099 {
Richard Marian Thomaiyar8af90eb2019-03-03 15:13:33 +0530100 if (inMessage->payload.size() <
101 (sizeof(LAN::header::Request) + sizeof(LAN::trailer::Request)))
102 {
103 return;
104 }
Tom Josephe6361a22016-08-10 06:56:25 -0500105
Richard Marian Thomaiyar8af90eb2019-03-03 15:13:33 +0530106 auto start =
107 inMessage->payload.begin() + sizeof(LAN::header::Request);
108 auto end = inMessage->payload.end() - sizeof(LAN::trailer::Request);
109 std::vector<uint8_t> inPayload(start, end);
110 std::get<command::Table&>(singletonPool)
111 .executeCommand(command, inPayload, shared_from_this());
112 }
113 else
114 {
115 std::vector<uint8_t> payload{IPMI_CC_INSUFFICIENT_PRIVILEGE};
116 outPayload = std::move(payload);
117 }
Tom Josephe6361a22016-08-10 06:56:25 -0500118 }
119 else
120 {
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800121 std::get<command::Table&>(singletonPool)
122 .executeCommand(command, inMessage->payload, shared_from_this());
Tom Josephe6361a22016-08-10 06:56:25 -0500123 }
Tom Josephe6361a22016-08-10 06:56:25 -0500124}
125
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700126void Handler::send(std::shared_ptr<Message> outMessage)
Tom Josephe6361a22016-08-10 06:56:25 -0500127{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700128 auto session =
Vernon Maueryae1fda42018-10-15 12:55:34 -0700129 std::get<session::Manager&>(singletonPool).getSession(sessionID);
Tom Josephe6361a22016-08-10 06:56:25 -0500130
131 // Flatten the packet
Vernon Mauery224f36a2018-10-25 08:52:23 -0700132 auto packet = parser::flatten(outMessage, sessionHeader, session);
Tom Josephe6361a22016-08-10 06:56:25 -0500133
Tom Joseph04b30382017-04-03 01:06:51 +0530134 // Write the packet
Tom Josephe6361a22016-08-10 06:56:25 -0500135 auto writeStatus = channel->write(packet);
136 if (writeStatus < 0)
137 {
Tom Joseph04b30382017-04-03 01:06:51 +0530138 throw std::runtime_error("Error in writing to socket");
Tom Josephe6361a22016-08-10 06:56:25 -0500139 }
Tom Josephe6361a22016-08-10 06:56:25 -0500140}
141
Tom Josephff848492017-03-31 10:43:48 +0530142void Handler::setChannelInSession() const
143{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700144 auto session =
Vernon Maueryae1fda42018-10-15 12:55:34 -0700145 std::get<session::Manager&>(singletonPool).getSession(sessionID);
Tom Josephff848492017-03-31 10:43:48 +0530146
147 session->channelPtr = channel;
148}
149
Vernon Mauery70fd29c2017-11-30 13:11:43 -0800150void Handler::sendSOLPayload(const std::vector<uint8_t>& input)
Tom Joseph22596f22017-03-31 10:52:27 +0530151{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700152 auto session =
Vernon Maueryae1fda42018-10-15 12:55:34 -0700153 std::get<session::Manager&>(singletonPool).getSession(sessionID);
Tom Joseph22596f22017-03-31 10:52:27 +0530154
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700155 auto outMessage = std::make_shared<Message>();
156 outMessage->payloadType = PayloadType::SOL;
157 outMessage->payload = input;
158 outMessage->isPacketEncrypted = session->isCryptAlgoEnabled();
159 outMessage->isPacketAuthenticated = session->isIntegrityAlgoEnabled();
160 outMessage->rcSessionID = session->getRCSessionID();
161 outMessage->bmcSessionID = sessionID;
Tom Joseph22596f22017-03-31 10:52:27 +0530162
163 send(outMessage);
164}
165
Vernon Mauery9e801a22018-10-12 13:20:49 -0700166void Handler::sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd,
Tom Joseph63d3e492017-03-31 11:01:08 +0530167 const std::vector<uint8_t>& output)
168{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700169 auto session =
Vernon Maueryae1fda42018-10-15 12:55:34 -0700170 std::get<session::Manager&>(singletonPool).getSession(sessionID);
Tom Joseph63d3e492017-03-31 11:01:08 +0530171
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700172 auto outMessage = std::make_shared<Message>();
173 outMessage->payloadType = PayloadType::IPMI;
174 outMessage->isPacketEncrypted = session->isCryptAlgoEnabled();
175 outMessage->isPacketAuthenticated = session->isIntegrityAlgoEnabled();
176 outMessage->rcSessionID = session->getRCSessionID();
177 outMessage->bmcSessionID = sessionID;
Tom Joseph63d3e492017-03-31 11:01:08 +0530178
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700179 outMessage->payload.resize(sizeof(LAN::header::Request) + output.size() +
180 sizeof(LAN::trailer::Request));
Tom Joseph63d3e492017-03-31 11:01:08 +0530181
Vernon Mauery9e801a22018-10-12 13:20:49 -0700182 auto respHeader =
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700183 reinterpret_cast<LAN::header::Request*>(outMessage->payload.data());
Tom Joseph63d3e492017-03-31 11:01:08 +0530184
185 // Add IPMI LAN Message Request Header
186 respHeader->rsaddr = LAN::requesterBMCAddress;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700187 respHeader->netfn = (netfn << 0x02);
188 respHeader->cs = crc8bit(&(respHeader->rsaddr), 2);
Tom Joseph63d3e492017-03-31 11:01:08 +0530189 respHeader->rqaddr = LAN::responderBMCAddress;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700190 respHeader->rqseq = 0;
191 respHeader->cmd = cmd;
Tom Joseph63d3e492017-03-31 11:01:08 +0530192
193 auto assembledSize = sizeof(LAN::header::Request);
194
195 // Copy the output by the execution of the command
Vernon Mauery9e801a22018-10-12 13:20:49 -0700196 std::copy(output.begin(), output.end(),
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700197 outMessage->payload.begin() + assembledSize);
Tom Joseph63d3e492017-03-31 11:01:08 +0530198 assembledSize += output.size();
199
200 // Add the IPMI LAN Message Trailer
Vernon Mauery9e801a22018-10-12 13:20:49 -0700201 auto trailer = reinterpret_cast<LAN::trailer::Request*>(
Vernon Maueryd999ffc2018-10-25 09:16:05 -0700202 outMessage->payload.data() + assembledSize);
Tom Joseph63d3e492017-03-31 11:01:08 +0530203
204 // Calculate the checksum for the field rqaddr in the header to the
205 // command data, 3 corresponds to size of the fields before rqaddr( rsaddr,
206 // netfn, cs).
207 trailer->checksum = crc8bit(&respHeader->rqaddr, assembledSize - 3);
208
209 send(outMessage);
210}
211
Vernon Mauery9e801a22018-10-12 13:20:49 -0700212} // namespace message