google-ipmi-sys: initial commit: OEM IPMI handler

This implements a set of OEM IPMI commands built as sub-commands under
one OEM IPMI command registered in the Google OEM Namespace.

Change-Id: I65ff6f35838b501ac6ac67f7765bbc474b808660
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/cable.cpp b/cable.cpp
new file mode 100644
index 0000000..025b228
--- /dev/null
+++ b/cable.cpp
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "cable.hpp"
+
+#include "main.hpp"
+
+#include <cstdint>
+#include <experimental/filesystem>
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <system_error>
+
+namespace google
+{
+namespace ipmi
+{
+namespace fs = std::experimental::filesystem;
+
+struct CableRequest
+{
+    uint8_t subcommand;
+    uint8_t if_name_len;
+    uint8_t if_name[0];
+} __attribute__((packed));
+
+struct CableReply
+{
+    uint8_t subcommand;
+    uint8_t value;
+} __attribute__((packed));
+
+ipmi_ret_t CableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen)
+{
+    // There is an IPMI LAN channel statistics command which could be used for
+    // this type of check, however, we're not able to wait for the OpenBMC
+    // implementation to stabilize related to the network management.
+    //
+    // There is a link status file, but it is "unknown" to start with...
+    // The path we're checking: /sys/class/net/eth1/statistics/rx_packets
+
+    // This command is expecting: [0x00][len][if_name]
+    if ((*dataLen) < sizeof(struct CableRequest) + sizeof(uint8_t))
+    {
+        fprintf(stderr, "Invalid command length: %lu\n", (*dataLen));
+        return IPMI_CC_INVALID;
+    }
+
+    const auto request =
+        reinterpret_cast<const struct CableRequest*>(&reqBuf[0]);
+
+    // Sanity check the object contents.
+    if (request->if_name_len == 0)
+    {
+        fprintf(stderr, "Invalid string length: %d\n", request->if_name_len);
+        return IPMI_CC_INVALID;
+    }
+
+    // Verify the request buffer contains the object and the string.
+    if ((*dataLen) < (sizeof(struct CableRequest) + request->if_name_len))
+    {
+        fprintf(stderr, "*dataLen too small: %lu\n", (*dataLen));
+        return IPMI_CC_INVALID;
+    }
+
+    // Maximum length one can specify, plus null terminator.
+    char nameBuf[256] = {};
+    std::ostringstream opath;
+
+    // Copy the string out of the request buffer.
+    memcpy(&nameBuf[0], request->if_name, request->if_name_len);
+    std::string name = nameBuf;
+
+    // Minor sanity & security check (of course, I'm less certain if unicode
+    // comes into play here.
+    //
+    // Basically you can't easily inject ../ or /../ into the path below.
+    if (name.find("/") != std::string::npos)
+    {
+        fprintf(stderr, "Invalid or illegal name: '%s'\n", nameBuf);
+        return IPMI_CC_INVALID;
+    }
+
+    opath << "/sys/class/net/" << name << "/statistics/rx_packets";
+    std::string path = opath.str();
+
+    std::error_code ec;
+    if (!fs::exists(path, ec))
+    {
+        fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
+        return IPMI_CC_INVALID;
+    }
+    // We're uninterested in the state of ec.
+
+    // Read the file and check the result.
+    int64_t count = 0;
+    std::ifstream ifs;
+    ifs.exceptions(std::ifstream::failbit);
+    try
+    {
+        ifs.open(path);
+        ifs >> count;
+    }
+    catch (std::ios_base::failure& fail)
+    {
+        return IPMI_CC_INVALID;
+    }
+
+    struct CableReply reply;
+    reply.subcommand = SysCableCheck;
+    reply.value = 0x00; // Default false
+
+    // If we have received packets then there is a cable present.
+    reply.value = (count > 0) ? 1 : 0;
+
+    // Return the subcommand and the result.
+    memcpy(&replyBuf[0], &reply, sizeof(struct CableReply));
+    (*dataLen) = sizeof(struct CableReply);
+
+    return IPMI_CC_OK;
+}
+
+} // namespace ipmi
+} // namespace google