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