1 /* 2 * Copyright 2018 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "cable.hpp" 18 19 #include "main.hpp" 20 21 #include <cstdint> 22 #include <experimental/filesystem> 23 #include <fstream> 24 #include <sstream> 25 #include <string> 26 #include <system_error> 27 28 namespace google 29 { 30 namespace ipmi 31 { 32 namespace fs = std::experimental::filesystem; 33 34 struct CableRequest 35 { 36 uint8_t subcommand; 37 uint8_t if_name_len; 38 uint8_t if_name[0]; 39 } __attribute__((packed)); 40 41 struct CableReply 42 { 43 uint8_t subcommand; 44 uint8_t value; 45 } __attribute__((packed)); 46 47 ipmi_ret_t CableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen) 48 { 49 // There is an IPMI LAN channel statistics command which could be used for 50 // this type of check, however, we're not able to wait for the OpenBMC 51 // implementation to stabilize related to the network management. 52 // 53 // There is a link status file, but it is "unknown" to start with... 54 // The path we're checking: /sys/class/net/eth1/statistics/rx_packets 55 56 // This command is expecting: [0x00][len][if_name] 57 if ((*dataLen) < sizeof(struct CableRequest) + sizeof(uint8_t)) 58 { 59 fprintf(stderr, "Invalid command length: %lu\n", (*dataLen)); 60 return IPMI_CC_INVALID; 61 } 62 63 const auto request = 64 reinterpret_cast<const struct CableRequest*>(&reqBuf[0]); 65 66 // Sanity check the object contents. 67 if (request->if_name_len == 0) 68 { 69 fprintf(stderr, "Invalid string length: %d\n", request->if_name_len); 70 return IPMI_CC_INVALID; 71 } 72 73 // Verify the request buffer contains the object and the string. 74 if ((*dataLen) < (sizeof(struct CableRequest) + request->if_name_len)) 75 { 76 fprintf(stderr, "*dataLen too small: %lu\n", (*dataLen)); 77 return IPMI_CC_INVALID; 78 } 79 80 // Maximum length one can specify, plus null terminator. 81 char nameBuf[256] = {}; 82 std::ostringstream opath; 83 84 // Copy the string out of the request buffer. 85 memcpy(&nameBuf[0], request->if_name, request->if_name_len); 86 std::string name = nameBuf; 87 88 // Minor sanity & security check (of course, I'm less certain if unicode 89 // comes into play here. 90 // 91 // Basically you can't easily inject ../ or /../ into the path below. 92 if (name.find("/") != std::string::npos) 93 { 94 fprintf(stderr, "Invalid or illegal name: '%s'\n", nameBuf); 95 return IPMI_CC_INVALID; 96 } 97 98 opath << "/sys/class/net/" << name << "/statistics/rx_packets"; 99 std::string path = opath.str(); 100 101 std::error_code ec; 102 if (!fs::exists(path, ec)) 103 { 104 fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str()); 105 return IPMI_CC_INVALID; 106 } 107 // We're uninterested in the state of ec. 108 109 // Read the file and check the result. 110 int64_t count = 0; 111 std::ifstream ifs; 112 ifs.exceptions(std::ifstream::failbit); 113 try 114 { 115 ifs.open(path); 116 ifs >> count; 117 } 118 catch (std::ios_base::failure& fail) 119 { 120 return IPMI_CC_INVALID; 121 } 122 123 struct CableReply reply; 124 reply.subcommand = SysCableCheck; 125 reply.value = 0x00; // Default false 126 127 // If we have received packets then there is a cable present. 128 reply.value = (count > 0) ? 1 : 0; 129 130 // Return the subcommand and the result. 131 memcpy(&replyBuf[0], &reply, sizeof(struct CableReply)); 132 (*dataLen) = sizeof(struct CableReply); 133 134 return IPMI_CC_OK; 135 } 136 137 } // namespace ipmi 138 } // namespace google 139