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