xref: /openbmc/google-ipmi-sys/cable.cpp (revision 0dede335)
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: %u\n",
60                 static_cast<uint32_t>(*dataLen));
61         return IPMI_CC_INVALID;
62     }
63 
64     const auto request =
65         reinterpret_cast<const struct CableRequest*>(&reqBuf[0]);
66 
67     // Sanity check the object contents.
68     if (request->if_name_len == 0)
69     {
70         fprintf(stderr, "Invalid string length: %d\n", request->if_name_len);
71         return IPMI_CC_INVALID;
72     }
73 
74     // Verify the request buffer contains the object and the string.
75     if ((*dataLen) < (sizeof(struct CableRequest) + request->if_name_len))
76     {
77         fprintf(stderr, "*dataLen too small: %u\n",
78                 static_cast<uint32_t>(*dataLen));
79         return IPMI_CC_INVALID;
80     }
81 
82     // Maximum length one can specify, plus null terminator.
83     char nameBuf[256] = {};
84     std::ostringstream opath;
85 
86     // Copy the string out of the request buffer.
87     memcpy(&nameBuf[0], request->if_name, request->if_name_len);
88     std::string name = nameBuf;
89 
90     // Minor sanity & security check (of course, I'm less certain if unicode
91     // comes into play here.
92     //
93     // Basically you can't easily inject ../ or /../ into the path below.
94     if (name.find("/") != std::string::npos)
95     {
96         fprintf(stderr, "Invalid or illegal name: '%s'\n", nameBuf);
97         return IPMI_CC_INVALID;
98     }
99 
100     opath << "/sys/class/net/" << name << "/statistics/rx_packets";
101     std::string path = opath.str();
102 
103     std::error_code ec;
104     if (!fs::exists(path, ec))
105     {
106         fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
107         return IPMI_CC_INVALID;
108     }
109     // We're uninterested in the state of ec.
110 
111     // Read the file and check the result.
112     int64_t count = 0;
113     std::ifstream ifs;
114     ifs.exceptions(std::ifstream::failbit);
115     try
116     {
117         ifs.open(path);
118         ifs >> count;
119     }
120     catch (std::ios_base::failure& fail)
121     {
122         return IPMI_CC_INVALID;
123     }
124 
125     struct CableReply reply;
126     reply.subcommand = SysCableCheck;
127     reply.value = 0x00; // Default false
128 
129     // If we have received packets then there is a cable present.
130     reply.value = (count > 0) ? 1 : 0;
131 
132     // Return the subcommand and the result.
133     memcpy(&replyBuf[0], &reply, sizeof(struct CableReply));
134     (*dataLen) = sizeof(struct CableReply);
135 
136     return IPMI_CC_OK;
137 }
138 
139 } // namespace ipmi
140 } // namespace google
141