Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 1 | #include "channel.hpp" |
Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 2 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 3 | #include "transporthandler.hpp" |
Johnathan Mantey | 74a2102 | 2018-12-13 13:17:56 -0800 | [diff] [blame] | 4 | #include "user_channel/channel_layer.hpp" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 5 | |
Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 6 | #include <arpa/inet.h> |
| 7 | |
Tom Joseph | 1322768 | 2018-08-10 01:05:21 +0530 | [diff] [blame] | 8 | #include <boost/process/child.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 9 | #include <fstream> |
Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 10 | #include <ipmid/types.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 11 | #include <ipmid/utils.hpp> |
Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 12 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 13 | #include <phosphor-logging/log.hpp> |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 14 | #include <set> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <xyz/openbmc_project/Common/error.hpp> |
Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 17 | |
| 18 | using namespace phosphor::logging; |
| 19 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 20 | |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 21 | namespace cipher |
| 22 | { |
| 23 | |
| 24 | /** @brief Get the supported Cipher records |
| 25 | * |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 26 | * The cipher records are read from the JSON file and converted into |
| 27 | * 1. cipher suite record format mentioned in the IPMI specification. The |
| 28 | * records can be either OEM or standard cipher. Each json entry is parsed and |
| 29 | * converted into the cipher record format and pushed into the vector. |
| 30 | * 2. Algorithms listed in vector format |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 31 | * |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 32 | * @return pair of vector containing 1. all the cipher suite records. 2. |
| 33 | * Algorithms supported |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 34 | * |
| 35 | */ |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 36 | std::pair<std::vector<uint8_t>, std::vector<uint8_t>> getCipherRecords() |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 37 | { |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 38 | std::vector<uint8_t> cipherRecords; |
| 39 | std::vector<uint8_t> supportedAlgorithmRecords; |
| 40 | // create set to get the unique supported algorithms |
| 41 | std::set<uint8_t> supportedAlgorithmSet; |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 42 | |
| 43 | std::ifstream jsonFile(configFile); |
| 44 | if (!jsonFile.is_open()) |
| 45 | { |
| 46 | log<level::ERR>("Channel Cipher suites file not found"); |
| 47 | elog<InternalFailure>(); |
| 48 | } |
| 49 | |
| 50 | auto data = Json::parse(jsonFile, nullptr, false); |
| 51 | if (data.is_discarded()) |
| 52 | { |
| 53 | log<level::ERR>("Parsing channel cipher suites JSON failed"); |
| 54 | elog<InternalFailure>(); |
| 55 | } |
| 56 | |
| 57 | for (const auto& record : data) |
| 58 | { |
| 59 | if (record.find(oem) != record.end()) |
| 60 | { |
| 61 | // OEM cipher suite - 0xC1 |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 62 | cipherRecords.push_back(oemCipherSuite); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 63 | // Cipher Suite ID |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 64 | cipherRecords.push_back(record.value(cipher, 0)); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 65 | // OEM IANA - 3 bytes |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 66 | cipherRecords.push_back(record.value(oem, 0)); |
| 67 | cipherRecords.push_back(record.value(oem, 0) >> 8); |
| 68 | cipherRecords.push_back(record.value(oem, 0) >> 16); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 69 | } |
| 70 | else |
| 71 | { |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 72 | // Standard cipher suite - 0xC0 |
| 73 | cipherRecords.push_back(stdCipherSuite); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 74 | // Cipher Suite ID |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 75 | cipherRecords.push_back(record.value(cipher, 0)); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Authentication algorithm number |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 79 | cipherRecords.push_back(record.value(auth, 0)); |
| 80 | supportedAlgorithmSet.insert(record.value(auth, 0)); |
| 81 | |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 82 | // Integrity algorithm number |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 83 | cipherRecords.push_back(record.value(integrity, 0) | integrityTag); |
| 84 | supportedAlgorithmSet.insert(record.value(integrity, 0) | integrityTag); |
| 85 | |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 86 | // Confidentiality algorithm number |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 87 | cipherRecords.push_back(record.value(conf, 0) | confTag); |
| 88 | supportedAlgorithmSet.insert(record.value(conf, 0) | confTag); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 89 | } |
| 90 | |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 91 | // copy the set to supportedAlgorithmRecord which is vector based. |
| 92 | std::copy(supportedAlgorithmSet.begin(), supportedAlgorithmSet.end(), |
| 93 | std::back_inserter(supportedAlgorithmRecords)); |
| 94 | |
| 95 | return std::make_pair(cipherRecords, supportedAlgorithmRecords); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 96 | } |
| 97 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 98 | } // namespace cipher |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 99 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 100 | ipmi_ret_t getChannelCipherSuites(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 101 | ipmi_request_t request, |
| 102 | ipmi_response_t response, |
| 103 | ipmi_data_len_t data_len, |
| 104 | ipmi_context_t context) |
| 105 | { |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 106 | static std::vector<uint8_t> cipherRecords; |
| 107 | static std::vector<uint8_t> supportedAlgorithms; |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 108 | static auto recordInit = false; |
| 109 | |
| 110 | auto requestData = |
| 111 | reinterpret_cast<const GetChannelCipherRequest*>(request); |
| 112 | |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 113 | if (*data_len < sizeof(GetChannelCipherRequest)) |
| 114 | { |
| 115 | *data_len = 0; |
| 116 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 117 | } |
| 118 | |
| 119 | *data_len = 0; |
| 120 | |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 121 | if (!recordInit) |
| 122 | { |
| 123 | try |
| 124 | { |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 125 | std::tie(cipherRecords, supportedAlgorithms) = |
| 126 | cipher::getCipherRecords(); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 127 | recordInit = true; |
| 128 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 129 | catch (const std::exception& e) |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 130 | { |
| 131 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 132 | } |
| 133 | } |
| 134 | |
Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 135 | const auto& records = (cipher::listCipherSuite == |
| 136 | (requestData->listIndex & cipher::listTypeMask)) |
| 137 | ? cipherRecords |
| 138 | : supportedAlgorithms; |
| 139 | |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 140 | // List index(00h-3Fh), 0h selects the first set of 16, 1h selects the next |
| 141 | // set of 16 and so on. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 142 | auto index = |
| 143 | static_cast<size_t>(requestData->listIndex & cipher::listIndexMask); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 144 | |
| 145 | // Calculate the number of record data bytes to be returned. |
| 146 | auto start = std::min(index * cipher::respSize, records.size()); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 147 | auto end = |
| 148 | std::min((index * cipher::respSize) + cipher::respSize, records.size()); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 149 | auto size = end - start; |
| 150 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 151 | auto responseData = reinterpret_cast<GetChannelCipherRespHeader*>(response); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 152 | responseData->channelNumber = cipher::defaultChannelNumber; |
| 153 | |
| 154 | if (!size) |
| 155 | { |
| 156 | *data_len = sizeof(GetChannelCipherRespHeader); |
| 157 | } |
| 158 | else |
| 159 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 160 | std::copy_n(records.data() + start, size, |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 161 | static_cast<uint8_t*>(response) + 1); |
| 162 | *data_len = size + sizeof(GetChannelCipherRespHeader); |
| 163 | } |
| 164 | |
| 165 | return IPMI_CC_OK; |
| 166 | } |
Tom Joseph | 1322768 | 2018-08-10 01:05:21 +0530 | [diff] [blame] | 167 | |
| 168 | template <typename... ArgTypes> |
| 169 | static int executeCmd(const char* path, ArgTypes&&... tArgs) |
| 170 | { |
| 171 | boost::process::child execProg(path, const_cast<char*>(tArgs)...); |
| 172 | execProg.wait(); |
| 173 | return execProg.exit_code(); |
| 174 | } |
| 175 | |
| 176 | /** @brief Enable the network IPMI service on the specified ethernet interface. |
| 177 | * |
| 178 | * @param[in] intf - ethernet interface on which to enable IPMI |
| 179 | */ |
| 180 | void enableNetworkIPMI(const std::string& intf) |
| 181 | { |
| 182 | // Check if there is a iptable filter to drop IPMI packets for the |
| 183 | // interface. |
| 184 | auto retCode = |
| 185 | executeCmd("/usr/sbin/iptables", "-C", "INPUT", "-p", "udp", "-i", |
| 186 | intf.c_str(), "--dport", "623", "-j", "DROP"); |
| 187 | |
| 188 | // If the iptable filter exists, delete the filter. |
| 189 | if (!retCode) |
| 190 | { |
| 191 | auto response = |
| 192 | executeCmd("/usr/sbin/iptables", "-D", "INPUT", "-p", "udp", "-i", |
| 193 | intf.c_str(), "--dport", "623", "-j", "DROP"); |
| 194 | |
| 195 | if (response) |
| 196 | { |
| 197 | log<level::ERR>("Dropping the iptables filter failed", |
| 198 | entry("INTF=%s", intf.c_str()), |
| 199 | entry("RETURN_CODE:%d", response)); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** @brief Disable the network IPMI service on the specified ethernet interface. |
| 205 | * |
| 206 | * @param[in] intf - ethernet interface on which to disable IPMI |
| 207 | */ |
| 208 | void disableNetworkIPMI(const std::string& intf) |
| 209 | { |
| 210 | // Check if there is a iptable filter to drop IPMI packets for the |
| 211 | // interface. |
| 212 | auto retCode = |
| 213 | executeCmd("/usr/sbin/iptables", "-C", "INPUT", "-p", "udp", "-i", |
| 214 | intf.c_str(), "--dport", "623", "-j", "DROP"); |
| 215 | |
| 216 | // If the iptable filter does not exist, add filter to drop network IPMI |
| 217 | // packets |
| 218 | if (retCode) |
| 219 | { |
| 220 | auto response = |
| 221 | executeCmd("/usr/sbin/iptables", "-I", "INPUT", "-p", "udp", "-i", |
| 222 | intf.c_str(), "--dport", "623", "-j", "DROP"); |
| 223 | |
| 224 | if (response) |
| 225 | { |
| 226 | log<level::ERR>("Inserting iptables filter failed", |
| 227 | entry("INTF=%s", intf.c_str()), |
| 228 | entry("RETURN_CODE:%d", response)); |
| 229 | } |
| 230 | } |
| 231 | } |