blob: 45ce91bb909058ec8929136b902a8177bcd1a1e0 [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 "eth.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 <string>
24
25namespace google
26{
27namespace ipmi
28{
29
30struct EthDeviceRequest
31{
32 uint8_t subcommand;
33} __attribute__((packed));
34
Patrick Venture4d49ae62018-09-17 11:35:32 -070035// The phosphor-host-ipmi daemon requires a configuration that maps
36// the if_name to the IPMI LAN channel. However, that doesn't strictly
37// define which is meant to be used for NCSI.
38#ifndef NCSI_IPMI_CHANNEL
39#define NCSI_IPMI_CHANNEL 1
40#endif
41
42#ifndef NCSI_IF_NAME
43#define NCSI_IF_NAME eth0
44#endif
45
46// TOOD(venture): The ipmid.h has this macro, which is a header we
47// can't normally access.
48#ifndef MAX_IPMI_BUFFER
49#define MAX_IPMI_BUFFER 64
50#endif
51
52// To deal with receiving a string without quotes.
53#define QUOTE(name) #name
54#define STR(macro) QUOTE(macro)
55#define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
56
57ipmi_ret_t GetEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf,
58 size_t* dataLen)
59{
60 if ((*dataLen) < sizeof(struct EthDeviceRequest))
61 {
Patrick Venturece07ee02018-09-19 18:09:32 -070062 std::fprintf(stderr, "Invalid command length: %u\n",
63 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080064 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070065 }
66
67 std::string device = NCSI_IF_NAME_STR;
68 if (device.length() == 0)
69 {
Patrick Venturece07ee02018-09-19 18:09:32 -070070 std::fprintf(stderr, "Invalid eth string\n");
Patrick Venturefff98612018-11-12 09:05:54 -080071 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070072 }
73
74 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
75 {
Patrick Venturece07ee02018-09-19 18:09:32 -070076 std::fprintf(stderr, "Response would overflow response buffer\n");
Patrick Venturefff98612018-11-12 09:05:54 -080077 return IPMI_CC_REQUESTED_TOO_MANY_BYTES;
Patrick Venture4d49ae62018-09-17 11:35:32 -070078 }
79
80 // Fill in the response buffer.
81 auto reply = reinterpret_cast<struct EthDeviceReply*>(&replyBuf[0]);
82 reply->subcommand = SysGetEthDevice;
83 reply->channel = NCSI_IPMI_CHANNEL;
84 reply->if_name_len = device.length();
Patrick Venturece07ee02018-09-19 18:09:32 -070085 std::memcpy(reply->if_name, device.c_str(), device.length());
Patrick Venture4d49ae62018-09-17 11:35:32 -070086
87 (*dataLen) = sizeof(struct EthDeviceReply) + device.length();
88
89 return IPMI_CC_OK;
90}
91
92} // namespace ipmi
93} // namespace google