blob: 6dbf07703d6a35b25fe6ba9f5fa9d44a684bdd4e [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
35// The reply to the ethdevice command specifies the
36// IPMI channel number and the if_name used for the
37// ncis connection.
38struct 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
69ipmi_ret_t GetEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf,
70 size_t* dataLen)
71{
72 if ((*dataLen) < sizeof(struct EthDeviceRequest))
73 {
Patrick Venturece07ee02018-09-19 18:09:32 -070074 std::fprintf(stderr, "Invalid command length: %u\n",
75 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080076 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070077 }
78
79 std::string device = NCSI_IF_NAME_STR;
80 if (device.length() == 0)
81 {
Patrick Venturece07ee02018-09-19 18:09:32 -070082 std::fprintf(stderr, "Invalid eth string\n");
Patrick Venturefff98612018-11-12 09:05:54 -080083 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070084 }
85
86 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
87 {
Patrick Venturece07ee02018-09-19 18:09:32 -070088 std::fprintf(stderr, "Response would overflow response buffer\n");
Patrick Venturefff98612018-11-12 09:05:54 -080089 return IPMI_CC_REQUESTED_TOO_MANY_BYTES;
Patrick Venture4d49ae62018-09-17 11:35:32 -070090 }
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 Venturece07ee02018-09-19 18:09:32 -070097 std::memcpy(reply->if_name, device.c_str(), device.length());
Patrick Venture4d49ae62018-09-17 11:35:32 -070098
99 (*dataLen) = sizeof(struct EthDeviceReply) + device.length();
100
101 return IPMI_CC_OK;
102}
103
104} // namespace ipmi
105} // namespace google