Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 3 | #include "session.hpp" |
| 4 | |
Vernon Mauery | ecc8efa | 2021-06-12 12:52:23 -0700 | [diff] [blame^] | 5 | #include <boost/asio/steady_timer.hpp> |
| 6 | #include <chrono> |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 7 | #include <ipmid/api.hpp> |
| 8 | #include <ipmid/sessiondef.hpp> |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <memory> |
| 11 | #include <mutex> |
Andrew Geissler | 7408e76 | 2020-05-17 08:56:05 -0500 | [diff] [blame] | 12 | #include <string> |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 13 | |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 14 | namespace session |
| 15 | { |
| 16 | |
| 17 | enum class RetrieveOption |
| 18 | { |
| 19 | BMC_SESSION_ID, |
| 20 | RC_SESSION_ID, |
| 21 | }; |
| 22 | |
Vernon Mauery | ecc8efa | 2021-06-12 12:52:23 -0700 | [diff] [blame^] | 23 | static constexpr size_t maxSessionHandles = multiIntfaceSessionHandleMask; |
| 24 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 25 | /** |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 26 | * @class Manager |
| 27 | * |
| 28 | * Manager class acts a manager for the IPMI sessions and provides interfaces |
| 29 | * to start a session, stop a session and get reference to the session objects. |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | class Manager |
| 34 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 35 | public: |
| 36 | // BMC Session ID is the key for the map |
| 37 | using SessionMap = std::map<SessionID, std::shared_ptr<Session>>; |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 38 | |
Vernon Mauery | ecc8efa | 2021-06-12 12:52:23 -0700 | [diff] [blame^] | 39 | Manager() = delete; |
| 40 | explicit Manager(std::shared_ptr<boost::asio::io_context>& io) : |
| 41 | io(io), timer(*io){}; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 42 | ~Manager() = default; |
| 43 | Manager(const Manager&) = delete; |
| 44 | Manager& operator=(const Manager&) = delete; |
| 45 | Manager(Manager&&) = default; |
| 46 | Manager& operator=(Manager&&) = default; |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 47 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 48 | /** |
| 49 | * @brief Start an IPMI session |
| 50 | * |
| 51 | * @param[in] remoteConsoleSessID - Remote Console Session ID mentioned |
| 52 | * in the Open SessionRequest Command |
| 53 | * @param[in] priv - Privilege level requested |
| 54 | * @param[in] authAlgo - Authentication Algorithm |
| 55 | * @param[in] intAlgo - Integrity Algorithm |
| 56 | * @param[in] cryptAlgo - Confidentiality Algorithm |
| 57 | * |
| 58 | * @return session handle on success and nullptr on failure |
| 59 | * |
| 60 | */ |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 61 | std::shared_ptr<Session> |
| 62 | startSession(SessionID remoteConsoleSessID, Privilege priv, |
| 63 | cipher::rakp_auth::Algorithms authAlgo, |
| 64 | cipher::integrity::Algorithms intAlgo, |
| 65 | cipher::crypt::Algorithms cryptAlgo); |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 66 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 67 | /** |
| 68 | * @brief Stop IPMI Session |
| 69 | * |
| 70 | * @param[in] bmcSessionID - BMC Session ID |
| 71 | * |
| 72 | * @return true on success and failure if session ID is invalid |
| 73 | * |
| 74 | */ |
| 75 | bool stopSession(SessionID bmcSessionID); |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 76 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 77 | /** |
| 78 | * @brief Get Session Handle |
| 79 | * |
| 80 | * @param[in] sessionID - Session ID |
| 81 | * @param[in] option - Select between BMC Session ID and Remote Console |
| 82 | * Session ID, Default option is BMC Session ID |
| 83 | * |
| 84 | * @return session handle on success and nullptr on failure |
| 85 | * |
| 86 | */ |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 87 | std::shared_ptr<Session> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 88 | getSession(SessionID sessionID, |
| 89 | RetrieveOption option = RetrieveOption::BMC_SESSION_ID); |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 90 | uint8_t getActiveSessionCount() const; |
| 91 | uint8_t getSessionHandle(SessionID bmcSessionID) const; |
| 92 | uint8_t storeSessionHandle(SessionID bmcSessionID); |
| 93 | uint32_t getSessionIDbyHandle(uint8_t sessionHandle) const; |
| 94 | |
| 95 | void managerInit(const std::string& channel); |
| 96 | |
| 97 | uint8_t getNetworkInstance(void); |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 98 | |
Vernon Mauery | ecc8efa | 2021-06-12 12:52:23 -0700 | [diff] [blame^] | 99 | /** |
| 100 | * @brief Clean Session Stale Entries |
| 101 | * |
| 102 | * Schedules cleaning the inactive sessions entries from the Session Map |
| 103 | */ |
| 104 | void scheduleSessionCleaner(const std::chrono::microseconds& grace); |
| 105 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 106 | private: |
Vernon Mauery | ecc8efa | 2021-06-12 12:52:23 -0700 | [diff] [blame^] | 107 | /** |
| 108 | * @brief reclaim system resources by limiting idle sessions |
| 109 | * |
| 110 | * Limits on active, authenticated sessions are calculated independently |
| 111 | * from in-setup sessions, which are not required to be authenticated. This |
| 112 | * will prevent would-be DoS attacks by calling a bunch of Open Session |
| 113 | * requests to fill up all available sessions. Too many active sessions will |
| 114 | * trigger a shorter timeout, but is unaffected by setup session counts. |
| 115 | * |
| 116 | * For active sessions, grace time is inversely proportional to (the number |
| 117 | * of active sessions beyond max sessions per channel)^3 |
| 118 | * |
| 119 | * For sessions in setup, grace time is inversely proportional to (the |
| 120 | * number of total sessions beyond max sessions per channel)^3, with a max |
| 121 | * of 3 seconds |
| 122 | */ |
| 123 | void cleanStaleEntries(); |
| 124 | |
| 125 | std::shared_ptr<boost::asio::io_context> io; |
| 126 | boost::asio::steady_timer timer; |
| 127 | |
| 128 | std::array<uint32_t, session::maxSessionHandles> sessionHandleMap = {0}; |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 129 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 130 | /** |
| 131 | * @brief Session Manager keeps the session objects as a sorted |
| 132 | * associative container with Session ID as the unique key |
| 133 | */ |
| 134 | SessionMap sessionsMap; |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 135 | std::unique_ptr<sdbusplus::server::manager::manager> objManager = nullptr; |
| 136 | std::string chName{}; // Channel Name |
| 137 | uint8_t ipmiNetworkInstance; |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 138 | void setNetworkInstance(void); |
Tom Joseph | 3e61aa0 | 2016-08-08 08:42:39 -0500 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // namespace session |