| /* |
| * 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 "eth.hpp" |
| |
| #include "main.hpp" |
| |
| #include <cstdint> |
| #include <cstring> |
| #include <string> |
| |
| namespace google |
| { |
| namespace ipmi |
| { |
| |
| struct EthDeviceRequest |
| { |
| uint8_t subcommand; |
| } __attribute__((packed)); |
| |
| // The phosphor-host-ipmi daemon requires a configuration that maps |
| // the if_name to the IPMI LAN channel. However, that doesn't strictly |
| // define which is meant to be used for NCSI. |
| #ifndef NCSI_IPMI_CHANNEL |
| #define NCSI_IPMI_CHANNEL 1 |
| #endif |
| |
| #ifndef NCSI_IF_NAME |
| #define NCSI_IF_NAME eth0 |
| #endif |
| |
| // TOOD(venture): The ipmid.h has this macro, which is a header we |
| // can't normally access. |
| #ifndef MAX_IPMI_BUFFER |
| #define MAX_IPMI_BUFFER 64 |
| #endif |
| |
| // To deal with receiving a string without quotes. |
| #define QUOTE(name) #name |
| #define STR(macro) QUOTE(macro) |
| #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME) |
| |
| ipmi_ret_t GetEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf, |
| size_t* dataLen) |
| { |
| if ((*dataLen) < sizeof(struct EthDeviceRequest)) |
| { |
| std::fprintf(stderr, "Invalid command length: %u\n", |
| static_cast<uint32_t>(*dataLen)); |
| return IPMI_CC_REQ_DATA_LEN_INVALID; |
| } |
| |
| std::string device = NCSI_IF_NAME_STR; |
| if (device.length() == 0) |
| { |
| std::fprintf(stderr, "Invalid eth string\n"); |
| return IPMI_CC_REQ_DATA_LEN_INVALID; |
| } |
| |
| if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER) |
| { |
| std::fprintf(stderr, "Response would overflow response buffer\n"); |
| return IPMI_CC_REQUESTED_TOO_MANY_BYTES; |
| } |
| |
| // Fill in the response buffer. |
| auto reply = reinterpret_cast<struct EthDeviceReply*>(&replyBuf[0]); |
| reply->subcommand = SysGetEthDevice; |
| reply->channel = NCSI_IPMI_CHANNEL; |
| reply->if_name_len = device.length(); |
| std::memcpy(reply->if_name, device.c_str(), device.length()); |
| |
| (*dataLen) = sizeof(struct EthDeviceReply) + device.length(); |
| |
| return IPMI_CC_OK; |
| } |
| |
| } // namespace ipmi |
| } // namespace google |