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