blob: ab3fe0754b63611fb628bbfa3a894ab35afd63b6 [file] [log] [blame]
Tom Josephf0ca5132016-08-09 08:16:12 -05001#pragma once
2
Vernon Mauery9e801a22018-10-12 13:20:49 -07003#include "auth_algo.hpp"
4#include "crypt_algo.hpp"
5#include "endian.hpp"
6#include "integrity_algo.hpp"
Vernon Mauery8977d122018-10-24 14:02:16 -07007#include "prng.hpp"
Vernon Mauery9e801a22018-10-12 13:20:49 -07008#include "socket_channel.hpp"
9
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053010#include <ipmid/api.hpp>
11#include <ipmid/sessiondef.hpp>
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053012#include <sdbusplus/bus.hpp>
13#include <sdbusplus/server/object.hpp>
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +053014#include <user_channel/channel_layer.hpp>
15#include <user_channel/user_layer.hpp>
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053016#include <xyz/openbmc_project/Ipmi/SessionInfo/server.hpp>
Tom Josephf0ca5132016-08-09 08:16:12 -050017
George Liubc8958f2022-07-04 09:29:49 +080018#include <chrono>
19#include <exception>
20#include <list>
21#include <memory>
22#include <string>
23#include <unordered_map>
24#include <vector>
25
Tom Josephf0ca5132016-08-09 08:16:12 -050026namespace session
27{
28
29using namespace std::chrono_literals;
30using SessionID = uint32_t;
31
32enum class Privilege : uint8_t
33{
34 HIGHEST_MATCHING,
35 CALLBACK,
36 USER,
37 OPERATOR,
38 ADMIN,
39 OEM,
40};
41
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053042// Mask to get only the privilege from requested maximum privlege (RAKP message
43// 1)
44constexpr uint8_t reqMaxPrivMask = 0xF;
45
Tom Joseph3563f8f2017-05-08 15:42:54 +053046/**
Tom Josephf0ca5132016-08-09 08:16:12 -050047 * @struct SequenceNumbers Session Sequence Numbers
48 *
49 * IPMI v2.0 RMCP+ Session Sequence Numbers are used for rejecting packets that
50 * may have been duplicated by the network or intentionally replayed. There are
51 * two sets of Session SequenceNumbers for a given session.One set of inbound
52 * and outbound sequence numbers is used for authenticated (signed) packets,
53 * and the other set is used for unauthenticated packets.
54 *
55 * The individual Session Sequence Numbers is are initialized to zero whenever
56 * a session is created and incremented by one at the start of outbound
57 * processing for a given packet (i.e. the first transmitted packet has a ‘1’
58 * as the sequence number, not 0). Session Sequence numbers are incremented for
59 * every packet that is transmitted by a given sender, regardless of whether
60 * the payload for the packet is a ‘retry’ or not.
61 */
62struct SequenceNumbers
63{
Vernon Mauery9e801a22018-10-12 13:20:49 -070064 auto get(bool inbound = true) const
65 {
66 return inbound ? in : out;
67 }
Tom Josephf0ca5132016-08-09 08:16:12 -050068
Vernon Mauery9e801a22018-10-12 13:20:49 -070069 void set(uint32_t seqNumber, bool inbound = true)
70 {
71 inbound ? (in = seqNumber) : (out = seqNumber);
72 }
Tom Josephf0ca5132016-08-09 08:16:12 -050073
Vernon Mauery9e801a22018-10-12 13:20:49 -070074 auto increment()
75 {
76 return ++out;
77 }
Tom Josephf0ca5132016-08-09 08:16:12 -050078
Vernon Mauery9e801a22018-10-12 13:20:49 -070079 private:
80 uint32_t in = 0;
81 uint32_t out = 0;
Tom Josephf0ca5132016-08-09 08:16:12 -050082};
Tom Joseph3563f8f2017-05-08 15:42:54 +053083/**
Tom Josephf0ca5132016-08-09 08:16:12 -050084 * @class Session
85 *
86 * Encapsulates the data related to an IPMI Session
87 *
88 * Authenticated IPMI communication to the BMC is accomplished by establishing
89 * a session. Once established, a session is identified by a Session ID. The
90 * Session ID may be thought of as a handle that identifies a connection between
91 * a given remote user and the BMC. The specification supports having multiple
92 * active sessions established with the BMC. It is recommended that a BMC
93 * implementation support at least four simultaneous sessions
94 */
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053095
Patrick Williams0a590622022-07-22 19:26:53 -050096using SessionIface = sdbusplus::server::object_t<
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053097 sdbusplus::xyz::openbmc_project::Ipmi::server::SessionInfo>;
98
99class Session : public SessionIface
Tom Josephf0ca5132016-08-09 08:16:12 -0500100{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700101 public:
102 Session() = default;
103 ~Session() = default;
104 Session(const Session&) = delete;
105 Session& operator=(const Session&) = delete;
106 Session(Session&&) = default;
107 Session& operator=(Session&&) = default;
Tom Josephf0ca5132016-08-09 08:16:12 -0500108
Vernon Mauery9e801a22018-10-12 13:20:49 -0700109 /**
110 * @brief Session Constructor
111 *
112 * This is issued by the Session Manager when a session is started for
113 * the Open SessionRequest command
114 *
115 * @param[in] inRemoteConsoleSessID - Remote Console Session ID
116 * @param[in] priv - Privilege Level requested in the Command
117 */
Patrick Williams0a590622022-07-22 19:26:53 -0500118 Session(sdbusplus::bus_t& bus, const char* path,
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530119 SessionID inRemoteConsoleSessID, SessionID BMCSessionID,
Patrick Williams84256242024-08-16 15:20:21 -0400120 char priv) : SessionIface(bus, path)
Vernon Mauery9e801a22018-10-12 13:20:49 -0700121 {
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530122 reqMaxPrivLevel = static_cast<session::Privilege>(priv);
123 bmcSessionID = BMCSessionID;
124 remoteConsoleSessionID = inRemoteConsoleSessID;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700125 }
Tom Josephf0ca5132016-08-09 08:16:12 -0500126
Vernon Mauery9e801a22018-10-12 13:20:49 -0700127 auto getBMCSessionID() const
128 {
129 return bmcSessionID;
130 }
Tom Josephf0ca5132016-08-09 08:16:12 -0500131
Vernon Mauery9e801a22018-10-12 13:20:49 -0700132 auto getRCSessionID() const
133 {
134 return remoteConsoleSessionID;
135 }
136
137 auto getAuthAlgo() const
138 {
139 if (authAlgoInterface)
Tom Josephf0ca5132016-08-09 08:16:12 -0500140 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700141 return authAlgoInterface.get();
Tom Josephf0ca5132016-08-09 08:16:12 -0500142 }
Vernon Mauery9e801a22018-10-12 13:20:49 -0700143 else
Tom Josephf0ca5132016-08-09 08:16:12 -0500144 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700145 throw std::runtime_error("Authentication Algorithm Empty");
Tom Josephf0ca5132016-08-09 08:16:12 -0500146 }
Vernon Mauery9e801a22018-10-12 13:20:49 -0700147 }
Tom Josephf0ca5132016-08-09 08:16:12 -0500148
Vernon Mauery9e801a22018-10-12 13:20:49 -0700149 void setAuthAlgo(std::unique_ptr<cipher::rakp_auth::Interface>&& inAuthAlgo)
150 {
151 authAlgoInterface = std::move(inAuthAlgo);
152 }
153
154 /**
155 * @brief Get Session's Integrity Algorithm
156 *
157 * @return pointer to the integrity algorithm
158 */
159 auto getIntegrityAlgo() const
160 {
161 if (integrityAlgoInterface)
Tom Josephf0ca5132016-08-09 08:16:12 -0500162 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700163 return integrityAlgoInterface.get();
Tom Josephf0ca5132016-08-09 08:16:12 -0500164 }
Vernon Mauery9e801a22018-10-12 13:20:49 -0700165 else
Tom Josephf0ca5132016-08-09 08:16:12 -0500166 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700167 throw std::runtime_error("Integrity Algorithm Empty");
Tom Josephf0ca5132016-08-09 08:16:12 -0500168 }
Vernon Mauery9e801a22018-10-12 13:20:49 -0700169 }
Tom Josephf0ca5132016-08-09 08:16:12 -0500170
Vernon Mauery9e801a22018-10-12 13:20:49 -0700171 /**
172 * @brief Set Session's Integrity Algorithm
173 *
174 * @param[in] integrityAlgo - unique pointer to integrity algorithm
175 * instance
176 */
177 void setIntegrityAlgo(
178 std::unique_ptr<cipher::integrity::Interface>&& integrityAlgo)
179 {
180 integrityAlgoInterface = std::move(integrityAlgo);
181 }
182
183 /** @brief Check if integrity algorithm is enabled for this session.
184 *
185 * @return true if integrity algorithm is enabled else false.
186 */
187 auto isIntegrityAlgoEnabled()
188 {
189 return integrityAlgoInterface ? true : false;
190 }
191
192 /**
193 * @brief Get Session's Confidentiality Algorithm
194 *
195 * @return pointer to the confidentiality algorithm
196 */
197 auto getCryptAlgo() const
198 {
199 if (cryptAlgoInterface)
Tom Joseph638d0662017-01-10 16:02:07 +0530200 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700201 return cryptAlgoInterface.get();
Tom Joseph638d0662017-01-10 16:02:07 +0530202 }
Vernon Mauery9e801a22018-10-12 13:20:49 -0700203 else
Tom Joseph638d0662017-01-10 16:02:07 +0530204 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700205 throw std::runtime_error("Confidentiality Algorithm Empty");
Tom Joseph638d0662017-01-10 16:02:07 +0530206 }
Vernon Mauery9e801a22018-10-12 13:20:49 -0700207 }
Tom Joseph638d0662017-01-10 16:02:07 +0530208
Vernon Mauery9e801a22018-10-12 13:20:49 -0700209 /**
210 * @brief Set Session's Confidentiality Algorithm
211 *
212 * @param[in] confAlgo - unique pointer to confidentiality algorithm
213 * instance
214 */
215 void setCryptAlgo(std::unique_ptr<cipher::crypt::Interface>&& cryptAlgo)
216 {
217 cryptAlgoInterface = std::move(cryptAlgo);
218 }
Tom Josephd8c78612017-03-31 10:17:30 +0530219
Vernon Mauery9e801a22018-10-12 13:20:49 -0700220 /** @brief Check if confidentiality algorithm is enabled for this
221 * session.
222 *
223 * @return true if confidentiality algorithm is enabled else false.
224 */
225 auto isCryptAlgoEnabled()
226 {
227 return cryptAlgoInterface ? true : false;
228 }
Tom Joseph491dbd02017-01-24 18:20:41 +0530229
Vernon Mauery9e801a22018-10-12 13:20:49 -0700230 void updateLastTransactionTime()
231 {
232 lastTime = std::chrono::steady_clock::now();
233 }
Tom Joseph491dbd02017-01-24 18:20:41 +0530234
Vernon Mauery9e801a22018-10-12 13:20:49 -0700235 /**
236 * @brief Session Active Status
237 *
238 * Session Active status is decided upon the Session State and the last
239 * transaction time is compared against the session inactivity timeout.
240 *
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700241 * @param[in] activeGrace - microseconds of idle time for active sessions
242 * @param[in] setupGrace - microseconds of idle time for sessions in setup
243 *
Vernon Mauery9e801a22018-10-12 13:20:49 -0700244 */
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700245 bool isSessionActive(const std::chrono::microseconds& activeGrace,
246 const std::chrono::microseconds& setupGrace)
Vernon Mauery07e5b282018-10-24 13:11:23 -0700247 {
248 auto currentTime = std::chrono::steady_clock::now();
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700249 auto elapsedMicros =
Patrick Williams84256242024-08-16 15:20:21 -0400250 std::chrono::duration_cast<std::chrono::microseconds>(
251 currentTime - lastTime);
Vernon Mauery07e5b282018-10-24 13:11:23 -0700252
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700253 State state = static_cast<session::State>(this->state());
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530254
Vernon Mauery07e5b282018-10-24 13:11:23 -0700255 switch (state)
256 {
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700257 case State::active:
258 if (elapsedMicros < activeGrace)
Vernon Mauery07e5b282018-10-24 13:11:23 -0700259 {
260 return true;
261 }
262 break;
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700263 case State::setupInProgress:
264 if (elapsedMicros < setupGrace)
Vernon Mauery07e5b282018-10-24 13:11:23 -0700265 {
266 return true;
267 }
268 break;
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700269 case State::tearDownInProgress:
270 break;
Vernon Mauery07e5b282018-10-24 13:11:23 -0700271 default:
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700272 break;
Vernon Mauery07e5b282018-10-24 13:11:23 -0700273 }
274 return false;
275 }
Tom Joseph895df942017-03-31 10:19:40 +0530276
Vernon Mauery9e801a22018-10-12 13:20:49 -0700277 /**
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +0530278 * @brief Session's Requested Maximum Privilege Level
Vernon Mauery9e801a22018-10-12 13:20:49 -0700279 */
Tom Joseph4021b1f2019-02-12 10:10:12 +0530280 Privilege reqMaxPrivLevel;
Tom Josephf0ca5132016-08-09 08:16:12 -0500281
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +0530282 /**
283 * @brief session's user & channel access details
284 */
285 ipmi::PrivAccess sessionUserPrivAccess{};
286 ipmi::ChannelAccess sessionChannelAccess{};
287
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530288 SequenceNumbers sequenceNums; // Session Sequence Numbers
289 std::string userName{}; // User Name
Tom Josephf0ca5132016-08-09 08:16:12 -0500290
Vernon Mauery9e801a22018-10-12 13:20:49 -0700291 /** @brief Socket channel for communicating with the remote client.*/
292 std::shared_ptr<udpsocket::Channel> channelPtr;
Tom Josephf0ca5132016-08-09 08:16:12 -0500293
Vernon Mauery9e801a22018-10-12 13:20:49 -0700294 private:
295 SessionID bmcSessionID = 0; // BMC Session ID
296 SessionID remoteConsoleSessionID = 0; // Remote Console Session ID
Tom Josephf0ca5132016-08-09 08:16:12 -0500297
Vernon Mauery9e801a22018-10-12 13:20:49 -0700298 // Authentication Algorithm Interface for the Session
299 std::unique_ptr<cipher::rakp_auth::Interface> authAlgoInterface;
Tom Josephcc27e122017-03-31 10:21:04 +0530300
Vernon Mauery9e801a22018-10-12 13:20:49 -0700301 // Integrity Algorithm Interface for the Session
302 std::unique_ptr<cipher::integrity::Interface> integrityAlgoInterface =
303 nullptr;
Tom Josephf0ca5132016-08-09 08:16:12 -0500304
Vernon Mauery9e801a22018-10-12 13:20:49 -0700305 // Confidentiality Algorithm Interface for the Session
306 std::unique_ptr<cipher::crypt::Interface> cryptAlgoInterface = nullptr;
Tom Josephf0ca5132016-08-09 08:16:12 -0500307
Vernon Mauery9e801a22018-10-12 13:20:49 -0700308 // Last Transaction Time
309 decltype(std::chrono::steady_clock::now()) lastTime;
Tom Josephf0ca5132016-08-09 08:16:12 -0500310};
311
312} // namespace session