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