Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 Intel Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "config.h" |
| 17 | |
| 18 | #include "settings.hpp" |
| 19 | |
| 20 | #include <dlfcn.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <any> |
Vernon Mauery | e8d4323 | 2019-03-26 16:23:43 -0700 | [diff] [blame] | 24 | #include <dcmihandler.hpp> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 25 | #include <exception> |
Vernon Mauery | bdda800 | 2019-02-26 10:18:51 -0800 | [diff] [blame] | 26 | #include <filesystem> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 27 | #include <forward_list> |
| 28 | #include <host-cmd-manager.hpp> |
| 29 | #include <ipmid-host/cmd.hpp> |
| 30 | #include <ipmid/api.hpp> |
| 31 | #include <ipmid/handler.hpp> |
| 32 | #include <ipmid/message.hpp> |
| 33 | #include <ipmid/oemrouter.hpp> |
Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 34 | #include <ipmid/types.hpp> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 35 | #include <map> |
| 36 | #include <memory> |
| 37 | #include <optional> |
| 38 | #include <phosphor-logging/log.hpp> |
| 39 | #include <sdbusplus/asio/connection.hpp> |
| 40 | #include <sdbusplus/asio/object_server.hpp> |
| 41 | #include <sdbusplus/asio/sd_event.hpp> |
| 42 | #include <sdbusplus/bus.hpp> |
| 43 | #include <sdbusplus/bus/match.hpp> |
| 44 | #include <sdbusplus/timer.hpp> |
| 45 | #include <tuple> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 46 | #include <unordered_map> |
| 47 | #include <utility> |
| 48 | #include <vector> |
| 49 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 50 | namespace fs = std::filesystem; |
| 51 | |
| 52 | using namespace phosphor::logging; |
| 53 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 54 | // IPMI Spec, shared Reservation ID. |
| 55 | static unsigned short selReservationID = 0xFFFF; |
| 56 | static bool selReservationValid = false; |
| 57 | |
| 58 | unsigned short reserveSel(void) |
| 59 | { |
| 60 | // IPMI spec, Reservation ID, the value simply increases against each |
| 61 | // execution of the Reserve SEL command. |
| 62 | if (++selReservationID == 0) |
| 63 | { |
| 64 | selReservationID = 1; |
| 65 | } |
| 66 | selReservationValid = true; |
| 67 | return selReservationID; |
| 68 | } |
| 69 | |
| 70 | bool checkSELReservation(unsigned short id) |
| 71 | { |
| 72 | return (selReservationValid && selReservationID == id); |
| 73 | } |
| 74 | |
| 75 | void cancelSELReservation(void) |
| 76 | { |
| 77 | selReservationValid = false; |
| 78 | } |
| 79 | |
| 80 | EInterfaceIndex getInterfaceIndex(void) |
| 81 | { |
| 82 | return interfaceKCS; |
| 83 | } |
| 84 | |
| 85 | sd_bus* bus; |
| 86 | sd_event* events = nullptr; |
| 87 | sd_event* ipmid_get_sd_event_connection(void) |
| 88 | { |
| 89 | return events; |
| 90 | } |
| 91 | sd_bus* ipmid_get_sd_bus_connection(void) |
| 92 | { |
| 93 | return bus; |
| 94 | } |
| 95 | |
| 96 | namespace ipmi |
| 97 | { |
| 98 | |
| 99 | static inline unsigned int makeCmdKey(unsigned int cluster, unsigned int cmd) |
| 100 | { |
| 101 | return (cluster << 8) | cmd; |
| 102 | } |
| 103 | |
| 104 | using HandlerTuple = std::tuple<int, /* prio */ |
| 105 | Privilege, HandlerBase::ptr /* handler */ |
| 106 | >; |
| 107 | |
| 108 | /* map to handle standard registered commands */ |
| 109 | static std::unordered_map<unsigned int, /* key is NetFn/Cmd */ |
| 110 | HandlerTuple> |
| 111 | handlerMap; |
| 112 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 113 | /* special map for decoding Group registered commands (NetFn 2Ch) */ |
| 114 | static std::unordered_map<unsigned int, /* key is Group/Cmd (NetFn is 2Ch) */ |
| 115 | HandlerTuple> |
| 116 | groupHandlerMap; |
| 117 | |
| 118 | /* special map for decoding OEM registered commands (NetFn 2Eh) */ |
| 119 | static std::unordered_map<unsigned int, /* key is Iana/Cmd (NetFn is 2Eh) */ |
| 120 | HandlerTuple> |
| 121 | oemHandlerMap; |
| 122 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 123 | using FilterTuple = std::tuple<int, /* prio */ |
| 124 | FilterBase::ptr /* filter */ |
| 125 | >; |
| 126 | |
| 127 | /* list to hold all registered ipmi command filters */ |
| 128 | static std::forward_list<FilterTuple> filterList; |
| 129 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 130 | namespace impl |
| 131 | { |
| 132 | /* common function to register all standard IPMI handlers */ |
| 133 | bool registerHandler(int prio, NetFn netFn, Cmd cmd, Privilege priv, |
| 134 | HandlerBase::ptr handler) |
| 135 | { |
| 136 | // check for valid NetFn: even; 00-0Ch, 30-3Eh |
| 137 | if (netFn & 1 || (netFn > netFnTransport && netFn < netFnGroup) || |
| 138 | netFn > netFnOemEight) |
| 139 | { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | // create key and value for this handler |
| 144 | unsigned int netFnCmd = makeCmdKey(netFn, cmd); |
| 145 | HandlerTuple item(prio, priv, handler); |
| 146 | |
| 147 | // consult the handler map and look for a match |
| 148 | auto& mapCmd = handlerMap[netFnCmd]; |
| 149 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 150 | { |
| 151 | mapCmd = item; |
| 152 | return true; |
| 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 157 | /* common function to register all Group IPMI handlers */ |
| 158 | bool registerGroupHandler(int prio, Group group, Cmd cmd, Privilege priv, |
| 159 | HandlerBase::ptr handler) |
| 160 | { |
| 161 | // create key and value for this handler |
| 162 | unsigned int netFnCmd = makeCmdKey(group, cmd); |
| 163 | HandlerTuple item(prio, priv, handler); |
| 164 | |
| 165 | // consult the handler map and look for a match |
| 166 | auto& mapCmd = groupHandlerMap[netFnCmd]; |
| 167 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 168 | { |
| 169 | mapCmd = item; |
| 170 | return true; |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | /* common function to register all OEM IPMI handlers */ |
| 176 | bool registerOemHandler(int prio, Iana iana, Cmd cmd, Privilege priv, |
| 177 | HandlerBase::ptr handler) |
| 178 | { |
| 179 | // create key and value for this handler |
| 180 | unsigned int netFnCmd = makeCmdKey(iana, cmd); |
| 181 | HandlerTuple item(prio, priv, handler); |
| 182 | |
| 183 | // consult the handler map and look for a match |
| 184 | auto& mapCmd = oemHandlerMap[netFnCmd]; |
| 185 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 186 | { |
| 187 | mapCmd = item; |
| 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 193 | /* common function to register all IPMI filter handlers */ |
| 194 | void registerFilter(int prio, FilterBase::ptr filter) |
| 195 | { |
| 196 | // check for initial placement |
| 197 | if (filterList.empty() || std::get<int>(filterList.front()) < prio) |
| 198 | { |
| 199 | filterList.emplace_front(std::make_tuple(prio, filter)); |
| 200 | } |
| 201 | // walk the list and put it in the right place |
| 202 | auto j = filterList.begin(); |
| 203 | for (auto i = j; i != filterList.end() && std::get<int>(*i) > prio; i++) |
| 204 | { |
| 205 | j = i; |
| 206 | } |
| 207 | filterList.emplace_after(j, std::make_tuple(prio, filter)); |
| 208 | } |
| 209 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 210 | } // namespace impl |
| 211 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 212 | message::Response::ptr filterIpmiCommand(message::Request::ptr request) |
| 213 | { |
| 214 | // pass the command through the filter mechanism |
| 215 | // This can be the firmware firewall or any OEM mechanism like |
| 216 | // whitelist filtering based on operational mode |
| 217 | for (auto& item : filterList) |
| 218 | { |
| 219 | FilterBase::ptr filter = std::get<FilterBase::ptr>(item); |
| 220 | ipmi::Cc cc = filter->call(request); |
| 221 | if (ipmi::ccSuccess != cc) |
| 222 | { |
| 223 | return errorResponse(request, cc); |
| 224 | } |
| 225 | } |
| 226 | return message::Response::ptr(); |
| 227 | } |
| 228 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 229 | message::Response::ptr executeIpmiCommandCommon( |
| 230 | std::unordered_map<unsigned int, HandlerTuple>& handlers, |
| 231 | unsigned int keyCommon, message::Request::ptr request) |
| 232 | { |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 233 | // filter the command first; a non-null message::Response::ptr |
| 234 | // means that the message has been rejected for some reason |
| 235 | message::Response::ptr response = filterIpmiCommand(request); |
| 236 | if (response) |
| 237 | { |
| 238 | return response; |
| 239 | } |
| 240 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 241 | Cmd cmd = request->ctx->cmd; |
| 242 | unsigned int key = makeCmdKey(keyCommon, cmd); |
| 243 | auto cmdIter = handlers.find(key); |
| 244 | if (cmdIter != handlers.end()) |
| 245 | { |
| 246 | HandlerTuple& chosen = cmdIter->second; |
| 247 | if (request->ctx->priv < std::get<Privilege>(chosen)) |
| 248 | { |
| 249 | return errorResponse(request, ccInsufficientPrivilege); |
| 250 | } |
| 251 | return std::get<HandlerBase::ptr>(chosen)->call(request); |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | unsigned int wildcard = makeCmdKey(keyCommon, cmdWildcard); |
| 256 | cmdIter = handlers.find(wildcard); |
| 257 | if (cmdIter != handlers.end()) |
| 258 | { |
| 259 | HandlerTuple& chosen = cmdIter->second; |
| 260 | if (request->ctx->priv < std::get<Privilege>(chosen)) |
| 261 | { |
| 262 | return errorResponse(request, ccInsufficientPrivilege); |
| 263 | } |
| 264 | return std::get<HandlerBase::ptr>(chosen)->call(request); |
| 265 | } |
| 266 | } |
| 267 | return errorResponse(request, ccInvalidCommand); |
| 268 | } |
| 269 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 270 | message::Response::ptr executeIpmiGroupCommand(message::Request::ptr request) |
| 271 | { |
| 272 | // look up the group for this request |
| 273 | Group group; |
Vernon Mauery | d35dcd0 | 2019-03-13 08:54:12 -0700 | [diff] [blame] | 274 | if (0 != request->payload.unpack(group)) |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 275 | { |
| 276 | return errorResponse(request, ccReqDataLenInvalid); |
| 277 | } |
| 278 | // The handler will need to unpack group as well; we just need it for lookup |
| 279 | request->payload.reset(); |
| 280 | message::Response::ptr response = |
| 281 | executeIpmiCommandCommon(groupHandlerMap, group, request); |
| 282 | // if the handler should add the group; executeIpmiCommandCommon does not |
| 283 | if (response->cc != ccSuccess && response->payload.size() == 0) |
| 284 | { |
| 285 | response->pack(group); |
| 286 | } |
| 287 | return response; |
| 288 | } |
| 289 | |
| 290 | message::Response::ptr executeIpmiOemCommand(message::Request::ptr request) |
| 291 | { |
| 292 | // look up the iana for this request |
| 293 | Iana iana; |
Vernon Mauery | d35dcd0 | 2019-03-13 08:54:12 -0700 | [diff] [blame] | 294 | if (0 != request->payload.unpack(iana)) |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 295 | { |
| 296 | return errorResponse(request, ccReqDataLenInvalid); |
| 297 | } |
| 298 | request->payload.reset(); |
| 299 | message::Response::ptr response = |
| 300 | executeIpmiCommandCommon(oemHandlerMap, iana, request); |
| 301 | // if the handler should add the iana; executeIpmiCommandCommon does not |
| 302 | if (response->cc != ccSuccess && response->payload.size() == 0) |
| 303 | { |
| 304 | response->pack(iana); |
| 305 | } |
| 306 | return response; |
| 307 | } |
| 308 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 309 | message::Response::ptr executeIpmiCommand(message::Request::ptr request) |
| 310 | { |
| 311 | NetFn netFn = request->ctx->netFn; |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 312 | if (netFnGroup == netFn) |
| 313 | { |
| 314 | return executeIpmiGroupCommand(request); |
| 315 | } |
| 316 | else if (netFnOem == netFn) |
| 317 | { |
| 318 | return executeIpmiOemCommand(request); |
| 319 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 320 | return executeIpmiCommandCommon(handlerMap, netFn, request); |
| 321 | } |
| 322 | |
| 323 | /* called from sdbus async server context */ |
| 324 | auto executionEntry(boost::asio::yield_context yield, NetFn netFn, uint8_t lun, |
| 325 | Cmd cmd, std::vector<uint8_t>& data, |
| 326 | std::map<std::string, ipmi::Value>& options) |
| 327 | { |
| 328 | auto ctx = std::make_shared<ipmi::Context>(netFn, cmd, 0, 0, |
| 329 | ipmi::Privilege::Admin, &yield); |
| 330 | auto request = std::make_shared<ipmi::message::Request>( |
| 331 | ctx, std::forward<std::vector<uint8_t>>(data)); |
| 332 | message::Response::ptr response = executeIpmiCommand(request); |
| 333 | |
| 334 | // Responses in IPMI require a bit set. So there ya go... |
| 335 | netFn |= 0x01; |
| 336 | return std::make_tuple(netFn, lun, cmd, response->cc, |
| 337 | response->payload.raw); |
| 338 | } |
| 339 | |
| 340 | /** @struct IpmiProvider |
| 341 | * |
| 342 | * RAII wrapper for dlopen so that dlclose gets called on exit |
| 343 | */ |
| 344 | struct IpmiProvider |
| 345 | { |
| 346 | public: |
| 347 | /** @brief address of the opened library */ |
| 348 | void* addr; |
| 349 | std::string name; |
| 350 | |
| 351 | IpmiProvider() = delete; |
| 352 | IpmiProvider(const IpmiProvider&) = delete; |
| 353 | IpmiProvider& operator=(const IpmiProvider&) = delete; |
| 354 | IpmiProvider(IpmiProvider&&) = delete; |
| 355 | IpmiProvider& operator=(IpmiProvider&&) = delete; |
| 356 | |
| 357 | /** @brief dlopen a shared object file by path |
| 358 | * @param[in] filename - path of shared object to open |
| 359 | */ |
| 360 | explicit IpmiProvider(const char* fname) : addr(nullptr), name(fname) |
| 361 | { |
| 362 | log<level::DEBUG>("Open IPMI provider library", |
| 363 | entry("PROVIDER=%s", name.c_str())); |
| 364 | try |
| 365 | { |
| 366 | addr = dlopen(name.c_str(), RTLD_NOW); |
| 367 | } |
| 368 | catch (std::exception& e) |
| 369 | { |
| 370 | log<level::ERR>("ERROR opening IPMI provider", |
| 371 | entry("PROVIDER=%s", name.c_str()), |
| 372 | entry("ERROR=%s", e.what())); |
| 373 | } |
| 374 | catch (...) |
| 375 | { |
| 376 | std::exception_ptr eptr = std::current_exception(); |
| 377 | try |
| 378 | { |
| 379 | std::rethrow_exception(eptr); |
| 380 | } |
| 381 | catch (std::exception& e) |
| 382 | { |
| 383 | log<level::ERR>("ERROR opening IPMI provider", |
| 384 | entry("PROVIDER=%s", name.c_str()), |
| 385 | entry("ERROR=%s", e.what())); |
| 386 | } |
| 387 | } |
| 388 | if (!isOpen()) |
| 389 | { |
| 390 | log<level::ERR>("ERROR opening IPMI provider", |
| 391 | entry("PROVIDER=%s", name.c_str()), |
| 392 | entry("ERROR=%s", dlerror())); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | ~IpmiProvider() |
| 397 | { |
| 398 | if (isOpen()) |
| 399 | { |
| 400 | dlclose(addr); |
| 401 | } |
| 402 | } |
| 403 | bool isOpen() const |
| 404 | { |
| 405 | return (nullptr != addr); |
| 406 | } |
| 407 | }; |
| 408 | |
| 409 | // Plugin libraries need to contain .so either at the end or in the middle |
| 410 | constexpr const char ipmiPluginExtn[] = ".so"; |
| 411 | |
| 412 | /* return a list of self-closing library handles */ |
| 413 | std::forward_list<IpmiProvider> loadProviders(const fs::path& ipmiLibsPath) |
| 414 | { |
| 415 | std::vector<fs::path> libs; |
| 416 | for (const auto& libPath : fs::directory_iterator(ipmiLibsPath)) |
| 417 | { |
Vernon Mauery | b0ab5fe | 2019-03-06 14:03:00 -0800 | [diff] [blame] | 418 | std::error_code ec; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 419 | fs::path fname = libPath.path(); |
Vernon Mauery | b0ab5fe | 2019-03-06 14:03:00 -0800 | [diff] [blame] | 420 | if (fs::is_symlink(fname, ec) || ec) |
| 421 | { |
| 422 | // it's a symlink or some other error; skip it |
| 423 | continue; |
| 424 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 425 | while (fname.has_extension()) |
| 426 | { |
| 427 | fs::path extn = fname.extension(); |
| 428 | if (extn == ipmiPluginExtn) |
| 429 | { |
| 430 | libs.push_back(libPath.path()); |
| 431 | break; |
| 432 | } |
| 433 | fname.replace_extension(); |
| 434 | } |
| 435 | } |
| 436 | std::sort(libs.begin(), libs.end()); |
| 437 | |
| 438 | std::forward_list<IpmiProvider> handles; |
| 439 | for (auto& lib : libs) |
| 440 | { |
| 441 | #ifdef __IPMI_DEBUG__ |
| 442 | log<level::DEBUG>("Registering handler", |
| 443 | entry("HANDLER=%s", lib.c_str())); |
| 444 | #endif |
| 445 | handles.emplace_front(lib.c_str()); |
| 446 | } |
| 447 | return handles; |
| 448 | } |
| 449 | |
| 450 | } // namespace ipmi |
| 451 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 452 | #ifdef ALLOW_DEPRECATED_API |
| 453 | /* legacy registration */ |
| 454 | void ipmi_register_callback(ipmi_netfn_t netFn, ipmi_cmd_t cmd, |
| 455 | ipmi_context_t context, ipmid_callback_t handler, |
| 456 | ipmi_cmd_privilege_t priv) |
| 457 | { |
Vernon Mauery | be37630 | 2019-03-21 13:02:05 -0700 | [diff] [blame] | 458 | auto h = ipmi::makeLegacyHandler(handler, context); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 459 | // translate priv from deprecated enum to current |
| 460 | ipmi::Privilege realPriv; |
| 461 | switch (priv) |
| 462 | { |
| 463 | case PRIVILEGE_CALLBACK: |
| 464 | realPriv = ipmi::Privilege::Callback; |
| 465 | break; |
| 466 | case PRIVILEGE_USER: |
| 467 | realPriv = ipmi::Privilege::User; |
| 468 | break; |
| 469 | case PRIVILEGE_OPERATOR: |
| 470 | realPriv = ipmi::Privilege::Operator; |
| 471 | break; |
| 472 | case PRIVILEGE_ADMIN: |
| 473 | realPriv = ipmi::Privilege::Admin; |
| 474 | break; |
| 475 | case PRIVILEGE_OEM: |
| 476 | realPriv = ipmi::Privilege::Oem; |
| 477 | break; |
| 478 | case SYSTEM_INTERFACE: |
| 479 | realPriv = ipmi::Privilege::Admin; |
| 480 | break; |
| 481 | default: |
| 482 | realPriv = ipmi::Privilege::Admin; |
| 483 | break; |
| 484 | } |
Vernon Mauery | e8d4323 | 2019-03-26 16:23:43 -0700 | [diff] [blame] | 485 | // The original ipmi_register_callback allowed for group OEM handlers |
| 486 | // to be registered via this same interface. It just so happened that |
| 487 | // all the handlers were part of the DCMI group, so default to that. |
| 488 | if (netFn == NETFUN_GRPEXT) |
| 489 | { |
| 490 | ipmi::impl::registerGroupHandler(ipmi::prioOpenBmcBase, |
| 491 | dcmi::groupExtId, cmd, realPriv, h); |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | ipmi::impl::registerHandler(ipmi::prioOpenBmcBase, netFn, cmd, realPriv, |
| 496 | h); |
| 497 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 500 | namespace oem |
| 501 | { |
| 502 | |
| 503 | class LegacyRouter : public oem::Router |
| 504 | { |
| 505 | public: |
| 506 | virtual ~LegacyRouter() |
| 507 | { |
| 508 | } |
| 509 | |
| 510 | /// Enable message routing to begin. |
| 511 | void activate() override |
| 512 | { |
| 513 | } |
| 514 | |
| 515 | void registerHandler(Number oen, ipmi_cmd_t cmd, Handler handler) override |
| 516 | { |
| 517 | auto h = ipmi::makeLegacyHandler(std::forward<Handler>(handler)); |
| 518 | ipmi::impl::registerOemHandler(ipmi::prioOpenBmcBase, oen, cmd, |
| 519 | ipmi::Privilege::Admin, h); |
| 520 | } |
| 521 | }; |
| 522 | static LegacyRouter legacyRouter; |
| 523 | |
| 524 | Router* mutableRouter() |
| 525 | { |
| 526 | return &legacyRouter; |
| 527 | } |
| 528 | |
| 529 | } // namespace oem |
| 530 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 531 | /* legacy alternative to executionEntry */ |
| 532 | void handleLegacyIpmiCommand(sdbusplus::message::message& m) |
| 533 | { |
| 534 | unsigned char seq, netFn, lun, cmd; |
| 535 | std::vector<uint8_t> data; |
| 536 | |
| 537 | m.read(seq, netFn, lun, cmd, data); |
| 538 | |
| 539 | auto ctx = std::make_shared<ipmi::Context>(netFn, cmd, 0, 0, |
| 540 | ipmi::Privilege::Admin); |
| 541 | auto request = std::make_shared<ipmi::message::Request>( |
| 542 | ctx, std::forward<std::vector<uint8_t>>(data)); |
| 543 | ipmi::message::Response::ptr response = ipmi::executeIpmiCommand(request); |
| 544 | |
| 545 | // Responses in IPMI require a bit set. So there ya go... |
| 546 | netFn |= 0x01; |
| 547 | |
| 548 | const char *dest, *path; |
| 549 | constexpr const char* DBUS_INTF = "org.openbmc.HostIpmi"; |
| 550 | |
| 551 | dest = m.get_sender(); |
| 552 | path = m.get_path(); |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 553 | getSdBus()->async_method_call([](boost::system::error_code ec) {}, dest, |
| 554 | path, DBUS_INTF, "sendMessage", seq, netFn, |
| 555 | lun, cmd, response->cc, |
| 556 | response->payload.raw); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | #endif /* ALLOW_DEPRECATED_API */ |
| 560 | |
| 561 | // Calls host command manager to do the right thing for the command |
| 562 | using CommandHandler = phosphor::host::command::CommandHandler; |
| 563 | std::unique_ptr<phosphor::host::command::Manager> cmdManager; |
| 564 | void ipmid_send_cmd_to_host(CommandHandler&& cmd) |
| 565 | { |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 566 | return cmdManager->execute(std::forward<CommandHandler>(cmd)); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | std::unique_ptr<phosphor::host::command::Manager>& ipmid_get_host_cmd_manager() |
| 570 | { |
| 571 | return cmdManager; |
| 572 | } |
| 573 | |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 574 | // These are symbols that are present in libipmid, but not expected |
| 575 | // to be used except here (or maybe a unit test), so declare them here |
| 576 | extern void setIoContext(std::shared_ptr<boost::asio::io_context>& newIo); |
| 577 | extern void setSdBus(std::shared_ptr<sdbusplus::asio::connection>& newBus); |
| 578 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 579 | int main(int argc, char* argv[]) |
| 580 | { |
| 581 | // Connect to system bus |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 582 | auto io = std::make_shared<boost::asio::io_context>(); |
| 583 | setIoContext(io); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 584 | if (argc > 1 && std::string(argv[1]) == "-session") |
| 585 | { |
| 586 | sd_bus_default_user(&bus); |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | sd_bus_default_system(&bus); |
| 591 | } |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 592 | auto sdbusp = std::make_shared<sdbusplus::asio::connection>(*io, bus); |
| 593 | setSdBus(sdbusp); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 594 | sdbusp->request_name("xyz.openbmc_project.Ipmi.Host"); |
| 595 | |
| 596 | // TODO: Hack to keep the sdEvents running.... Not sure why the sd_event |
| 597 | // queue stops running if we don't have a timer that keeps re-arming |
| 598 | phosphor::Timer t2([]() { ; }); |
| 599 | t2.start(std::chrono::microseconds(500000), true); |
| 600 | |
| 601 | // TODO: Remove all vestiges of sd_event from phosphor-host-ipmid |
| 602 | // until that is done, add the sd_event wrapper to the io object |
| 603 | sdbusplus::asio::sd_event_wrapper sdEvents(*io); |
| 604 | |
| 605 | cmdManager = std::make_unique<phosphor::host::command::Manager>(*sdbusp); |
| 606 | |
| 607 | // Register all command providers and filters |
Vernon Mauery | 4ec4e40 | 2019-03-20 13:09:27 -0700 | [diff] [blame] | 608 | std::forward_list<ipmi::IpmiProvider> providers = |
| 609 | ipmi::loadProviders(HOST_IPMI_LIB_PATH); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 610 | |
| 611 | // Add bindings for inbound IPMI requests |
| 612 | auto server = sdbusplus::asio::object_server(sdbusp); |
| 613 | auto iface = server.add_interface("/xyz/openbmc_project/Ipmi", |
| 614 | "xyz.openbmc_project.Ipmi.Server"); |
| 615 | iface->register_method("execute", ipmi::executionEntry); |
| 616 | iface->initialize(); |
| 617 | |
| 618 | #ifdef ALLOW_DEPRECATED_API |
| 619 | // listen on deprecated signal interface for kcs/bt commands |
| 620 | constexpr const char* FILTER = "type='signal',interface='org.openbmc." |
| 621 | "HostIpmi',member='ReceivedMessage'"; |
| 622 | sdbusplus::bus::match::match oldIpmiInterface(*sdbusp, FILTER, |
| 623 | handleLegacyIpmiCommand); |
| 624 | #endif /* ALLOW_DEPRECATED_API */ |
| 625 | |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 626 | // set up boost::asio signal handling |
| 627 | std::function<SignalResponse(int)> stopAsioRunLoop = |
| 628 | [&io](int signalNumber) { |
| 629 | log<level::INFO>("Received signal; quitting", |
| 630 | entry("SIGNAL=%d", signalNumber)); |
| 631 | io->stop(); |
| 632 | return SignalResponse::breakExecution; |
| 633 | }; |
| 634 | registerSignalHandler(ipmi::prioOpenBmcBase, SIGINT, stopAsioRunLoop); |
| 635 | registerSignalHandler(ipmi::prioOpenBmcBase, SIGTERM, stopAsioRunLoop); |
| 636 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 637 | io->run(); |
| 638 | |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 639 | // destroy all the IPMI handlers so the providers can unload safely |
| 640 | ipmi::handlerMap.clear(); |
| 641 | ipmi::groupHandlerMap.clear(); |
| 642 | ipmi::oemHandlerMap.clear(); |
| 643 | ipmi::filterList.clear(); |
| 644 | // unload the provider libraries |
Vernon Mauery | 4ec4e40 | 2019-03-20 13:09:27 -0700 | [diff] [blame] | 645 | providers.clear(); |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 646 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 647 | return 0; |
| 648 | } |