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