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