Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 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 "eth.hpp" |
| 18 | |
| 19 | #include "main.hpp" |
| 20 | |
| 21 | #include <cstdint> |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 22 | #include <cstring> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
| 25 | namespace google |
| 26 | { |
| 27 | namespace ipmi |
| 28 | { |
| 29 | |
| 30 | struct EthDeviceRequest |
| 31 | { |
| 32 | uint8_t subcommand; |
| 33 | } __attribute__((packed)); |
| 34 | |
| 35 | // The reply to the ethdevice command specifies the |
| 36 | // IPMI channel number and the if_name used for the |
| 37 | // ncis connection. |
| 38 | struct EthDeviceReply |
| 39 | { |
| 40 | uint8_t subcommand; |
| 41 | uint8_t channel; |
| 42 | // if_name_len doesn't include the null-terminator. |
| 43 | uint8_t if_name_len; |
| 44 | uint8_t if_name[0]; |
| 45 | } __attribute__((packed)); |
| 46 | |
| 47 | // The phosphor-host-ipmi daemon requires a configuration that maps |
| 48 | // the if_name to the IPMI LAN channel. However, that doesn't strictly |
| 49 | // define which is meant to be used for NCSI. |
| 50 | #ifndef NCSI_IPMI_CHANNEL |
| 51 | #define NCSI_IPMI_CHANNEL 1 |
| 52 | #endif |
| 53 | |
| 54 | #ifndef NCSI_IF_NAME |
| 55 | #define NCSI_IF_NAME eth0 |
| 56 | #endif |
| 57 | |
| 58 | // TOOD(venture): The ipmid.h has this macro, which is a header we |
| 59 | // can't normally access. |
| 60 | #ifndef MAX_IPMI_BUFFER |
| 61 | #define MAX_IPMI_BUFFER 64 |
| 62 | #endif |
| 63 | |
| 64 | // To deal with receiving a string without quotes. |
| 65 | #define QUOTE(name) #name |
| 66 | #define STR(macro) QUOTE(macro) |
| 67 | #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME) |
| 68 | |
| 69 | ipmi_ret_t GetEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf, |
| 70 | size_t* dataLen) |
| 71 | { |
| 72 | if ((*dataLen) < sizeof(struct EthDeviceRequest)) |
| 73 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 74 | std::fprintf(stderr, "Invalid command length: %u\n", |
| 75 | static_cast<uint32_t>(*dataLen)); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 76 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | std::string device = NCSI_IF_NAME_STR; |
| 80 | if (device.length() == 0) |
| 81 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 82 | std::fprintf(stderr, "Invalid eth string\n"); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 83 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER) |
| 87 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 88 | std::fprintf(stderr, "Response would overflow response buffer\n"); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 89 | return IPMI_CC_REQUESTED_TOO_MANY_BYTES; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Fill in the response buffer. |
| 93 | auto reply = reinterpret_cast<struct EthDeviceReply*>(&replyBuf[0]); |
| 94 | reply->subcommand = SysGetEthDevice; |
| 95 | reply->channel = NCSI_IPMI_CHANNEL; |
| 96 | reply->if_name_len = device.length(); |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 97 | std::memcpy(reply->if_name, device.c_str(), device.length()); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 98 | |
| 99 | (*dataLen) = sizeof(struct EthDeviceReply) + device.length(); |
| 100 | |
| 101 | return IPMI_CC_OK; |
| 102 | } |
| 103 | |
| 104 | } // namespace ipmi |
| 105 | } // namespace google |