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