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