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