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