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 | |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 22 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 9860505 | 2025-02-13 16:57:13 -0800 | [diff] [blame^] | 23 | #include <boost/asio/detached.hpp> |
Ed Tanous | 778418d | 2020-08-17 23:20:21 -0700 | [diff] [blame] | 24 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 26a386f | 2025-02-13 16:57:13 -0800 | [diff] [blame] | 25 | #include <boost/asio/spawn.hpp> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 26 | #include <host-cmd-manager.hpp> |
| 27 | #include <ipmid-host/cmd.hpp> |
| 28 | #include <ipmid/api.hpp> |
| 29 | #include <ipmid/handler.hpp> |
| 30 | #include <ipmid/message.hpp> |
| 31 | #include <ipmid/oemrouter.hpp> |
Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 32 | #include <ipmid/types.hpp> |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 33 | #include <phosphor-logging/lg2.hpp> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 34 | #include <sdbusplus/asio/connection.hpp> |
| 35 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Williams | 3b301a3 | 2025-02-03 13:05:36 -0500 | [diff] [blame] | 36 | #include <sdbusplus/asio/sd_event.hpp> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 37 | #include <sdbusplus/bus.hpp> |
| 38 | #include <sdbusplus/bus/match.hpp> |
| 39 | #include <sdbusplus/timer.hpp> |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 40 | |
| 41 | #include <algorithm> |
| 42 | #include <any> |
| 43 | #include <exception> |
| 44 | #include <filesystem> |
| 45 | #include <forward_list> |
| 46 | #include <map> |
| 47 | #include <memory> |
| 48 | #include <optional> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 49 | #include <tuple> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 50 | #include <unordered_map> |
| 51 | #include <utility> |
| 52 | #include <vector> |
| 53 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 54 | namespace fs = std::filesystem; |
| 55 | |
| 56 | using namespace phosphor::logging; |
| 57 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 58 | // IPMI Spec, shared Reservation ID. |
| 59 | static unsigned short selReservationID = 0xFFFF; |
| 60 | static bool selReservationValid = false; |
| 61 | |
| 62 | unsigned short reserveSel(void) |
| 63 | { |
| 64 | // IPMI spec, Reservation ID, the value simply increases against each |
| 65 | // execution of the Reserve SEL command. |
| 66 | if (++selReservationID == 0) |
| 67 | { |
| 68 | selReservationID = 1; |
| 69 | } |
| 70 | selReservationValid = true; |
| 71 | return selReservationID; |
| 72 | } |
| 73 | |
| 74 | bool checkSELReservation(unsigned short id) |
| 75 | { |
| 76 | return (selReservationValid && selReservationID == id); |
| 77 | } |
| 78 | |
| 79 | void cancelSELReservation(void) |
| 80 | { |
| 81 | selReservationValid = false; |
| 82 | } |
| 83 | |
| 84 | EInterfaceIndex getInterfaceIndex(void) |
| 85 | { |
| 86 | return interfaceKCS; |
| 87 | } |
| 88 | |
| 89 | sd_bus* bus; |
Patrick Williams | 3b301a3 | 2025-02-03 13:05:36 -0500 | [diff] [blame] | 90 | sd_event* events = nullptr; |
| 91 | sd_event* ipmid_get_sd_event_connection(void) |
| 92 | { |
| 93 | return events; |
| 94 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 95 | sd_bus* ipmid_get_sd_bus_connection(void) |
| 96 | { |
| 97 | return bus; |
| 98 | } |
| 99 | |
| 100 | namespace ipmi |
| 101 | { |
| 102 | |
| 103 | static inline unsigned int makeCmdKey(unsigned int cluster, unsigned int cmd) |
| 104 | { |
| 105 | return (cluster << 8) | cmd; |
| 106 | } |
| 107 | |
| 108 | using HandlerTuple = std::tuple<int, /* prio */ |
| 109 | Privilege, HandlerBase::ptr /* handler */ |
| 110 | >; |
| 111 | |
| 112 | /* map to handle standard registered commands */ |
| 113 | static std::unordered_map<unsigned int, /* key is NetFn/Cmd */ |
| 114 | HandlerTuple> |
| 115 | handlerMap; |
| 116 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 117 | /* special map for decoding Group registered commands (NetFn 2Ch) */ |
| 118 | static std::unordered_map<unsigned int, /* key is Group/Cmd (NetFn is 2Ch) */ |
| 119 | HandlerTuple> |
| 120 | groupHandlerMap; |
| 121 | |
| 122 | /* special map for decoding OEM registered commands (NetFn 2Eh) */ |
| 123 | static std::unordered_map<unsigned int, /* key is Iana/Cmd (NetFn is 2Eh) */ |
| 124 | HandlerTuple> |
| 125 | oemHandlerMap; |
| 126 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 127 | using FilterTuple = std::tuple<int, /* prio */ |
| 128 | FilterBase::ptr /* filter */ |
| 129 | >; |
| 130 | |
| 131 | /* list to hold all registered ipmi command filters */ |
| 132 | static std::forward_list<FilterTuple> filterList; |
| 133 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 134 | namespace impl |
| 135 | { |
| 136 | /* common function to register all standard IPMI handlers */ |
| 137 | bool registerHandler(int prio, NetFn netFn, Cmd cmd, Privilege priv, |
| 138 | HandlerBase::ptr handler) |
| 139 | { |
| 140 | // check for valid NetFn: even; 00-0Ch, 30-3Eh |
| 141 | if (netFn & 1 || (netFn > netFnTransport && netFn < netFnGroup) || |
| 142 | netFn > netFnOemEight) |
| 143 | { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | // create key and value for this handler |
| 148 | unsigned int netFnCmd = makeCmdKey(netFn, cmd); |
| 149 | HandlerTuple item(prio, priv, handler); |
| 150 | |
| 151 | // consult the handler map and look for a match |
| 152 | auto& mapCmd = handlerMap[netFnCmd]; |
| 153 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 154 | { |
| 155 | mapCmd = item; |
| 156 | return true; |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 161 | /* common function to register all Group IPMI handlers */ |
| 162 | bool registerGroupHandler(int prio, Group group, Cmd cmd, Privilege priv, |
| 163 | HandlerBase::ptr handler) |
| 164 | { |
| 165 | // create key and value for this handler |
| 166 | unsigned int netFnCmd = makeCmdKey(group, cmd); |
| 167 | HandlerTuple item(prio, priv, handler); |
| 168 | |
| 169 | // consult the handler map and look for a match |
| 170 | auto& mapCmd = groupHandlerMap[netFnCmd]; |
| 171 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 172 | { |
| 173 | mapCmd = item; |
| 174 | return true; |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | /* common function to register all OEM IPMI handlers */ |
| 180 | bool registerOemHandler(int prio, Iana iana, Cmd cmd, Privilege priv, |
| 181 | HandlerBase::ptr handler) |
| 182 | { |
| 183 | // create key and value for this handler |
| 184 | unsigned int netFnCmd = makeCmdKey(iana, cmd); |
| 185 | HandlerTuple item(prio, priv, handler); |
| 186 | |
| 187 | // consult the handler map and look for a match |
| 188 | auto& mapCmd = oemHandlerMap[netFnCmd]; |
| 189 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 190 | { |
| 191 | mapCmd = item; |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 192 | lg2::debug("registered OEM Handler: NetFn/Cmd={NETFNCMD}", "IANA", |
| 193 | lg2::hex, iana, "CMD", lg2::hex, cmd, "NETFNCMD", lg2::hex, |
| 194 | netFnCmd); |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 195 | return true; |
| 196 | } |
Alexander Hansen | 7197b34 | 2023-09-06 11:45:15 +0200 | [diff] [blame] | 197 | |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 198 | lg2::warning("could not register OEM Handler: NetFn/Cmd={NETFNCMD}", "IANA", |
| 199 | lg2::hex, iana, "CMD", lg2::hex, cmd, "NETFNCMD", lg2::hex, |
| 200 | netFnCmd); |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 204 | /* common function to register all IPMI filter handlers */ |
| 205 | void registerFilter(int prio, FilterBase::ptr filter) |
| 206 | { |
| 207 | // check for initial placement |
| 208 | if (filterList.empty() || std::get<int>(filterList.front()) < prio) |
| 209 | { |
| 210 | filterList.emplace_front(std::make_tuple(prio, filter)); |
Yong Li | be06323 | 2021-03-04 16:52:52 +0800 | [diff] [blame] | 211 | return; |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 212 | } |
| 213 | // walk the list and put it in the right place |
| 214 | auto j = filterList.begin(); |
| 215 | for (auto i = j; i != filterList.end() && std::get<int>(*i) > prio; i++) |
| 216 | { |
| 217 | j = i; |
| 218 | } |
| 219 | filterList.emplace_after(j, std::make_tuple(prio, filter)); |
| 220 | } |
| 221 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 222 | } // namespace impl |
| 223 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 224 | message::Response::ptr filterIpmiCommand(message::Request::ptr request) |
| 225 | { |
| 226 | // pass the command through the filter mechanism |
| 227 | // This can be the firmware firewall or any OEM mechanism like |
| 228 | // whitelist filtering based on operational mode |
| 229 | for (auto& item : filterList) |
| 230 | { |
| 231 | FilterBase::ptr filter = std::get<FilterBase::ptr>(item); |
| 232 | ipmi::Cc cc = filter->call(request); |
| 233 | if (ipmi::ccSuccess != cc) |
| 234 | { |
| 235 | return errorResponse(request, cc); |
| 236 | } |
| 237 | } |
| 238 | return message::Response::ptr(); |
| 239 | } |
| 240 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 241 | message::Response::ptr executeIpmiCommandCommon( |
| 242 | std::unordered_map<unsigned int, HandlerTuple>& handlers, |
| 243 | unsigned int keyCommon, message::Request::ptr request) |
| 244 | { |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 245 | // filter the command first; a non-null message::Response::ptr |
| 246 | // means that the message has been rejected for some reason |
Vernon Mauery | 51f7814 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 247 | message::Response::ptr filterResponse = filterIpmiCommand(request); |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 248 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 249 | Cmd cmd = request->ctx->cmd; |
| 250 | unsigned int key = makeCmdKey(keyCommon, cmd); |
| 251 | auto cmdIter = handlers.find(key); |
| 252 | if (cmdIter != handlers.end()) |
| 253 | { |
Vernon Mauery | 51f7814 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 254 | // only return the filter response if the command is found |
| 255 | if (filterResponse) |
| 256 | { |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 257 | lg2::debug("request for NetFn/Cmd {NETFN}/{CMD} has been filtered", |
| 258 | "NETFN", lg2::hex, keyCommon, "CMD", lg2::hex, cmd); |
Vernon Mauery | 51f7814 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 259 | return filterResponse; |
| 260 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 261 | HandlerTuple& chosen = cmdIter->second; |
| 262 | if (request->ctx->priv < std::get<Privilege>(chosen)) |
| 263 | { |
| 264 | return errorResponse(request, ccInsufficientPrivilege); |
| 265 | } |
| 266 | return std::get<HandlerBase::ptr>(chosen)->call(request); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | unsigned int wildcard = makeCmdKey(keyCommon, cmdWildcard); |
| 271 | cmdIter = handlers.find(wildcard); |
| 272 | if (cmdIter != handlers.end()) |
| 273 | { |
Vernon Mauery | 51f7814 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 274 | // only return the filter response if the command is found |
| 275 | if (filterResponse) |
| 276 | { |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 277 | lg2::debug( |
| 278 | "request for NetFn/Cmd {NETFN}/{CMD} has been filtered", |
| 279 | "NETFN", lg2::hex, keyCommon, "CMD", lg2::hex, cmd); |
Vernon Mauery | 51f7814 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 280 | return filterResponse; |
| 281 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 282 | HandlerTuple& chosen = cmdIter->second; |
| 283 | if (request->ctx->priv < std::get<Privilege>(chosen)) |
| 284 | { |
| 285 | return errorResponse(request, ccInsufficientPrivilege); |
| 286 | } |
| 287 | return std::get<HandlerBase::ptr>(chosen)->call(request); |
| 288 | } |
| 289 | } |
| 290 | return errorResponse(request, ccInvalidCommand); |
| 291 | } |
| 292 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 293 | message::Response::ptr executeIpmiGroupCommand(message::Request::ptr request) |
| 294 | { |
| 295 | // look up the group for this request |
William A. Kennington III | d10d905 | 2019-04-24 01:57:36 -0700 | [diff] [blame] | 296 | uint8_t bytes; |
| 297 | if (0 != request->payload.unpack(bytes)) |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 298 | { |
| 299 | return errorResponse(request, ccReqDataLenInvalid); |
| 300 | } |
William A. Kennington III | d10d905 | 2019-04-24 01:57:36 -0700 | [diff] [blame] | 301 | auto group = static_cast<Group>(bytes); |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 302 | message::Response::ptr response = |
| 303 | executeIpmiCommandCommon(groupHandlerMap, group, request); |
William A. Kennington III | da31f9a | 2019-04-25 01:36:32 -0700 | [diff] [blame] | 304 | ipmi::message::Payload prefix; |
| 305 | prefix.pack(bytes); |
| 306 | response->prepend(prefix); |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 307 | return response; |
| 308 | } |
| 309 | |
| 310 | message::Response::ptr executeIpmiOemCommand(message::Request::ptr request) |
| 311 | { |
| 312 | // look up the iana for this request |
William A. Kennington III | d10d905 | 2019-04-24 01:57:36 -0700 | [diff] [blame] | 313 | uint24_t bytes; |
| 314 | if (0 != request->payload.unpack(bytes)) |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 315 | { |
| 316 | return errorResponse(request, ccReqDataLenInvalid); |
| 317 | } |
William A. Kennington III | d10d905 | 2019-04-24 01:57:36 -0700 | [diff] [blame] | 318 | auto iana = static_cast<Iana>(bytes); |
Alexander Hansen | 7197b34 | 2023-09-06 11:45:15 +0200 | [diff] [blame] | 319 | |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 320 | lg2::debug("unpack IANA {IANA}", "IANA", lg2::hex, iana); |
Alexander Hansen | 7197b34 | 2023-09-06 11:45:15 +0200 | [diff] [blame] | 321 | |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 322 | message::Response::ptr response = |
| 323 | executeIpmiCommandCommon(oemHandlerMap, iana, request); |
William A. Kennington III | da31f9a | 2019-04-25 01:36:32 -0700 | [diff] [blame] | 324 | ipmi::message::Payload prefix; |
| 325 | prefix.pack(bytes); |
| 326 | response->prepend(prefix); |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 327 | return response; |
| 328 | } |
| 329 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 330 | message::Response::ptr executeIpmiCommand(message::Request::ptr request) |
| 331 | { |
| 332 | NetFn netFn = request->ctx->netFn; |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 333 | if (netFnGroup == netFn) |
| 334 | { |
| 335 | return executeIpmiGroupCommand(request); |
| 336 | } |
| 337 | else if (netFnOem == netFn) |
| 338 | { |
| 339 | return executeIpmiOemCommand(request); |
| 340 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 341 | return executeIpmiCommandCommon(handlerMap, netFn, request); |
| 342 | } |
| 343 | |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 344 | namespace utils |
| 345 | { |
| 346 | template <typename AssocContainer, typename UnaryPredicate> |
| 347 | void assoc_erase_if(AssocContainer& c, UnaryPredicate p) |
| 348 | { |
| 349 | typename AssocContainer::iterator next = c.begin(); |
| 350 | typename AssocContainer::iterator last = c.end(); |
| 351 | while ((next = std::find_if(next, last, p)) != last) |
| 352 | { |
| 353 | c.erase(next++); |
| 354 | } |
| 355 | } |
| 356 | } // namespace utils |
| 357 | |
| 358 | namespace |
| 359 | { |
| 360 | std::unordered_map<std::string, uint8_t> uniqueNameToChannelNumber; |
| 361 | |
| 362 | // sdbusplus::bus::match::rules::arg0namespace() wants the prefix |
| 363 | // to match without any trailing '.' |
| 364 | constexpr const char ipmiDbusChannelMatch[] = |
| 365 | "xyz.openbmc_project.Ipmi.Channel"; |
| 366 | void updateOwners(sdbusplus::asio::connection& conn, const std::string& name) |
| 367 | { |
| 368 | conn.async_method_call( |
| 369 | [name](const boost::system::error_code ec, |
| 370 | const std::string& nameOwner) { |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 371 | if (ec) |
| 372 | { |
| 373 | lg2::error("Error getting dbus owner for {INTERFACE}", |
| 374 | "INTERFACE", name); |
| 375 | return; |
| 376 | } |
| 377 | // start after ipmiDbusChannelPrefix (after the '.') |
| 378 | std::string chName = |
| 379 | name.substr(std::strlen(ipmiDbusChannelMatch) + 1); |
| 380 | try |
| 381 | { |
| 382 | uint8_t channel = getChannelByName(chName); |
| 383 | uniqueNameToChannelNumber[nameOwner] = channel; |
| 384 | lg2::info( |
| 385 | "New interface mapping: {INTERFACE} -> channel {CHANNEL}", |
| 386 | "INTERFACE", name, "CHANNEL", channel); |
| 387 | } |
| 388 | catch (const std::exception& e) |
| 389 | { |
| 390 | lg2::info("Failed interface mapping, no such name: {INTERFACE}", |
| 391 | "INTERFACE", name); |
| 392 | } |
| 393 | }, |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 394 | "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", |
| 395 | name); |
| 396 | } |
| 397 | |
Jayanth Othayoth | 9f3073a | 2024-12-08 09:29:43 -0600 | [diff] [blame] | 398 | void doListNames(sdbusplus::asio::connection& conn) |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 399 | { |
| 400 | conn.async_method_call( |
Jayanth Othayoth | 9f3073a | 2024-12-08 09:29:43 -0600 | [diff] [blame] | 401 | [&conn](const boost::system::error_code ec, |
| 402 | std::vector<std::string> busNames) { |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 403 | if (ec) |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 404 | { |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 405 | lg2::error("Error getting dbus names: {ERROR}", "ERROR", |
| 406 | ec.message()); |
| 407 | std::exit(EXIT_FAILURE); |
| 408 | return; |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 409 | } |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 410 | // Try to make startup consistent |
| 411 | std::sort(busNames.begin(), busNames.end()); |
| 412 | |
| 413 | const std::string channelPrefix = |
| 414 | std::string(ipmiDbusChannelMatch) + "."; |
| 415 | for (const std::string& busName : busNames) |
| 416 | { |
| 417 | if (busName.find(channelPrefix) == 0) |
| 418 | { |
| 419 | updateOwners(conn, busName); |
| 420 | } |
| 421 | } |
| 422 | }, |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 423 | "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", |
| 424 | "ListNames"); |
| 425 | } |
| 426 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 427 | void nameChangeHandler(sdbusplus::message_t& message) |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 428 | { |
| 429 | std::string name; |
| 430 | std::string oldOwner; |
| 431 | std::string newOwner; |
| 432 | |
| 433 | message.read(name, oldOwner, newOwner); |
| 434 | |
| 435 | if (!oldOwner.empty()) |
| 436 | { |
| 437 | if (boost::starts_with(oldOwner, ":")) |
| 438 | { |
| 439 | // Connection removed |
| 440 | auto it = uniqueNameToChannelNumber.find(oldOwner); |
| 441 | if (it != uniqueNameToChannelNumber.end()) |
| 442 | { |
| 443 | uniqueNameToChannelNumber.erase(it); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | if (!newOwner.empty()) |
| 448 | { |
| 449 | // start after ipmiDbusChannelMatch (and after the '.') |
| 450 | std::string chName = name.substr(std::strlen(ipmiDbusChannelMatch) + 1); |
| 451 | try |
| 452 | { |
| 453 | uint8_t channel = getChannelByName(chName); |
| 454 | uniqueNameToChannelNumber[newOwner] = channel; |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 455 | lg2::info("New interface mapping: {INTERFACE} -> channel {CHANNEL}", |
| 456 | "INTERFACE", name, "CHANNEL", channel); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 457 | } |
| 458 | catch (const std::exception& e) |
| 459 | { |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 460 | lg2::info("Failed interface mapping, no such name: {INTERFACE}", |
| 461 | "INTERFACE", name); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | }; |
| 465 | |
| 466 | } // anonymous namespace |
| 467 | |
| 468 | static constexpr const char intraBmcName[] = "INTRABMC"; |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 469 | uint8_t channelFromMessage(sdbusplus::message_t& msg) |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 470 | { |
| 471 | // channel name for ipmitool to resolve to |
| 472 | std::string sender = msg.get_sender(); |
| 473 | auto chIter = uniqueNameToChannelNumber.find(sender); |
| 474 | if (chIter != uniqueNameToChannelNumber.end()) |
| 475 | { |
| 476 | return chIter->second; |
| 477 | } |
| 478 | // FIXME: currently internal connections are ephemeral and hard to pin down |
| 479 | try |
| 480 | { |
| 481 | return getChannelByName(intraBmcName); |
| 482 | } |
| 483 | catch (const std::exception& e) |
| 484 | { |
| 485 | return invalidChannel; |
| 486 | } |
| 487 | } // namespace ipmi |
| 488 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 489 | /* called from sdbus async server context */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 490 | auto executionEntry(boost::asio::yield_context yield, sdbusplus::message_t& m, |
| 491 | NetFn netFn, uint8_t lun, Cmd cmd, ipmi::SecureBuffer& data, |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 492 | std::map<std::string, ipmi::Value>& options) |
| 493 | { |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 494 | const auto dbusResponse = |
Vernon Mauery | 997952a | 2021-07-30 14:06:14 -0700 | [diff] [blame] | 495 | [netFn, lun, cmd](Cc cc, const ipmi::SecureBuffer& data = {}) { |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 496 | constexpr uint8_t netFnResponse = 0x01; |
| 497 | uint8_t retNetFn = netFn | netFnResponse; |
| 498 | return std::make_tuple(retNetFn, lun, cmd, cc, data); |
| 499 | }; |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 500 | std::string sender = m.get_sender(); |
| 501 | Privilege privilege = Privilege::None; |
Vernon Mauery | d6a2da0 | 2019-04-09 16:00:46 -0700 | [diff] [blame] | 502 | int rqSA = 0; |
Kumar Thangavel | f7d081f | 2020-08-19 20:41:18 +0530 | [diff] [blame] | 503 | int hostIdx = 0; |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 504 | uint8_t userId = 0; // undefined user |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 505 | uint32_t sessionId = 0; |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 506 | |
| 507 | // figure out what channel the request came in on |
| 508 | uint8_t channel = channelFromMessage(m); |
| 509 | if (channel == invalidChannel) |
| 510 | { |
| 511 | // unknown sender channel; refuse to service the request |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 512 | lg2::error("ERROR determining source IPMI channel from " |
| 513 | "{SENDER} NetFn/Cmd {NETFN}/{CMD}", |
| 514 | "SENDER", sender, "NETFN", lg2::hex, netFn, "CMD", lg2::hex, |
| 515 | cmd); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 516 | return dbusResponse(ipmi::ccDestinationUnavailable); |
| 517 | } |
| 518 | |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 519 | // session-based channels are required to provide userId, privilege and |
| 520 | // sessionId |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 521 | if (getChannelSessionSupport(channel) != EChannelSessSupported::none) |
| 522 | { |
| 523 | try |
| 524 | { |
| 525 | Value requestPriv = options.at("privilege"); |
| 526 | Value requestUserId = options.at("userId"); |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 527 | Value requestSessionId = options.at("currentSessionId"); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 528 | privilege = static_cast<Privilege>(std::get<int>(requestPriv)); |
| 529 | userId = static_cast<uint8_t>(std::get<int>(requestUserId)); |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 530 | sessionId = |
| 531 | static_cast<uint32_t>(std::get<uint32_t>(requestSessionId)); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 532 | } |
| 533 | catch (const std::exception& e) |
| 534 | { |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 535 | lg2::error("ERROR determining IPMI session credentials on " |
| 536 | "channel {CHANNEL} for userid {USERID}", |
| 537 | "CHANNEL", channel, "USERID", userId, "NETFN", lg2::hex, |
| 538 | netFn, "CMD", lg2::hex, cmd); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 539 | return dbusResponse(ipmi::ccUnspecifiedError); |
| 540 | } |
| 541 | } |
| 542 | else |
| 543 | { |
| 544 | // get max privilege for session-less channels |
| 545 | // For now, there is not a way to configure this, default to Admin |
| 546 | privilege = Privilege::Admin; |
Vernon Mauery | d6a2da0 | 2019-04-09 16:00:46 -0700 | [diff] [blame] | 547 | |
| 548 | // ipmb should supply rqSA |
| 549 | ChannelInfo chInfo; |
| 550 | getChannelInfo(channel, chInfo); |
| 551 | if (static_cast<EChannelMediumType>(chInfo.mediumType) == |
| 552 | EChannelMediumType::ipmb) |
| 553 | { |
| 554 | const auto iter = options.find("rqSA"); |
| 555 | if (iter != options.end()) |
| 556 | { |
| 557 | if (std::holds_alternative<int>(iter->second)) |
| 558 | { |
| 559 | rqSA = std::get<int>(iter->second); |
| 560 | } |
| 561 | } |
Kumar Thangavel | f7d081f | 2020-08-19 20:41:18 +0530 | [diff] [blame] | 562 | const auto iteration = options.find("hostId"); |
| 563 | if (iteration != options.end()) |
| 564 | { |
| 565 | if (std::holds_alternative<int>(iteration->second)) |
| 566 | { |
| 567 | hostIdx = std::get<int>(iteration->second); |
| 568 | } |
| 569 | } |
Vernon Mauery | d6a2da0 | 2019-04-09 16:00:46 -0700 | [diff] [blame] | 570 | } |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 571 | } |
| 572 | // check to see if the requested priv/username is valid |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 573 | lg2::debug("Set up ipmi context: Ch:NetFn/Cmd={CHANNEL}:{NETFN}/{CMD}", |
| 574 | "SENDER", sender, "NETFN", lg2::hex, netFn, "LUN", lg2::hex, lun, |
| 575 | "CMD", lg2::hex, cmd, "CHANNEL", channel, "USERID", userId, |
| 576 | "SESSIONID", lg2::hex, sessionId, "PRIVILEGE", |
| 577 | static_cast<uint8_t>(privilege), "RQSA", lg2::hex, rqSA); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 578 | |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 579 | auto ctx = std::make_shared<ipmi::Context>( |
| 580 | getSdBus(), netFn, lun, cmd, channel, userId, sessionId, privilege, |
| 581 | rqSA, hostIdx, yield); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 582 | auto request = std::make_shared<ipmi::message::Request>( |
Vernon Mauery | 997952a | 2021-07-30 14:06:14 -0700 | [diff] [blame] | 583 | ctx, std::forward<ipmi::SecureBuffer>(data)); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 584 | message::Response::ptr response = executeIpmiCommand(request); |
| 585 | |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 586 | return dbusResponse(response->cc, response->payload.raw); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /** @struct IpmiProvider |
| 590 | * |
| 591 | * RAII wrapper for dlopen so that dlclose gets called on exit |
| 592 | */ |
| 593 | struct IpmiProvider |
| 594 | { |
| 595 | public: |
| 596 | /** @brief address of the opened library */ |
| 597 | void* addr; |
| 598 | std::string name; |
| 599 | |
| 600 | IpmiProvider() = delete; |
| 601 | IpmiProvider(const IpmiProvider&) = delete; |
| 602 | IpmiProvider& operator=(const IpmiProvider&) = delete; |
| 603 | IpmiProvider(IpmiProvider&&) = delete; |
| 604 | IpmiProvider& operator=(IpmiProvider&&) = delete; |
| 605 | |
| 606 | /** @brief dlopen a shared object file by path |
| 607 | * @param[in] filename - path of shared object to open |
| 608 | */ |
| 609 | explicit IpmiProvider(const char* fname) : addr(nullptr), name(fname) |
| 610 | { |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 611 | lg2::debug("Open IPMI provider library: {PROVIDER}", "PROVIDER", name); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 612 | try |
| 613 | { |
| 614 | addr = dlopen(name.c_str(), RTLD_NOW); |
| 615 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 616 | catch (const std::exception& e) |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 617 | { |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 618 | lg2::error("ERROR opening IPMI provider {PROVIDER}: {ERROR}", |
| 619 | "PROVIDER", name, "ERROR", e); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 620 | } |
| 621 | catch (...) |
| 622 | { |
Vernon Mauery | 03d7a4b | 2021-01-19 11:22:17 -0800 | [diff] [blame] | 623 | const char* what = currentExceptionType(); |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 624 | lg2::error("ERROR opening IPMI provider {PROVIDER}: {ERROR}", |
| 625 | "PROVIDER", name, "ERROR", what); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 626 | } |
| 627 | if (!isOpen()) |
| 628 | { |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 629 | lg2::error("ERROR opening IPMI provider {PROVIDER}: {ERROR}", |
| 630 | "PROVIDER", name, "ERROR", dlerror()); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
| 634 | ~IpmiProvider() |
| 635 | { |
| 636 | if (isOpen()) |
| 637 | { |
| 638 | dlclose(addr); |
| 639 | } |
| 640 | } |
| 641 | bool isOpen() const |
| 642 | { |
| 643 | return (nullptr != addr); |
| 644 | } |
| 645 | }; |
| 646 | |
| 647 | // Plugin libraries need to contain .so either at the end or in the middle |
| 648 | constexpr const char ipmiPluginExtn[] = ".so"; |
| 649 | |
| 650 | /* return a list of self-closing library handles */ |
| 651 | std::forward_list<IpmiProvider> loadProviders(const fs::path& ipmiLibsPath) |
| 652 | { |
| 653 | std::vector<fs::path> libs; |
| 654 | for (const auto& libPath : fs::directory_iterator(ipmiLibsPath)) |
| 655 | { |
Vernon Mauery | b0ab5fe | 2019-03-06 14:03:00 -0800 | [diff] [blame] | 656 | std::error_code ec; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 657 | fs::path fname = libPath.path(); |
Vernon Mauery | b0ab5fe | 2019-03-06 14:03:00 -0800 | [diff] [blame] | 658 | if (fs::is_symlink(fname, ec) || ec) |
| 659 | { |
| 660 | // it's a symlink or some other error; skip it |
| 661 | continue; |
| 662 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 663 | while (fname.has_extension()) |
| 664 | { |
| 665 | fs::path extn = fname.extension(); |
| 666 | if (extn == ipmiPluginExtn) |
| 667 | { |
| 668 | libs.push_back(libPath.path()); |
| 669 | break; |
| 670 | } |
| 671 | fname.replace_extension(); |
| 672 | } |
| 673 | } |
| 674 | std::sort(libs.begin(), libs.end()); |
| 675 | |
| 676 | std::forward_list<IpmiProvider> handles; |
| 677 | for (auto& lib : libs) |
| 678 | { |
| 679 | #ifdef __IPMI_DEBUG__ |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 680 | lg2::debug("Registering handler {HANDLER}", "HANDLER", lib); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 681 | #endif |
| 682 | handles.emplace_front(lib.c_str()); |
| 683 | } |
| 684 | return handles; |
| 685 | } |
| 686 | |
| 687 | } // namespace ipmi |
| 688 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 689 | #ifdef ALLOW_DEPRECATED_API |
| 690 | /* legacy registration */ |
| 691 | void ipmi_register_callback(ipmi_netfn_t netFn, ipmi_cmd_t cmd, |
| 692 | ipmi_context_t context, ipmid_callback_t handler, |
| 693 | ipmi_cmd_privilege_t priv) |
| 694 | { |
Vernon Mauery | be37630 | 2019-03-21 13:02:05 -0700 | [diff] [blame] | 695 | auto h = ipmi::makeLegacyHandler(handler, context); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 696 | // translate priv from deprecated enum to current |
| 697 | ipmi::Privilege realPriv; |
| 698 | switch (priv) |
| 699 | { |
| 700 | case PRIVILEGE_CALLBACK: |
| 701 | realPriv = ipmi::Privilege::Callback; |
| 702 | break; |
| 703 | case PRIVILEGE_USER: |
| 704 | realPriv = ipmi::Privilege::User; |
| 705 | break; |
| 706 | case PRIVILEGE_OPERATOR: |
| 707 | realPriv = ipmi::Privilege::Operator; |
| 708 | break; |
| 709 | case PRIVILEGE_ADMIN: |
| 710 | realPriv = ipmi::Privilege::Admin; |
| 711 | break; |
| 712 | case PRIVILEGE_OEM: |
| 713 | realPriv = ipmi::Privilege::Oem; |
| 714 | break; |
| 715 | case SYSTEM_INTERFACE: |
| 716 | realPriv = ipmi::Privilege::Admin; |
| 717 | break; |
| 718 | default: |
| 719 | realPriv = ipmi::Privilege::Admin; |
| 720 | break; |
| 721 | } |
Vernon Mauery | e8d4323 | 2019-03-26 16:23:43 -0700 | [diff] [blame] | 722 | // The original ipmi_register_callback allowed for group OEM handlers |
| 723 | // to be registered via this same interface. It just so happened that |
| 724 | // all the handlers were part of the DCMI group, so default to that. |
| 725 | if (netFn == NETFUN_GRPEXT) |
| 726 | { |
Vernon Mauery | 82cffcc | 2023-07-27 10:59:20 -0700 | [diff] [blame] | 727 | ipmi::impl::registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 728 | cmd, realPriv, h); |
Vernon Mauery | e8d4323 | 2019-03-26 16:23:43 -0700 | [diff] [blame] | 729 | } |
| 730 | else |
| 731 | { |
| 732 | ipmi::impl::registerHandler(ipmi::prioOpenBmcBase, netFn, cmd, realPriv, |
| 733 | h); |
| 734 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 737 | namespace oem |
| 738 | { |
| 739 | |
| 740 | class LegacyRouter : public oem::Router |
| 741 | { |
| 742 | public: |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 743 | virtual ~LegacyRouter() {} |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 744 | |
| 745 | /// Enable message routing to begin. |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 746 | void activate() override {} |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 747 | |
| 748 | void registerHandler(Number oen, ipmi_cmd_t cmd, Handler handler) override |
| 749 | { |
| 750 | auto h = ipmi::makeLegacyHandler(std::forward<Handler>(handler)); |
| 751 | ipmi::impl::registerOemHandler(ipmi::prioOpenBmcBase, oen, cmd, |
| 752 | ipmi::Privilege::Admin, h); |
| 753 | } |
| 754 | }; |
| 755 | static LegacyRouter legacyRouter; |
| 756 | |
| 757 | Router* mutableRouter() |
| 758 | { |
| 759 | return &legacyRouter; |
| 760 | } |
| 761 | |
| 762 | } // namespace oem |
| 763 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 764 | /* legacy alternative to executionEntry */ |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 765 | void handleLegacyIpmiCommand(sdbusplus::message_t& m) |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 766 | { |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 767 | // make a copy so the next two moves don't wreak havoc on the stack |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 768 | sdbusplus::message_t b{m}; |
Ed Tanous | 9860505 | 2025-02-13 16:57:13 -0800 | [diff] [blame^] | 769 | boost::asio::spawn( |
Jayanth Othayoth | 531223f | 2024-11-11 07:30:15 -0600 | [diff] [blame] | 770 | *getIoContext(), |
| 771 | [b = std::move(b)](boost::asio::yield_context yield) { |
| 772 | sdbusplus::message_t m{std::move(b)}; |
| 773 | unsigned char seq = 0, netFn = 0, lun = 0, cmd = 0; |
| 774 | ipmi::SecureBuffer data; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 775 | |
Jayanth Othayoth | 531223f | 2024-11-11 07:30:15 -0600 | [diff] [blame] | 776 | m.read(seq, netFn, lun, cmd, data); |
| 777 | std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus(); |
| 778 | auto ctx = std::make_shared<ipmi::Context>( |
| 779 | bus, netFn, lun, cmd, 0, 0, 0, ipmi::Privilege::Admin, 0, 0, |
| 780 | yield); |
| 781 | auto request = std::make_shared<ipmi::message::Request>( |
| 782 | ctx, std::forward<ipmi::SecureBuffer>(data)); |
| 783 | ipmi::message::Response::ptr response = |
| 784 | ipmi::executeIpmiCommand(request); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 785 | |
Jayanth Othayoth | 531223f | 2024-11-11 07:30:15 -0600 | [diff] [blame] | 786 | // Responses in IPMI require a bit set. So there ya go... |
| 787 | netFn |= 0x01; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 788 | |
Jayanth Othayoth | 531223f | 2024-11-11 07:30:15 -0600 | [diff] [blame] | 789 | const char *dest, *path; |
| 790 | constexpr const char* DBUS_INTF = "org.openbmc.HostIpmi"; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 791 | |
Jayanth Othayoth | 531223f | 2024-11-11 07:30:15 -0600 | [diff] [blame] | 792 | dest = m.get_sender(); |
| 793 | path = m.get_path(); |
| 794 | boost::system::error_code ec; |
| 795 | bus->yield_method_call(yield, ec, dest, path, DBUS_INTF, |
| 796 | "sendMessage", seq, netFn, lun, cmd, |
| 797 | response->cc, response->payload.raw); |
| 798 | if (ec) |
| 799 | { |
| 800 | lg2::error( |
| 801 | "Failed to send response to requestor ({NETFN}/{CMD}): {ERROR}", |
| 802 | "ERROR", ec.message(), "SENDER", dest, "NETFN", lg2::hex, |
| 803 | netFn, "CMD", lg2::hex, cmd); |
| 804 | } |
| 805 | }, |
Ed Tanous | 9860505 | 2025-02-13 16:57:13 -0800 | [diff] [blame^] | 806 | boost::asio::detached); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | #endif /* ALLOW_DEPRECATED_API */ |
| 810 | |
| 811 | // Calls host command manager to do the right thing for the command |
| 812 | using CommandHandler = phosphor::host::command::CommandHandler; |
| 813 | std::unique_ptr<phosphor::host::command::Manager> cmdManager; |
| 814 | void ipmid_send_cmd_to_host(CommandHandler&& cmd) |
| 815 | { |
Vernon Mauery | 5e096a2 | 2022-06-01 15:11:16 -0700 | [diff] [blame] | 816 | cmdManager->execute(std::forward<CommandHandler>(cmd)); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | std::unique_ptr<phosphor::host::command::Manager>& ipmid_get_host_cmd_manager() |
| 820 | { |
| 821 | return cmdManager; |
| 822 | } |
| 823 | |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 824 | // These are symbols that are present in libipmid, but not expected |
| 825 | // to be used except here (or maybe a unit test), so declare them here |
| 826 | extern void setIoContext(std::shared_ptr<boost::asio::io_context>& newIo); |
| 827 | extern void setSdBus(std::shared_ptr<sdbusplus::asio::connection>& newBus); |
| 828 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 829 | int main(int argc, char* argv[]) |
| 830 | { |
| 831 | // Connect to system bus |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 832 | auto io = std::make_shared<boost::asio::io_context>(); |
| 833 | setIoContext(io); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 834 | if (argc > 1 && std::string(argv[1]) == "-session") |
| 835 | { |
| 836 | sd_bus_default_user(&bus); |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | sd_bus_default_system(&bus); |
| 841 | } |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 842 | auto sdbusp = std::make_shared<sdbusplus::asio::connection>(*io, bus); |
| 843 | setSdBus(sdbusp); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 844 | |
Patrick Williams | 3b301a3 | 2025-02-03 13:05:36 -0500 | [diff] [blame] | 845 | // TODO: Hack to keep the sdEvents running.... Not sure why the sd_event |
| 846 | // queue stops running if we don't have a timer that keeps re-arming |
| 847 | sdbusplus::Timer t2([]() { ; }); |
| 848 | t2.start(std::chrono::microseconds(500000), true); |
| 849 | |
| 850 | // TODO: Remove all vestiges of sd_event from phosphor-host-ipmid |
| 851 | // until that is done, add the sd_event wrapper to the io object |
| 852 | sdbusplus::asio::sd_event_wrapper sdEvents(*io); |
| 853 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 854 | cmdManager = std::make_unique<phosphor::host::command::Manager>(*sdbusp); |
| 855 | |
| 856 | // Register all command providers and filters |
Vernon Mauery | 4ec4e40 | 2019-03-20 13:09:27 -0700 | [diff] [blame] | 857 | std::forward_list<ipmi::IpmiProvider> providers = |
| 858 | ipmi::loadProviders(HOST_IPMI_LIB_PATH); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 859 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 860 | #ifdef ALLOW_DEPRECATED_API |
| 861 | // listen on deprecated signal interface for kcs/bt commands |
| 862 | constexpr const char* FILTER = "type='signal',interface='org.openbmc." |
| 863 | "HostIpmi',member='ReceivedMessage'"; |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 864 | sdbusplus::bus::match_t oldIpmiInterface(*sdbusp, FILTER, |
| 865 | handleLegacyIpmiCommand); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 866 | #endif /* ALLOW_DEPRECATED_API */ |
| 867 | |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 868 | // set up bus name watching to match channels with bus names |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 869 | sdbusplus::bus::match_t nameOwnerChanged( |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 870 | *sdbusp, |
| 871 | sdbusplus::bus::match::rules::nameOwnerChanged() + |
| 872 | sdbusplus::bus::match::rules::arg0namespace( |
| 873 | ipmi::ipmiDbusChannelMatch), |
| 874 | ipmi::nameChangeHandler); |
Jayanth Othayoth | 9f3073a | 2024-12-08 09:29:43 -0600 | [diff] [blame] | 875 | ipmi::doListNames(*sdbusp); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 876 | |
James Feist | b0094a7 | 2019-11-26 09:07:15 -0800 | [diff] [blame] | 877 | int exitCode = 0; |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 878 | // set up boost::asio signal handling |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 879 | std::function<SignalResponse(int)> stopAsioRunLoop = [&io, &exitCode]( |
| 880 | int signalNumber) { |
Vernon Mauery | e808bae | 2024-05-31 14:55:36 -0700 | [diff] [blame] | 881 | lg2::info("Received signal {SIGNAL}; quitting", "SIGNAL", signalNumber); |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 882 | io->stop(); |
| 883 | exitCode = signalNumber; |
| 884 | return SignalResponse::breakExecution; |
| 885 | }; |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 886 | registerSignalHandler(ipmi::prioOpenBmcBase, SIGINT, stopAsioRunLoop); |
| 887 | registerSignalHandler(ipmi::prioOpenBmcBase, SIGTERM, stopAsioRunLoop); |
| 888 | |
Richard Marian Thomaiyar | 369406e | 2020-01-09 14:56:54 +0530 | [diff] [blame] | 889 | sdbusp->request_name("xyz.openbmc_project.Ipmi.Host"); |
| 890 | // Add bindings for inbound IPMI requests |
| 891 | auto server = sdbusplus::asio::object_server(sdbusp); |
| 892 | auto iface = server.add_interface("/xyz/openbmc_project/Ipmi", |
| 893 | "xyz.openbmc_project.Ipmi.Server"); |
| 894 | iface->register_method("execute", ipmi::executionEntry); |
| 895 | iface->initialize(); |
| 896 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 897 | io->run(); |
| 898 | |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 899 | // destroy all the IPMI handlers so the providers can unload safely |
| 900 | ipmi::handlerMap.clear(); |
| 901 | ipmi::groupHandlerMap.clear(); |
| 902 | ipmi::oemHandlerMap.clear(); |
| 903 | ipmi::filterList.clear(); |
| 904 | // unload the provider libraries |
Vernon Mauery | 4ec4e40 | 2019-03-20 13:09:27 -0700 | [diff] [blame] | 905 | providers.clear(); |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 906 | |
James Feist | b0094a7 | 2019-11-26 09:07:15 -0800 | [diff] [blame] | 907 | std::exit(exitCode); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 908 | } |