blob: 69eb8c94b40623d2b11fc682ebf95001244409ed [file] [log] [blame]
Patrick Venture4d49ae62018-09-17 11:35:32 -07001/*
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>
Patrick Venturece07ee02018-09-19 18:09:32 -070022#include <cstring>
Patrick Venture4d49ae62018-09-17 11:35:32 -070023#include <experimental/filesystem>
24#include <fstream>
25#include <sstream>
26#include <string>
27#include <system_error>
28
29namespace google
30{
31namespace ipmi
32{
33namespace fs = std::experimental::filesystem;
34
35struct CableRequest
36{
37 uint8_t subcommand;
38 uint8_t if_name_len;
39 uint8_t if_name[0];
40} __attribute__((packed));
41
42struct CableReply
43{
44 uint8_t subcommand;
45 uint8_t value;
46} __attribute__((packed));
47
48ipmi_ret_t CableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen)
49{
50 // There is an IPMI LAN channel statistics command which could be used for
51 // this type of check, however, we're not able to wait for the OpenBMC
52 // implementation to stabilize related to the network management.
53 //
54 // There is a link status file, but it is "unknown" to start with...
55 // The path we're checking: /sys/class/net/eth1/statistics/rx_packets
56
57 // This command is expecting: [0x00][len][if_name]
58 if ((*dataLen) < sizeof(struct CableRequest) + sizeof(uint8_t))
59 {
Patrick Venturece07ee02018-09-19 18:09:32 -070060 std::fprintf(stderr, "Invalid command length: %u\n",
61 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080062 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070063 }
64
65 const auto request =
66 reinterpret_cast<const struct CableRequest*>(&reqBuf[0]);
67
68 // Sanity check the object contents.
69 if (request->if_name_len == 0)
70 {
Patrick Venturece07ee02018-09-19 18:09:32 -070071 std::fprintf(stderr, "Invalid string length: %d\n",
72 request->if_name_len);
Patrick Venturefff98612018-11-12 09:05:54 -080073 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070074 }
75
76 // Verify the request buffer contains the object and the string.
77 if ((*dataLen) < (sizeof(struct CableRequest) + request->if_name_len))
78 {
Patrick Venturece07ee02018-09-19 18:09:32 -070079 std::fprintf(stderr, "*dataLen too small: %u\n",
80 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080081 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070082 }
83
84 // Maximum length one can specify, plus null terminator.
85 char nameBuf[256] = {};
86 std::ostringstream opath;
87
88 // Copy the string out of the request buffer.
Patrick Venturece07ee02018-09-19 18:09:32 -070089 std::memcpy(&nameBuf[0], request->if_name, request->if_name_len);
Patrick Venture4d49ae62018-09-17 11:35:32 -070090 std::string name = nameBuf;
91
92 // Minor sanity & security check (of course, I'm less certain if unicode
93 // comes into play here.
94 //
95 // Basically you can't easily inject ../ or /../ into the path below.
96 if (name.find("/") != std::string::npos)
97 {
Patrick Venturece07ee02018-09-19 18:09:32 -070098 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", nameBuf);
Patrick Venturefff98612018-11-12 09:05:54 -080099 return IPMI_CC_INVALID_FIELD_REQUEST;
Patrick Venture4d49ae62018-09-17 11:35:32 -0700100 }
101
102 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
103 std::string path = opath.str();
104
105 std::error_code ec;
106 if (!fs::exists(path, ec))
107 {
Patrick Venturece07ee02018-09-19 18:09:32 -0700108 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
Patrick Venturefff98612018-11-12 09:05:54 -0800109 return IPMI_CC_INVALID_FIELD_REQUEST;
Patrick Venture4d49ae62018-09-17 11:35:32 -0700110 }
111 // We're uninterested in the state of ec.
112
113 // Read the file and check the result.
114 int64_t count = 0;
115 std::ifstream ifs;
116 ifs.exceptions(std::ifstream::failbit);
117 try
118 {
119 ifs.open(path);
120 ifs >> count;
121 }
122 catch (std::ios_base::failure& fail)
123 {
Patrick Venturefff98612018-11-12 09:05:54 -0800124 return IPMI_CC_UNSPECIFIED_ERROR;
Patrick Venture4d49ae62018-09-17 11:35:32 -0700125 }
126
127 struct CableReply reply;
128 reply.subcommand = SysCableCheck;
Patrick Venture4d49ae62018-09-17 11:35:32 -0700129
130 // If we have received packets then there is a cable present.
131 reply.value = (count > 0) ? 1 : 0;
132
133 // Return the subcommand and the result.
Patrick Venturece07ee02018-09-19 18:09:32 -0700134 std::memcpy(&replyBuf[0], &reply, sizeof(struct CableReply));
Patrick Venture4d49ae62018-09-17 11:35:32 -0700135 (*dataLen) = sizeof(struct CableReply);
136
137 return IPMI_CC_OK;
138}
139
140} // namespace ipmi
141} // namespace google