Patrick Venture | 15e95f2 | 2018-11-14 19:25:08 -0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 3 | #include "ipmid.hpp" |
| 4 | |
Patrick Venture | 46470a3 | 2018-09-07 19:26:25 -0700 | [diff] [blame] | 5 | #include "host-cmd-manager.hpp" |
| 6 | #include "ipmiwhitelist.hpp" |
| 7 | #include "sensorhandler.hpp" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 8 | #include "settings.hpp" |
| 9 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 10 | #include <assert.h> |
| 11 | #include <dirent.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 12 | #include <dlfcn.h> |
| 13 | #include <errno.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 14 | #include <sys/time.h> |
| 15 | #include <systemd/sd-bus.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | #include <algorithm> |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 19 | #include <cstring> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 20 | #include <host-ipmid/ipmid-host-cmd.hpp> |
Patrick Venture | 46470a3 | 2018-09-07 19:26:25 -0700 | [diff] [blame] | 21 | #include <host-ipmid/oemrouter.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 22 | #include <iostream> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 23 | #include <iterator> |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 24 | #include <map> |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 25 | #include <memory> |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 27 | #include <sdbusplus/bus.hpp> |
| 28 | #include <sdbusplus/bus/match.hpp> |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 29 | #include <sdbusplus/message/types.hpp> |
Vernon Mauery | 1181af7 | 2018-10-08 12:05:00 -0700 | [diff] [blame] | 30 | #include <sdbusplus/timer.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | #include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp> |
| 33 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 34 | using namespace phosphor::logging; |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 35 | namespace sdbusRule = sdbusplus::bus::match::rules; |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 36 | namespace variant_ns = sdbusplus::message::variant_ns; |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 37 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 38 | sd_bus* bus = NULL; |
| 39 | sd_bus_slot* ipmid_slot = NULL; |
| 40 | sd_event* events = nullptr; |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 41 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 42 | // Need this to use new sdbusplus compatible interfaces |
| 43 | sdbusPtr sdbusp; |
| 44 | |
| 45 | // Global Host Bound Command manager |
| 46 | using cmdManagerPtr = std::unique_ptr<phosphor::host::command::Manager>; |
| 47 | cmdManagerPtr cmdManager; |
| 48 | |
Ratan Gupta | 7a7f012 | 2018-03-07 12:31:05 +0530 | [diff] [blame] | 49 | // Global timer for network changes |
Vernon Mauery | 1181af7 | 2018-10-08 12:05:00 -0700 | [diff] [blame] | 50 | std::unique_ptr<phosphor::Timer> networkTimer = nullptr; |
Ratan Gupta | 7a7f012 | 2018-03-07 12:31:05 +0530 | [diff] [blame] | 51 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 52 | // Command and handler tuple. Used when clients ask the command to be put |
| 53 | // into host message queue |
| 54 | using CommandHandler = phosphor::host::command::CommandHandler; |
| 55 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 56 | // Initialise restricted mode to true |
| 57 | bool restricted_mode = true; |
| 58 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 59 | FILE *ipmiio, *ipmidbus, *ipmicmddetails; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 60 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 61 | void print_usage(void) |
| 62 | { |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 63 | std::fprintf(stderr, "Options: [-d mask]\n"); |
| 64 | std::fprintf(stderr, " mask : 0x01 - Print ipmi packets\n"); |
| 65 | std::fprintf(stderr, " mask : 0x02 - Print DBUS operations\n"); |
| 66 | std::fprintf(stderr, " mask : 0x04 - Print ipmi command details\n"); |
| 67 | std::fprintf(stderr, " mask : 0xFF - Print all trace\n"); |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 70 | const char* DBUS_INTF = "org.openbmc.HostIpmi"; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 71 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 72 | const char* FILTER = |
| 73 | "type='signal',interface='org.openbmc.HostIpmi',member='ReceivedMessage'"; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 74 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 75 | typedef std::pair<ipmi_netfn_t, ipmi_cmd_t> ipmi_fn_cmd_t; |
| 76 | typedef std::pair<ipmid_callback_t, ipmi_context_t> ipmi_fn_context_t; |
| 77 | |
| 78 | // Global data structure that contains the IPMI command handler's registrations. |
| 79 | std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map; |
| 80 | |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 81 | // IPMI Spec, shared Reservation ID. |
Jason M. Bills | 13e67c8 | 2018-09-10 14:12:16 -0700 | [diff] [blame] | 82 | static unsigned short selReservationID = 0xFFFF; |
| 83 | static bool selReservationValid = false; |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 84 | |
Jason M. Bills | 13e67c8 | 2018-09-10 14:12:16 -0700 | [diff] [blame] | 85 | unsigned short reserveSel(void) |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 86 | { |
Jason M. Bills | 13e67c8 | 2018-09-10 14:12:16 -0700 | [diff] [blame] | 87 | // IPMI spec, Reservation ID, the value simply increases against each |
| 88 | // execution of the Reserve SEL command. |
| 89 | if (++selReservationID == 0) |
| 90 | { |
| 91 | selReservationID = 1; |
| 92 | } |
| 93 | selReservationValid = true; |
| 94 | return selReservationID; |
| 95 | } |
| 96 | |
| 97 | bool checkSELReservation(unsigned short id) |
| 98 | { |
| 99 | return (selReservationValid && selReservationID == id); |
| 100 | } |
| 101 | |
| 102 | void cancelSELReservation(void) |
| 103 | { |
| 104 | selReservationValid = false; |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 105 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 106 | |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 107 | namespace internal |
| 108 | { |
| 109 | |
| 110 | constexpr auto restrictionModeIntf = |
| 111 | "xyz.openbmc_project.Control.Security.RestrictionMode"; |
| 112 | |
| 113 | namespace cache |
| 114 | { |
| 115 | |
| 116 | std::unique_ptr<settings::Objects> objects = nullptr; |
| 117 | |
| 118 | } // namespace cache |
| 119 | } // namespace internal |
| 120 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 121 | #ifndef HEXDUMP_COLS |
| 122 | #define HEXDUMP_COLS 16 |
| 123 | #endif |
| 124 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 125 | void hexdump(FILE* s, void* mem, size_t len) |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 126 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 127 | unsigned int i, j; |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 128 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 129 | for (i = 0; |
| 130 | i < |
| 131 | len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); |
| 132 | i++) |
| 133 | { |
| 134 | /* print offset */ |
| 135 | if (i % HEXDUMP_COLS == 0) |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 136 | { |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 137 | std::fprintf(s, "0x%06x: ", i); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 138 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 139 | |
| 140 | /* print hex data */ |
| 141 | if (i < len) |
| 142 | { |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 143 | std::fprintf(s, "%02x ", 0xFF & ((char*)mem)[i]); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 144 | } |
| 145 | else /* end of block, just aligning for ASCII dump */ |
| 146 | { |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 147 | std::fprintf(s, " "); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* print ASCII dump */ |
| 151 | if (i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) |
| 152 | { |
| 153 | for (j = i - (HEXDUMP_COLS - 1); j <= i; j++) |
| 154 | { |
| 155 | if (j >= len) /* end of block, not really printing */ |
| 156 | { |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 157 | std::fputc(' ', s); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 158 | } |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 159 | else if (std::isprint(((char*)mem)[j])) /* printable char */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 160 | { |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 161 | std::fputc(0xFF & ((char*)mem)[j], s); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 162 | } |
| 163 | else /* other char */ |
| 164 | { |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 165 | std::fputc('.', s); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 168 | std::fputc('\n', s); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 173 | // Method that gets called by shared libraries to get their command handlers |
| 174 | // registered |
| 175 | void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 176 | ipmi_context_t context, ipmid_callback_t handler, |
| 177 | ipmi_cmd_privilege_t priv) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 178 | { |
| 179 | // Pack NetFn and Command in one. |
| 180 | auto netfn_and_cmd = std::make_pair(netfn, cmd); |
| 181 | |
| 182 | // Pack Function handler and Data in another. |
| 183 | auto handler_and_context = std::make_pair(handler, context); |
| 184 | |
| 185 | // Check if the registration has already been made.. |
| 186 | auto iter = g_ipmid_router_map.find(netfn_and_cmd); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 187 | if (iter != g_ipmid_router_map.end()) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 188 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 189 | log<level::ERR>("Duplicate registration", entry("NETFN=0x%X", netfn), |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 190 | entry("CMD=0x%X", cmd)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 191 | } |
| 192 | else |
| 193 | { |
| 194 | // This is a fresh registration.. Add it to the map. |
| 195 | g_ipmid_router_map.emplace(netfn_and_cmd, handler_and_context); |
| 196 | } |
| 197 | |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | // Looks at the map and calls corresponding handler functions. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 202 | ipmi_ret_t ipmi_netfn_router(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 203 | ipmi_request_t request, ipmi_response_t response, |
| 204 | ipmi_data_len_t data_len) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 205 | { |
| 206 | // return from the Command handlers. |
| 207 | ipmi_ret_t rc = IPMI_CC_INVALID; |
| 208 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 209 | // If restricted mode is true and command is not whitelisted, don't |
| 210 | // execute the command |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 211 | if (restricted_mode) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 212 | { |
| 213 | if (!std::binary_search(whitelist.cbegin(), whitelist.cend(), |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 214 | std::make_pair(netfn, cmd))) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 215 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 216 | log<level::ERR>("Net function not whitelisted", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 217 | entry("NETFN=0x%X", netfn), entry("CMD=0x%X", cmd)); |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 218 | rc = IPMI_CC_INSUFFICIENT_PRIVILEGE; |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 219 | std::memcpy(response, &rc, IPMI_CC_LEN); |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 220 | *data_len = IPMI_CC_LEN; |
| 221 | return rc; |
| 222 | } |
| 223 | } |
| 224 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 225 | // Walk the map that has the registered handlers and invoke the approprite |
| 226 | // handlers for matching commands. |
| 227 | auto iter = g_ipmid_router_map.find(std::make_pair(netfn, cmd)); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 228 | if (iter == g_ipmid_router_map.end()) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 229 | { |
Patrick Venture | 03f84ba | 2017-09-20 09:15:33 -0700 | [diff] [blame] | 230 | /* By default should only print on failure to find wildcard command. */ |
| 231 | #ifdef __IPMI_DEBUG__ |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 232 | log<level::ERR>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 233 | "No registered handlers for NetFn, trying Wilcard implementation", |
| 234 | entry("NET_FUN=0x%X", netfn) entry("CMD=0x%X", IPMI_CMD_WILDCARD)); |
Patrick Venture | 03f84ba | 2017-09-20 09:15:33 -0700 | [diff] [blame] | 235 | #endif |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 236 | |
| 237 | // Now that we did not find any specific [NetFn,Cmd], tuple, check for |
| 238 | // NetFn, WildCard command present. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 239 | iter = |
| 240 | g_ipmid_router_map.find(std::make_pair(netfn, IPMI_CMD_WILDCARD)); |
| 241 | if (iter == g_ipmid_router_map.end()) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 242 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 243 | log<level::ERR>("No Registered handlers for NetFn", |
| 244 | entry("NET_FUN=0x%X", netfn), |
| 245 | entry("CMD=0x%X", IPMI_CMD_WILDCARD)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 246 | |
| 247 | // Respond with a 0xC1 |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 248 | std::memcpy(response, &rc, IPMI_CC_LEN); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 249 | *data_len = IPMI_CC_LEN; |
| 250 | return rc; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | #ifdef __IPMI_DEBUG__ |
| 255 | // We have either a perfect match -OR- a wild card atleast, |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 256 | log<level::ERR>("Calling Net function", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 257 | entry("NET_FUN=0x%X", netfn) entry("CMD=0x%X", cmd)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 258 | #endif |
| 259 | |
| 260 | // Extract the map data onto appropriate containers |
| 261 | auto handler_and_context = iter->second; |
| 262 | |
| 263 | // Creating a pointer type casted to char* to make sure we advance 1 byte |
| 264 | // when we advance pointer to next's address. advancing void * would not |
| 265 | // make sense. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 266 | char* respo = &((char*)response)[IPMI_CC_LEN]; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 267 | |
Richard Marian Thomaiyar | ebc886f | 2018-06-06 14:33:59 +0530 | [diff] [blame] | 268 | try |
| 269 | { |
| 270 | // Response message from the plugin goes into a byte post the base |
| 271 | // response |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 272 | rc = (handler_and_context.first)(netfn, cmd, request, respo, data_len, |
| 273 | handler_and_context.second); |
Richard Marian Thomaiyar | ebc886f | 2018-06-06 14:33:59 +0530 | [diff] [blame] | 274 | } |
| 275 | // IPMI command handlers can throw unhandled exceptions, catch those |
| 276 | // and return sane error code. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 277 | catch (const std::exception& e) |
Richard Marian Thomaiyar | ebc886f | 2018-06-06 14:33:59 +0530 | [diff] [blame] | 278 | { |
| 279 | log<level::ERR>(e.what(), entry("NET_FUN=0x%X", netfn), |
| 280 | entry("CMD=0x%X", cmd)); |
| 281 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 282 | *data_len = 0; |
| 283 | // fall through |
| 284 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 285 | // Now copy the return code that we got from handler and pack it in first |
| 286 | // byte. |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 287 | std::memcpy(response, &rc, IPMI_CC_LEN); |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 288 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 289 | // Data length is now actual data + completion code. |
| 290 | *data_len = *data_len + IPMI_CC_LEN; |
| 291 | |
| 292 | return rc; |
| 293 | } |
| 294 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 295 | static int send_ipmi_message(sd_bus_message* req, unsigned char seq, |
| 296 | unsigned char netfn, unsigned char lun, |
| 297 | unsigned char cmd, unsigned char cc, |
| 298 | unsigned char* buf, unsigned char len) |
| 299 | { |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 300 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 301 | sd_bus_error error = SD_BUS_ERROR_NULL; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 302 | sd_bus_message *reply = NULL, *m = NULL; |
Jeremy Kerr | e41081f | 2015-10-27 12:11:36 +0800 | [diff] [blame] | 303 | const char *dest, *path; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 304 | int r, pty; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 305 | |
Jeremy Kerr | e41081f | 2015-10-27 12:11:36 +0800 | [diff] [blame] | 306 | dest = sd_bus_message_get_sender(req); |
| 307 | path = sd_bus_message_get_path(req); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 308 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 309 | r = sd_bus_message_new_method_call(bus, &m, dest, path, DBUS_INTF, |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 310 | "sendMessage"); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 311 | if (r < 0) |
| 312 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 313 | log<level::ERR>("Failed to add the method object", |
| 314 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 315 | return -1; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 316 | } |
| 317 | |
Chris Austen | abfb5e8 | 2015-10-13 12:29:24 -0500 | [diff] [blame] | 318 | // Responses in IPMI require a bit set. So there ya go... |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 319 | netfn |= 0x01; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 320 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 321 | // Add the bytes needed for the methods to be called |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 322 | r = sd_bus_message_append(m, "yyyyy", seq, netfn, lun, cmd, cc); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 323 | if (r < 0) |
| 324 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 325 | log<level::ERR>("Failed add the netfn and others", |
| 326 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 327 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 328 | } |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 329 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 330 | r = sd_bus_message_append_array(m, 'y', buf, len); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 331 | if (r < 0) |
| 332 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 333 | log<level::ERR>("Failed to add the string of response bytes", |
| 334 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 335 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 338 | // Call the IPMI responder on the bus so the message can be sent to the CEC |
| 339 | r = sd_bus_call(bus, m, 0, &error, &reply); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 340 | if (r < 0) |
| 341 | { |
| 342 | log<level::ERR>("Failed to call the method", entry("DEST=%s", dest), |
| 343 | entry("PATH=%s", path), entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 344 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | r = sd_bus_message_read(reply, "x", &pty); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 348 | if (r < 0) |
| 349 | { |
| 350 | log<level::ERR>("Failed to get a reply from the method", |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 351 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 354 | final: |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 355 | sd_bus_error_free(&error); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 356 | m = sd_bus_message_unref(m); |
| 357 | reply = sd_bus_message_unref(reply); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 358 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 359 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 360 | } |
| 361 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 362 | void cache_restricted_mode() |
| 363 | { |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 364 | restricted_mode = false; |
| 365 | using namespace sdbusplus::xyz::openbmc_project::Control::Security::server; |
| 366 | using namespace internal; |
| 367 | using namespace internal::cache; |
| 368 | sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection()); |
| 369 | const auto& restrictionModeSetting = |
Deepak Kodihalli | e602709 | 2017-08-27 08:13:37 -0500 | [diff] [blame] | 370 | objects->map.at(restrictionModeIntf).front(); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 371 | auto method = dbus.new_method_call( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 372 | objects->service(restrictionModeSetting, restrictionModeIntf).c_str(), |
| 373 | restrictionModeSetting.c_str(), "org.freedesktop.DBus.Properties", |
| 374 | "Get"); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 375 | method.append(restrictionModeIntf, "RestrictionMode"); |
| 376 | auto resp = dbus.call(method); |
| 377 | if (resp.is_method_error()) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 378 | { |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 379 | log<level::ERR>("Error in RestrictionMode Get"); |
| 380 | // Fail-safe to true. |
| 381 | restricted_mode = true; |
| 382 | return; |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 383 | } |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 384 | sdbusplus::message::variant<std::string> result; |
| 385 | resp.read(result); |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 386 | auto restrictionMode = RestrictionMode::convertModesFromString( |
| 387 | variant_ns::get<std::string>(result)); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 388 | if (RestrictionMode::Modes::Whitelist == restrictionMode) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 389 | { |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 390 | restricted_mode = true; |
| 391 | } |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 394 | static int handle_restricted_mode_change(sd_bus_message* m, void* user_data, |
| 395 | sd_bus_error* ret_error) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 396 | { |
| 397 | cache_restricted_mode(); |
| 398 | return 0; |
| 399 | } |
| 400 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 401 | static int handle_ipmi_command(sd_bus_message* m, void* user_data, |
| 402 | sd_bus_error* ret_error) |
| 403 | { |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 404 | int r = 0; |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 405 | unsigned char sequence, netfn, lun, cmd; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 406 | const void* request; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 407 | size_t sz; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 408 | size_t resplen = MAX_IPMI_BUFFER; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 409 | unsigned char response[MAX_IPMI_BUFFER]; |
| 410 | |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 411 | std::memset(response, 0, MAX_IPMI_BUFFER); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 412 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 413 | r = sd_bus_message_read(m, "yyyy", &sequence, &netfn, &lun, &cmd); |
| 414 | if (r < 0) |
| 415 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 416 | log<level::ERR>("Failed to parse signal message", |
| 417 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 418 | return -1; |
| 419 | } |
| 420 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 421 | r = sd_bus_message_read_array(m, 'y', &request, &sz); |
| 422 | if (r < 0) |
| 423 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 424 | log<level::ERR>("Failed to parse signal message", |
| 425 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 426 | return -1; |
| 427 | } |
| 428 | |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 429 | std::fprintf(ipmiio, |
| 430 | "IPMI Incoming: Seq 0x%02x, NetFn 0x%02x, CMD: 0x%02x \n", |
| 431 | sequence, netfn, cmd); |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 432 | hexdump(ipmiio, (void*)request, sz); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 433 | |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 434 | // Allow the length field to be used for both input and output of the |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 435 | // ipmi call |
| 436 | resplen = sz; |
| 437 | |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 438 | // Now that we have parsed the entire byte array from the caller |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 439 | // we can call the ipmi router to do the work... |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 440 | r = ipmi_netfn_router(netfn, cmd, (void*)request, (void*)response, |
| 441 | &resplen); |
| 442 | if (r != 0) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 443 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 444 | #ifdef __IPMI_DEBUG__ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 445 | log<level::ERR>("ERROR in handling NetFn", entry("ERRNO=0x%X", -r), |
| 446 | entry("NET_FUN=0x%X", netfn), entry("CMD=0x%X", cmd)); |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 447 | #endif |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 448 | resplen = 0; |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | resplen = resplen - 1; // first byte is for return code. |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 453 | } |
| 454 | |
Patrick Venture | b51bf9c | 2018-09-10 15:53:14 -0700 | [diff] [blame] | 455 | std::fprintf(ipmiio, "IPMI Response:\n"); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 456 | hexdump(ipmiio, (void*)response, resplen); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 457 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 458 | // Send the response buffer from the ipmi command |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 459 | r = send_ipmi_message(m, sequence, netfn, lun, cmd, response[0], |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 460 | ((unsigned char*)response) + 1, resplen); |
| 461 | if (r < 0) |
| 462 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 463 | log<level::ERR>("Failed to send the response message"); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 464 | return -1; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 465 | } |
| 466 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 467 | return 0; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | //---------------------------------------------------------------------- |
| 471 | // handler_select |
| 472 | // Select all the files ending with with .so. in the given diretcory |
| 473 | // @d: dirent structure containing the file name |
| 474 | //---------------------------------------------------------------------- |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 475 | int handler_select(const struct dirent* entry) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 476 | { |
| 477 | // To hold ".so" from entry->d_name; |
| 478 | char dname_copy[4] = {0}; |
| 479 | |
| 480 | // We want to avoid checking for everything and isolate to the ones having |
Adriana Kobylak | 87e080b | 2016-07-10 13:16:53 -0500 | [diff] [blame] | 481 | // .so.* or .so in them. |
| 482 | // Check for versioned libraries .so.* |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 483 | if (strstr(entry->d_name, IPMI_PLUGIN_SONAME_EXTN)) |
Adriana Kobylak | 87e080b | 2016-07-10 13:16:53 -0500 | [diff] [blame] | 484 | { |
| 485 | return 1; |
| 486 | } |
| 487 | // Check for non versioned libraries .so |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 488 | else if (strstr(entry->d_name, IPMI_PLUGIN_EXTN)) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 489 | { |
| 490 | // It is possible that .so could be anywhere in the string but unlikely |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 491 | // But being careful here. Get the base address of the string, move |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 492 | // until end and come back 3 steps and that gets what we need. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 493 | strcpy(dname_copy, (entry->d_name + strlen(entry->d_name) - |
| 494 | strlen(IPMI_PLUGIN_EXTN))); |
| 495 | if (strcmp(dname_copy, IPMI_PLUGIN_EXTN) == 0) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 496 | { |
| 497 | return 1; |
| 498 | } |
| 499 | } |
| 500 | return 0; |
| 501 | } |
| 502 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 503 | // This will do a dlopen of every .so in ipmi_lib_path and will dlopen |
| 504 | // everything so that they will register a callback handler |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 505 | void ipmi_register_callback_handlers(const char* ipmi_lib_path) |
| 506 | { |
| 507 | // For walking the ipmi_lib_path |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 508 | struct dirent** handler_list; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 509 | |
| 510 | // This is used to check and abort if someone tries to register a bad one. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 511 | void* lib_handler = NULL; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 512 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 513 | if (ipmi_lib_path == NULL) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 514 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 515 | log<level::ERR>("No handlers to be registered for ipmi.. Aborting"); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 516 | assert(0); |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | // 1: Open ipmi_lib_path. Its usually "/usr/lib/phosphor-host-ipmid" |
| 521 | // 2: Scan the directory for the files that end with .so |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 522 | // 3: For each one of them, just do a 'dlopen' so that they register |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 523 | // the handlers for callback routines. |
| 524 | |
| 525 | std::string handler_fqdn = ipmi_lib_path; |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 526 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 527 | // Append a "/" since we need to add the name of the .so. If there is |
| 528 | // already a .so, adding one more is not any harm. |
| 529 | handler_fqdn += "/"; |
| 530 | |
Patrick Venture | 4491a46 | 2018-10-13 13:00:42 -0700 | [diff] [blame] | 531 | int num_handlers = |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 532 | scandir(ipmi_lib_path, &handler_list, handler_select, alphasort); |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 533 | if (num_handlers < 0) |
| 534 | return; |
Jeremy Kerr | 5e8f85e | 2015-10-27 13:43:54 +0800 | [diff] [blame] | 535 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 536 | while (num_handlers--) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 537 | { |
Chris Austen | 5403026 | 2015-10-13 12:30:46 -0500 | [diff] [blame] | 538 | handler_fqdn = ipmi_lib_path; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 539 | handler_fqdn += handler_list[num_handlers]->d_name; |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 540 | #ifdef __IPMI_DEBUG__ |
| 541 | log<level::DEBUG>("Registering handler", |
| 542 | entry("HANDLER=%s", handler_fqdn.c_str())); |
| 543 | #endif |
Chris Austen | 5403026 | 2015-10-13 12:30:46 -0500 | [diff] [blame] | 544 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 545 | lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW); |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 546 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 547 | if (lib_handler == NULL) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 548 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 549 | log<level::ERR>("ERROR opening", |
| 550 | entry("HANDLER=%s", handler_fqdn.c_str()), |
| 551 | entry("ERROR=%s", dlerror())); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 552 | } |
| 553 | // Wipe the memory allocated for this particular entry. |
| 554 | free(handler_list[num_handlers]); |
| 555 | } |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 556 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 557 | // Done with all registration. |
| 558 | free(handler_list); |
| 559 | } |
| 560 | |
| 561 | // TODO : What to be done on the memory that is given by dlopen ?. |
| 562 | return; |
| 563 | } |
| 564 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 565 | sd_bus* ipmid_get_sd_bus_connection(void) |
| 566 | { |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 567 | return bus; |
| 568 | } |
| 569 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 570 | sd_event* ipmid_get_sd_event_connection(void) |
| 571 | { |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 572 | return events; |
| 573 | } |
| 574 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 575 | sd_bus_slot* ipmid_get_sd_bus_slot(void) |
| 576 | { |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 577 | return ipmid_slot; |
| 578 | } |
| 579 | |
ssekar | b849110 | 2018-08-16 17:31:43 +0530 | [diff] [blame] | 580 | EInterfaceIndex getInterfaceIndex(void) |
| 581 | { |
| 582 | return interfaceKCS; |
| 583 | } |
| 584 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 585 | // Calls host command manager to do the right thing for the command |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 586 | void ipmid_send_cmd_to_host(CommandHandler&& cmd) |
| 587 | { |
| 588 | return cmdManager->execute(std::move(cmd)); |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 589 | } |
| 590 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 591 | cmdManagerPtr& ipmid_get_host_cmd_manager() |
| 592 | { |
| 593 | return cmdManager; |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 594 | } |
| 595 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 596 | sdbusPtr& ipmid_get_sdbus_plus_handler() |
| 597 | { |
| 598 | return sdbusp; |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 599 | } |
| 600 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 601 | int main(int argc, char* argv[]) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 602 | { |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 603 | int r; |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 604 | unsigned long tvalue; |
| 605 | int c; |
| 606 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 607 | // This file and subsequient switch is for turning on levels |
| 608 | // of trace |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 609 | ipmicmddetails = ipmiio = ipmidbus = fopen("/dev/null", "w"); |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 610 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 611 | while ((c = getopt(argc, argv, "h:d:")) != -1) |
| 612 | switch (c) |
| 613 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 614 | case 'd': |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 615 | tvalue = strtoul(optarg, NULL, 16); |
| 616 | if (1 & tvalue) |
| 617 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 618 | ipmiio = stdout; |
| 619 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 620 | if (2 & tvalue) |
| 621 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 622 | ipmidbus = stdout; |
| 623 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 624 | if (4 & tvalue) |
| 625 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 626 | ipmicmddetails = stdout; |
| 627 | } |
| 628 | break; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 629 | case 'h': |
| 630 | case '?': |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 631 | print_usage(); |
| 632 | return 1; |
| 633 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 634 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 635 | /* Connect to system bus */ |
Vernon Mauery | 46590a3 | 2018-11-08 15:29:56 -0800 | [diff] [blame] | 636 | r = sd_bus_default_system(&bus); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 637 | if (r < 0) |
| 638 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 639 | log<level::ERR>("Failed to connect to system bus", |
| 640 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 641 | goto finish; |
| 642 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 643 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 644 | /* Get an sd event handler */ |
| 645 | r = sd_event_default(&events); |
| 646 | if (r < 0) |
| 647 | { |
| 648 | log<level::ERR>("Failure to create sd_event handler", |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 649 | entry("ERRNO=0x%X", -r)); |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 650 | goto finish; |
| 651 | } |
| 652 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 653 | // Now create the Host Bound Command manager. Need sdbusplus |
| 654 | // to use the generated bindings |
| 655 | sdbusp = std::make_unique<sdbusplus::bus::bus>(bus); |
James Feist | 331f5d5 | 2018-11-01 11:03:40 -0700 | [diff] [blame] | 656 | sdbusp->request_name("xyz.openbmc_project.Ipmi.Host"); |
| 657 | |
Vernon Mauery | 316f23d | 2018-10-29 13:34:26 -0700 | [diff] [blame] | 658 | cmdManager = std::make_unique<phosphor::host::command::Manager>(*sdbusp); |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 659 | |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 660 | // Activate OemRouter. |
| 661 | oem::mutableRouter()->activate(); |
| 662 | |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 663 | // Register all the handlers that provider implementation to IPMI commands. |
| 664 | ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH); |
| 665 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 666 | // Watch for BT messages |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 667 | r = sd_bus_add_match(bus, &ipmid_slot, FILTER, handle_ipmi_command, NULL); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 668 | if (r < 0) |
| 669 | { |
| 670 | log<level::ERR>("Failed: sd_bus_add_match", entry("FILTER=%s", FILTER), |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 671 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 672 | goto finish; |
| 673 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 674 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 675 | // Attach the bus to sd_event to service user requests |
| 676 | sd_bus_attach_event(bus, events, SD_EVENT_PRIORITY_NORMAL); |
| 677 | |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 678 | { |
| 679 | using namespace internal; |
| 680 | using namespace internal::cache; |
| 681 | sdbusplus::bus::bus dbus{bus}; |
| 682 | objects = std::make_unique<settings::Objects>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 683 | dbus, std::vector<settings::Interface>({restrictionModeIntf})); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 684 | // Initialize restricted mode |
| 685 | cache_restricted_mode(); |
| 686 | // Wait for changes on Restricted mode |
| 687 | sdbusplus::bus::match_t restrictedModeMatch( |
| 688 | dbus, |
| 689 | sdbusRule::propertiesChanged( |
Deepak Kodihalli | e602709 | 2017-08-27 08:13:37 -0500 | [diff] [blame] | 690 | objects->map.at(restrictionModeIntf).front(), |
| 691 | restrictionModeIntf), |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 692 | handle_restricted_mode_change); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 693 | |
Andrew Jeffery | b975c40 | 2018-11-23 15:28:58 +1100 | [diff] [blame] | 694 | r = sd_event_loop(events); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | finish: |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 698 | sd_event_unref(events); |
| 699 | sd_bus_detach_event(bus); |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 700 | sd_bus_slot_unref(ipmid_slot); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 701 | sd_bus_unref(bus); |
| 702 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 703 | } |