Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 1 | #include "payload_cmds.hpp" |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 2 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 3 | #include "sessions_manager.hpp" |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 4 | #include "sol/sol_manager.hpp" |
| 5 | #include "sol_cmds.hpp" |
| 6 | |
William A. Kennington III | 4f09eae | 2019-02-12 17:10:35 -0800 | [diff] [blame] | 7 | #include <ipmid/api.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | |
Ayushi Smriti | b12d875 | 2019-09-18 18:37:09 +0530 | [diff] [blame] | 9 | #include <ipmid/api-types.hpp> |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 11 | |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 12 | namespace sol |
| 13 | { |
| 14 | |
| 15 | namespace command |
| 16 | { |
| 17 | |
Tom Joseph | 18a45e9 | 2017-04-11 11:30:44 +0530 | [diff] [blame] | 18 | std::vector<uint8_t> activatePayload(const std::vector<uint8_t>& inPayload, |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 19 | std::shared_ptr<message::Handler>& handler) |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 20 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 21 | auto request = |
| 22 | reinterpret_cast<const ActivatePayloadRequest*>(inPayload.data()); |
Zhikui Ren | 2b1edef | 2020-07-24 14:32:13 -0700 | [diff] [blame] | 23 | if (inPayload.size() != sizeof(*request)) |
| 24 | { |
| 25 | std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID}; |
| 26 | return errorPayload; |
| 27 | } |
| 28 | |
| 29 | std::vector<uint8_t> outPayload(sizeof(ActivatePayloadResponse)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 30 | auto response = |
| 31 | reinterpret_cast<ActivatePayloadResponse*>(outPayload.data()); |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 32 | |
| 33 | response->completionCode = IPMI_CC_OK; |
| 34 | |
| 35 | // SOL is the payload currently supported for activation. |
| 36 | if (static_cast<uint8_t>(message::PayloadType::SOL) != request->payloadType) |
| 37 | { |
| 38 | response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST; |
| 39 | return outPayload; |
| 40 | } |
| 41 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 42 | sol::Manager::get().updateSOLParameter(ipmi::convertCurrentChannelNum( |
| 43 | ipmi::currentChNum, getInterfaceIndex())); |
| 44 | if (!sol::Manager::get().enable) |
Tom Joseph | 96b6d81 | 2017-04-28 01:27:17 +0530 | [diff] [blame] | 45 | { |
| 46 | response->completionCode = IPMI_CC_PAYLOAD_TYPE_DISABLED; |
| 47 | return outPayload; |
| 48 | } |
| 49 | |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 50 | // Only one instance of SOL is currently supported. |
| 51 | if (request->payloadInstance != 1) |
| 52 | { |
| 53 | response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST; |
| 54 | return outPayload; |
| 55 | } |
| 56 | |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 57 | auto session = session::Manager::get().getSession(handler->sessionID); |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 58 | |
| 59 | if (!request->encryption && session->isCryptAlgoEnabled()) |
| 60 | { |
| 61 | response->completionCode = IPMI_CC_PAYLOAD_WITHOUT_ENCRYPTION; |
| 62 | return outPayload; |
| 63 | } |
| 64 | |
Tang Yiwei | de4121c | 2022-05-17 18:21:03 +0800 | [diff] [blame] | 65 | if (session->currentPrivilege() < |
| 66 | static_cast<uint8_t>(sol::Manager::get().solMinPrivilege)) |
| 67 | { |
| 68 | response->completionCode = IPMI_CC_INSUFFICIENT_PRIVILEGE; |
| 69 | return outPayload; |
| 70 | } |
| 71 | |
Saravanan Palanisamy | 0a26904 | 2019-07-05 02:21:21 +0000 | [diff] [blame] | 72 | // Is SOL Payload enabled for this user & channel. |
| 73 | auto userId = ipmi::ipmiUserGetUserId(session->userName); |
| 74 | ipmi::PayloadAccess payloadAccess = {}; |
| 75 | if ((ipmi::ipmiUserGetUserPayloadAccess(session->channelNum(), userId, |
| 76 | payloadAccess) != IPMI_CC_OK) || |
| 77 | !(payloadAccess.stdPayloadEnables1[static_cast<uint8_t>( |
| 78 | message::PayloadType::SOL)])) |
| 79 | { |
| 80 | response->completionCode = IPMI_CC_PAYLOAD_TYPE_DISABLED; |
| 81 | return outPayload; |
| 82 | } |
| 83 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 84 | auto status = sol::Manager::get().isPayloadActive(request->payloadInstance); |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 85 | if (status) |
| 86 | { |
| 87 | response->completionCode = IPMI_CC_PAYLOAD_ALREADY_ACTIVE; |
| 88 | return outPayload; |
| 89 | } |
| 90 | |
| 91 | // Set the current command's socket channel to the session |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 92 | handler->setChannelInSession(); |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 93 | |
| 94 | // Start the SOL payload |
| 95 | try |
| 96 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 97 | sol::Manager::get().startPayloadInstance(request->payloadInstance, |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 98 | handler->sessionID); |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 99 | } |
Patrick Williams | 12d199b | 2021-10-06 12:36:48 -0500 | [diff] [blame] | 100 | catch (const std::exception& e) |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 101 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 102 | lg2::error("Failed to start SOL payload: {ERROR}", "ERROR", e); |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 103 | response->completionCode = IPMI_CC_UNSPECIFIED_ERROR; |
| 104 | return outPayload; |
| 105 | } |
| 106 | |
| 107 | response->inPayloadSize = endian::to_ipmi<uint16_t>(MAX_PAYLOAD_SIZE); |
| 108 | response->outPayloadSize = endian::to_ipmi<uint16_t>(MAX_PAYLOAD_SIZE); |
| 109 | response->portNum = endian::to_ipmi<uint16_t>(IPMI_STD_PORT); |
| 110 | |
| 111 | // VLAN addressing is not used |
| 112 | response->vlanNum = 0xFFFF; |
| 113 | |
| 114 | return outPayload; |
| 115 | } |
| 116 | |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 117 | std::vector<uint8_t> |
| 118 | deactivatePayload(const std::vector<uint8_t>& inPayload, |
Tang Yiwei | de4121c | 2022-05-17 18:21:03 +0800 | [diff] [blame] | 119 | std::shared_ptr<message::Handler>& handler) |
Tom Joseph | e2f0a8e | 2017-04-03 02:01:49 +0530 | [diff] [blame] | 120 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 121 | auto request = |
| 122 | reinterpret_cast<const DeactivatePayloadRequest*>(inPayload.data()); |
Zhikui Ren | 2b1edef | 2020-07-24 14:32:13 -0700 | [diff] [blame] | 123 | if (inPayload.size() != sizeof(*request)) |
| 124 | { |
| 125 | std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID}; |
| 126 | return errorPayload; |
| 127 | } |
| 128 | |
| 129 | std::vector<uint8_t> outPayload(sizeof(DeactivatePayloadResponse)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 130 | auto response = |
| 131 | reinterpret_cast<DeactivatePayloadResponse*>(outPayload.data()); |
Tom Joseph | e2f0a8e | 2017-04-03 02:01:49 +0530 | [diff] [blame] | 132 | response->completionCode = IPMI_CC_OK; |
| 133 | |
| 134 | // SOL is the payload currently supported for deactivation |
| 135 | if (static_cast<uint8_t>(message::PayloadType::SOL) != request->payloadType) |
| 136 | { |
| 137 | response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST; |
| 138 | return outPayload; |
| 139 | } |
| 140 | |
| 141 | // Only one instance of SOL is supported |
| 142 | if (request->payloadInstance != 1) |
| 143 | { |
| 144 | response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST; |
| 145 | return outPayload; |
| 146 | } |
| 147 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 148 | auto status = sol::Manager::get().isPayloadActive(request->payloadInstance); |
Tom Joseph | e2f0a8e | 2017-04-03 02:01:49 +0530 | [diff] [blame] | 149 | if (!status) |
| 150 | { |
| 151 | response->completionCode = IPMI_CC_PAYLOAD_DEACTIVATED; |
| 152 | return outPayload; |
| 153 | } |
| 154 | |
Tang Yiwei | de4121c | 2022-05-17 18:21:03 +0800 | [diff] [blame] | 155 | auto currentSession = |
| 156 | session::Manager::get().getSession(handler->sessionID); |
| 157 | auto solSessionID = |
| 158 | sol::Manager::get().getContext(request->payloadInstance).sessionID; |
Jayaprakash Mutyala | 34a2c41 | 2023-09-08 10:30:28 +0000 | [diff] [blame] | 159 | auto solActiveSession = |
| 160 | sol::Manager::get().getContext(request->payloadInstance).session; |
Tang Yiwei | de4121c | 2022-05-17 18:21:03 +0800 | [diff] [blame] | 161 | // The session owner or the ADMIN could deactivate the session |
| 162 | if (currentSession->userName != solActiveSession->userName && |
| 163 | currentSession->currentPrivilege() != |
| 164 | static_cast<uint8_t>(session::Privilege::ADMIN)) |
| 165 | { |
| 166 | response->completionCode = IPMI_CC_INSUFFICIENT_PRIVILEGE; |
| 167 | return outPayload; |
| 168 | } |
| 169 | |
Tom Joseph | e2f0a8e | 2017-04-03 02:01:49 +0530 | [diff] [blame] | 170 | try |
| 171 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 172 | sol::Manager::get().stopPayloadInstance(request->payloadInstance); |
Tom Joseph | e2f0a8e | 2017-04-03 02:01:49 +0530 | [diff] [blame] | 173 | |
Tom Joseph | 6516cef | 2017-07-31 18:48:34 +0530 | [diff] [blame] | 174 | try |
| 175 | { |
Tang Yiwei | de4121c | 2022-05-17 18:21:03 +0800 | [diff] [blame] | 176 | activating(request->payloadInstance, solSessionID); |
Tom Joseph | 6516cef | 2017-07-31 18:48:34 +0530 | [diff] [blame] | 177 | } |
Patrick Williams | 12d199b | 2021-10-06 12:36:48 -0500 | [diff] [blame] | 178 | catch (const std::exception& e) |
Tom Joseph | 6516cef | 2017-07-31 18:48:34 +0530 | [diff] [blame] | 179 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 180 | lg2::info("Failed to call the activating method: {ERROR}", "ERROR", |
| 181 | e); |
Tom Joseph | 6516cef | 2017-07-31 18:48:34 +0530 | [diff] [blame] | 182 | /* |
| 183 | * In case session has been closed (like in the case of inactivity |
| 184 | * timeout), then activating function would throw an exception, |
Jayaprakash Mutyala | 34a2c41 | 2023-09-08 10:30:28 +0000 | [diff] [blame] | 185 | * since solSessionID is not found. As session is already closed, |
| 186 | * returning IPMI status code for Payload already deactivated |
| 187 | * as BMC automatically deactivates all active payloads when |
| 188 | * session is terminated. |
Tom Joseph | 6516cef | 2017-07-31 18:48:34 +0530 | [diff] [blame] | 189 | */ |
Jayaprakash Mutyala | 34a2c41 | 2023-09-08 10:30:28 +0000 | [diff] [blame] | 190 | response->completionCode = IPMI_CC_PAYLOAD_DEACTIVATED; |
Tom Joseph | 6516cef | 2017-07-31 18:48:34 +0530 | [diff] [blame] | 191 | return outPayload; |
| 192 | } |
Tom Joseph | e2f0a8e | 2017-04-03 02:01:49 +0530 | [diff] [blame] | 193 | } |
Patrick Williams | 12d199b | 2021-10-06 12:36:48 -0500 | [diff] [blame] | 194 | catch (const std::exception& e) |
Tom Joseph | e2f0a8e | 2017-04-03 02:01:49 +0530 | [diff] [blame] | 195 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 196 | lg2::error("Failed to call the getContext method: {ERROR}", "ERROR", e); |
Tom Joseph | e2f0a8e | 2017-04-03 02:01:49 +0530 | [diff] [blame] | 197 | response->completionCode = IPMI_CC_UNSPECIFIED_ERROR; |
| 198 | return outPayload; |
| 199 | } |
| 200 | |
| 201 | return outPayload; |
| 202 | } |
| 203 | |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 204 | std::vector<uint8_t> |
| 205 | getPayloadStatus(const std::vector<uint8_t>& inPayload, |
George Liu | be1470c | 2022-07-04 14:33:24 +0800 | [diff] [blame] | 206 | std::shared_ptr<message::Handler>& /* handler */) |
Tom Joseph | e1ae56c | 2017-04-03 02:03:41 +0530 | [diff] [blame] | 207 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 208 | auto request = |
| 209 | reinterpret_cast<const GetPayloadStatusRequest*>(inPayload.data()); |
Zhikui Ren | 2b1edef | 2020-07-24 14:32:13 -0700 | [diff] [blame] | 210 | if (inPayload.size() != sizeof(*request)) |
| 211 | { |
| 212 | std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID}; |
| 213 | return errorPayload; |
| 214 | } |
| 215 | |
| 216 | std::vector<uint8_t> outPayload(sizeof(GetPayloadStatusResponse)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 217 | auto response = |
| 218 | reinterpret_cast<GetPayloadStatusResponse*>(outPayload.data()); |
Tom Joseph | e1ae56c | 2017-04-03 02:03:41 +0530 | [diff] [blame] | 219 | |
| 220 | // SOL is the payload currently supported for payload status |
| 221 | if (static_cast<uint8_t>(message::PayloadType::SOL) != request->payloadType) |
| 222 | { |
| 223 | response->completionCode = IPMI_CC_UNSPECIFIED_ERROR; |
| 224 | return outPayload; |
| 225 | } |
| 226 | |
| 227 | response->completionCode = IPMI_CC_OK; |
Ayushi Smriti | ddba9d1 | 2019-09-13 12:03:31 +0530 | [diff] [blame] | 228 | |
| 229 | constexpr size_t maxSolPayloadInstances = 1; |
| 230 | response->capacity = maxSolPayloadInstances; |
Tom Joseph | e1ae56c | 2017-04-03 02:03:41 +0530 | [diff] [blame] | 231 | |
| 232 | // Currently we support only one SOL session |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 233 | response->instance1 = sol::Manager::get().isPayloadActive(1); |
Tom Joseph | e1ae56c | 2017-04-03 02:03:41 +0530 | [diff] [blame] | 234 | |
| 235 | return outPayload; |
| 236 | } |
| 237 | |
George Liu | be1470c | 2022-07-04 14:33:24 +0800 | [diff] [blame] | 238 | std::vector<uint8_t> |
| 239 | getPayloadInfo(const std::vector<uint8_t>& inPayload, |
| 240 | std::shared_ptr<message::Handler>& /* handler */) |
Tom Joseph | 8093849 | 2018-03-22 10:05:20 +0530 | [diff] [blame] | 241 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 242 | auto request = |
| 243 | reinterpret_cast<const GetPayloadInfoRequest*>(inPayload.data()); |
Zhikui Ren | 2b1edef | 2020-07-24 14:32:13 -0700 | [diff] [blame] | 244 | |
| 245 | if (inPayload.size() != sizeof(*request)) |
| 246 | { |
| 247 | std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID}; |
| 248 | return errorPayload; |
| 249 | } |
| 250 | |
| 251 | std::vector<uint8_t> outPayload(sizeof(GetPayloadInfoResponse)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 252 | auto response = |
| 253 | reinterpret_cast<GetPayloadInfoResponse*>(outPayload.data()); |
Tom Joseph | 8093849 | 2018-03-22 10:05:20 +0530 | [diff] [blame] | 254 | |
| 255 | // SOL is the payload currently supported for payload status & only one |
| 256 | // instance of SOL is supported. |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 257 | if (static_cast<uint8_t>(message::PayloadType::SOL) != |
| 258 | request->payloadType || |
| 259 | request->payloadInstance != 1) |
Tom Joseph | 8093849 | 2018-03-22 10:05:20 +0530 | [diff] [blame] | 260 | { |
| 261 | response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST; |
| 262 | return outPayload; |
| 263 | } |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 264 | auto status = sol::Manager::get().isPayloadActive(request->payloadInstance); |
Tom Joseph | 8093849 | 2018-03-22 10:05:20 +0530 | [diff] [blame] | 265 | |
Richard Marian Thomaiyar | 5f1dd31 | 2019-01-22 15:55:41 +0530 | [diff] [blame] | 266 | if (status) |
Tom Joseph | 8093849 | 2018-03-22 10:05:20 +0530 | [diff] [blame] | 267 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 268 | auto& context = |
| 269 | sol::Manager::get().getContext(request->payloadInstance); |
Richard Marian Thomaiyar | 5f1dd31 | 2019-01-22 15:55:41 +0530 | [diff] [blame] | 270 | response->sessionID = context.sessionID; |
Tom Joseph | 8093849 | 2018-03-22 10:05:20 +0530 | [diff] [blame] | 271 | } |
Richard Marian Thomaiyar | 5f1dd31 | 2019-01-22 15:55:41 +0530 | [diff] [blame] | 272 | else |
| 273 | { |
| 274 | // No active payload - return session id as 0 |
| 275 | response->sessionID = 0; |
| 276 | } |
| 277 | response->completionCode = IPMI_CC_OK; |
Tom Joseph | 8093849 | 2018-03-22 10:05:20 +0530 | [diff] [blame] | 278 | return outPayload; |
| 279 | } |
| 280 | |
Tom Joseph | 5c846a8 | 2017-04-03 01:59:39 +0530 | [diff] [blame] | 281 | } // namespace command |
| 282 | |
| 283 | } // namespace sol |