blob: 38ab9e33ffcdb48ec7350e9960a406bb774ae809 [file] [log] [blame]
William A. Kennington III7d6fa422021-02-08 17:04:02 -08001/*
2 * Library of NC-SI commands compliant with version 1.0.0.
3 *
4 * This implements a subset of the commands provided in the specification.
5 *
6 * Checksums are optional and not implemented here. All NC-SI checksums are set
7 * to 0 to indicate that per 8.2.2.3.
8 */
9
10#include <stdint.h>
11#include <string.h>
12
13#include <arpa/inet.h>
14
15#include "platforms/nemora/portable/ncsi_client.h"
16
17#ifndef ARRAY_SIZE
18#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
19#endif
20
21// todo - To save space these tables use the notion that no response is
22// larger than 255 bytes. Need a BUILD_ASSERT.
23// todo - Replace 0's with actual sizes once the commands are supported
24static const uint8_t ncsi_response_size_table[] = {
25 sizeof(ncsi_simple_response_t), // NCSI_CLEAR_INITIAL_STATE
26 sizeof(ncsi_simple_response_t), // NCSI_SELECT_PACKAGE
27 sizeof(ncsi_simple_response_t), // NCSI_DESELECT_PACKAGE
28 sizeof(ncsi_simple_response_t), // NCSI_ENABLE_CHANNEL
29 sizeof(ncsi_simple_response_t), // NCSI_DISABLE_CHANNEL
30 sizeof(ncsi_simple_response_t), // NCSI_RESET_CHANNEL
31 sizeof(ncsi_simple_response_t), // NCSI_ENABLE_CHANNEL_NETWORK_TX
32 sizeof(ncsi_simple_response_t), // NCSI_DISABLE_CHANNEL_NETWORK_TX
33 sizeof(ncsi_simple_response_t), // NCSI_AEN_ENABLE
34 sizeof(ncsi_simple_response_t), // NCSI_SET_LINK
35 sizeof(ncsi_link_status_response_t), // NCSI_GET_LINK_STATUS
36 sizeof(ncsi_simple_response_t), // NCSI_SET_VLAN_FILTER
37 sizeof(ncsi_simple_response_t), // NCSI_ENABLE_VLAN
38 sizeof(ncsi_simple_response_t), // NCSI_DISABLE_VLAN
39 sizeof(ncsi_simple_response_t), // NCSI_SET_MAC_ADDRESS
40 0, // 0x0F is not a valid command
41 sizeof(ncsi_simple_response_t), // NCSI_ENABLE_BROADCAST_FILTER
42 sizeof(ncsi_simple_response_t), // NCSI_DISABLE_BROADCAST_FILTER
43 sizeof(ncsi_simple_response_t), // NCSI_ENABLE_GLOBAL_MULTICAST_FILTER
44 sizeof(ncsi_simple_response_t), // NCSI_DISABLE_GLOBAL_MULTICAST_FILTER
45 sizeof(ncsi_simple_response_t), // NCSI_SET_NCSI_FLOW_CONTROL
46 sizeof(ncsi_version_id_response_t), // NCSI_GET_VERSION_ID
47 sizeof(ncsi_capabilities_response_t), // NCSI_GET_CAPABILITIES
48 sizeof(ncsi_parameters_response_t), // NCSI_GET_PARAMETERS
49 0, // NCSI_GET_CONTROLLER_PACKET_STATISTICS
50 0, // NCSI_GET_NCSI_STATISTICS
51 sizeof(ncsi_passthrough_stats_response_t), // NCSI_GET_PASSTHROUGH_STATISTICS
52};
53
54static const uint8_t ncsi_oem_response_size_table[] = {
55 sizeof(ncsi_host_mac_response_t), // NCSI_OEM_COMMAND_GET_HOST_MAC
56 sizeof(ncsi_oem_simple_response_t), // NCSI_OEM_COMMAND_SET_FILTER
57 sizeof(ncsi_oem_get_filter_response_t), // NCSI_OEM_COMMAND_GET_FILTER
58 sizeof(ncsi_oem_echo_response_t), // NCSI_OEM_COMMAND_ECHO
59};
60
61// TODO Should increment when we send a packet that is not a retry.
62static uint8_t current_instance_id;
63
64/*
65 * Sets _most_ of the NC-SI header fields. Caller is expected to set
66 * payload_length field if it is > 0. For many NC-SI commands it is 0.
67 */
68static void set_header_fields(ncsi_header_t* header, uint8_t ch_id,
69 uint8_t cmd_type)
70{
71 // Destination MAC must be all 0xFF.
72 memset(header->ethhdr.dest.octet, 0xFF, sizeof(header->ethhdr.dest.octet));
73 // Source MAC can be any value.
74 memset(header->ethhdr.src.octet, 0xAB, sizeof(header->ethhdr.src.octet));
75 header->ethhdr.ethertype = htons(NCSI_ETHERTYPE);
76
77 // NC-SI Header
78 header->mc_id = NCSI_MC_ID;
79 header->header_revision = NCSI_HEADER_REV;
80 header->reserved_00 = 0;
81 header->instance_id = current_instance_id;
82 header->control_packet_type = cmd_type;
83 header->channel_id = ch_id;
84 header->payload_length = 0; // Caller is expected to set this if != 0.
85 memset(header->reserved_01, 0, sizeof(header->reserved_01));
86}
87
88uint32_t ncsi_get_response_size(uint8_t cmd_type)
89{
90 return (cmd_type < ARRAY_SIZE(ncsi_response_size_table))
91 ? ncsi_response_size_table[cmd_type]
92 : 0;
93}
94
95uint32_t ncsi_oem_get_response_size(uint8_t oem_cmd_type)
96{
97 return (oem_cmd_type < ARRAY_SIZE(ncsi_oem_response_size_table))
98 ? ncsi_oem_response_size_table[oem_cmd_type]
99 : 0;
100}
101
102/*
103 * Clear initial state.
104 */
105uint32_t ncsi_cmd_clear_initial_state(uint8_t* buf, uint8_t channel)
106{
107 set_header_fields((ncsi_header_t*)buf, channel, NCSI_CLEAR_INITIAL_STATE);
108 return sizeof(ncsi_simple_command_t);
109}
110
111/*
112 * Set MAC address filters.
113 */
114uint32_t ncsi_cmd_set_mac(uint8_t* buf, uint8_t channel_id, mac_addr_t* mac)
115{
116 ncsi_set_mac_command_t* cmd = (ncsi_set_mac_command_t*)buf;
117
118 set_header_fields((ncsi_header_t*)buf, channel_id, NCSI_SET_MAC_ADDRESS);
119 cmd->hdr.payload_length =
120 htons(sizeof(ncsi_set_mac_command_t) - sizeof(ncsi_header_t));
121 memcpy(cmd->mac_addr.octet, mac->octet, sizeof(cmd->mac_addr.octet));
122 cmd->mac_addr_num = 1;
123 // Unicast MAC address (AT=0), enabled (E=1).
124 cmd->misc = 0x01;
125 return sizeof(ncsi_set_mac_command_t);
126}
127
128uint32_t ncsi_cmd_enable_broadcast_filter(uint8_t* buf, uint8_t channel,
129 uint32_t filter_settings)
130{
131 ncsi_enable_broadcast_filter_command_t* cmd =
132 (ncsi_enable_broadcast_filter_command_t*)buf;
133 set_header_fields((ncsi_header_t*)buf, channel,
134 NCSI_ENABLE_BROADCAST_FILTER);
135 cmd->hdr.payload_length = htons(
136 sizeof(ncsi_enable_broadcast_filter_command_t) - sizeof(ncsi_header_t));
137 cmd->filter_settings = htonl(filter_settings);
138 return sizeof(ncsi_enable_broadcast_filter_command_t);
139}
140
141
142uint32_t ncsi_cmd_disable_broadcast_filter(uint8_t* buf, uint8_t channel)
143{
144 set_header_fields((ncsi_header_t*)buf, channel,
145 NCSI_DISABLE_BROADCAST_FILTER);
146 return sizeof(ncsi_simple_command_t);
147}
148
149uint32_t ncsi_cmd_enable_channel(uint8_t* buf, uint8_t channel)
150{
151 set_header_fields((ncsi_header_t*)buf, channel, NCSI_ENABLE_CHANNEL);
152 return sizeof(ncsi_simple_command_t);
153}
154
155uint32_t ncsi_cmd_get_link_status(uint8_t* buf, uint8_t channel)
156{
157 set_header_fields((ncsi_header_t*)buf, channel, NCSI_GET_LINK_STATUS);
158 return sizeof(ncsi_simple_command_t);
159}
160
161uint32_t ncsi_cmd_reset_channel(uint8_t* buf, uint8_t channel)
162{
163 set_header_fields((ncsi_header_t*)buf, channel, NCSI_RESET_CHANNEL);
164 return sizeof(ncsi_simple_command_t);
165}
166
167uint32_t ncsi_cmd_enable_tx(uint8_t* buf, uint8_t channel)
168{
169 set_header_fields((ncsi_header_t*)buf, channel,
170 NCSI_ENABLE_CHANNEL_NETWORK_TX);
171 return sizeof(ncsi_simple_command_t);
172}
173
174uint32_t ncsi_cmd_get_version(uint8_t* buf, uint8_t channel)
175{
176 set_header_fields((ncsi_header_t*)buf, channel, NCSI_GET_VERSION_ID);
177 return sizeof(ncsi_simple_command_t);
178}
179
180uint32_t ncsi_cmd_get_capabilities(uint8_t* buf, uint8_t channel)
181{
182 set_header_fields((ncsi_header_t*)buf, channel, NCSI_GET_CAPABILITIES);
183 return sizeof(ncsi_simple_command_t);
184}
185
186uint32_t ncsi_cmd_get_parameters(uint8_t* buf, uint8_t channel)
187{
188 set_header_fields((ncsi_header_t*)buf, channel, NCSI_GET_PARAMETERS);
189 return sizeof(ncsi_simple_command_t);
190}
191
192uint32_t ncsi_cmd_get_passthrough_stats(uint8_t* buf, uint8_t channel)
193{
194 set_header_fields((ncsi_header_t*)buf, channel,
195 NCSI_GET_PASSTHROUGH_STATISTICS);
196 return sizeof(ncsi_simple_command_t);
197}
198
199/* OEM commands */
200
201uint32_t ncsi_oem_cmd_get_host_mac(uint8_t* buf, uint8_t channel)
202{
203 ncsi_oem_simple_cmd_t* cmd = (ncsi_oem_simple_cmd_t*)buf;
204 set_header_fields((ncsi_header_t*)buf, channel, NCSI_OEM_COMMAND);
205 cmd->hdr.payload_length =
206 htons(sizeof(ncsi_oem_simple_cmd_t) - sizeof(ncsi_header_t));
207 cmd->oem_header.manufacturer_id = htonl(NCSI_OEM_MANUFACTURER_ID);
208 memset(cmd->oem_header.reserved, 0, sizeof(cmd->oem_header.reserved));
209 cmd->oem_header.oem_cmd = NCSI_OEM_COMMAND_GET_HOST_MAC;
210 return sizeof(ncsi_oem_simple_cmd_t);
211}
212
213uint32_t ncsi_oem_cmd_get_filter(uint8_t* buf, uint8_t channel)
214{
215 ncsi_oem_simple_cmd_t* cmd = (ncsi_oem_simple_cmd_t*)buf;
216 set_header_fields((ncsi_header_t*)buf, channel, NCSI_OEM_COMMAND);
217 cmd->hdr.payload_length =
218 htons(sizeof(ncsi_oem_simple_cmd_t) - sizeof(ncsi_header_t));
219 cmd->oem_header.manufacturer_id = htonl(NCSI_OEM_MANUFACTURER_ID);
220 memset(cmd->oem_header.reserved, 0, sizeof(cmd->oem_header.reserved));
221 cmd->oem_header.oem_cmd = NCSI_OEM_COMMAND_GET_FILTER;
222 return sizeof(ncsi_oem_simple_cmd_t);
223}
224
225uint32_t ncsi_oem_cmd_set_filter(uint8_t* buf, uint8_t channel, mac_addr_t* mac,
226 uint32_t ip, uint16_t port, uint8_t flags)
227{
228 ncsi_oem_set_filter_cmd_t* cmd = (ncsi_oem_set_filter_cmd_t*)buf;
229 set_header_fields((ncsi_header_t*)buf, channel, NCSI_OEM_COMMAND);
230 cmd->hdr.payload_length =
231 htons(sizeof(ncsi_oem_set_filter_cmd_t) - sizeof(ncsi_header_t));
232 cmd->oem_header.manufacturer_id = htonl(NCSI_OEM_MANUFACTURER_ID);
233 memset(cmd->oem_header.reserved, 0, sizeof(cmd->oem_header.reserved));
234 cmd->oem_header.oem_cmd = NCSI_OEM_COMMAND_SET_FILTER;
235
236 cmd->filter.reserved0 = 0;
237 memcpy(cmd->filter.mac, mac->octet, sizeof(cmd->filter.mac));
238 cmd->filter.ip = htonl(ip);
239 cmd->filter.port = htons(port);
240 cmd->filter.reserved1 = 0;
241 cmd->filter.flags = flags;
242 memset(cmd->filter.regid, 0, sizeof(cmd->filter.regid)); // reserved for set
243 return sizeof(ncsi_oem_set_filter_cmd_t);
244}
245
246uint32_t ncsi_oem_cmd_echo(uint8_t* buf, uint8_t channel, uint8_t pattern[64])
247{
248 ncsi_oem_echo_cmd_t* cmd = (ncsi_oem_echo_cmd_t*)buf;
249 set_header_fields((ncsi_header_t*)buf, channel, NCSI_OEM_COMMAND);
250 cmd->hdr.payload_length =
251 htons(sizeof(ncsi_oem_echo_cmd_t) - sizeof(ncsi_header_t));
252 cmd->oem_header.manufacturer_id = htonl(NCSI_OEM_MANUFACTURER_ID);
253 memset(cmd->oem_header.reserved, 0, sizeof(cmd->oem_header.reserved));
254 cmd->oem_header.oem_cmd = NCSI_OEM_COMMAND_ECHO;
255 memcpy(cmd->pattern, pattern, sizeof(cmd->pattern));
256 return sizeof(ncsi_oem_echo_cmd_t);
257}
258
259ncsi_response_type_t ncsi_validate_response(uint8_t* buf, uint32_t len,
260 uint8_t cmd_type, bool is_oem,
261 uint32_t expected_size) {
262 if (len < sizeof(ncsi_simple_response_t)) {
263 return NCSI_RESPONSE_UNDERSIZED;
264 }
265
266 const ncsi_simple_response_t* response = (ncsi_simple_response_t*)buf;
267 if (response->response_code || response->reason_code) {
268 return NCSI_RESPONSE_NACK;
269 }
270
271 const uint8_t std_cmd_type = is_oem ? NCSI_OEM_COMMAND : cmd_type;
272 if (response->hdr.control_packet_type != (std_cmd_type | NCSI_RESPONSE)) {
273 return NCSI_RESPONSE_UNEXPECTED_TYPE;
274 }
275
276 if (len < expected_size ||
277 ntohs(response->hdr.payload_length) !=
278 expected_size - sizeof(ncsi_header_t)) {
279 return NCSI_RESPONSE_UNEXPECTED_SIZE;
280 }
281
282 if (is_oem) {
283 /* Since the expected_size was checked above, we know that if this is an oem
284 * command, it is the right size. */
285 const ncsi_oem_simple_response_t* oem_response =
286 (ncsi_oem_simple_response_t*)buf;
287 if (oem_response->oem_header.manufacturer_id !=
288 htonl(NCSI_OEM_MANUFACTURER_ID) ||
289 oem_response->oem_header.oem_cmd != cmd_type) {
290 return NCSI_RESPONSE_OEM_FORMAT_ERROR;
291 }
292 }
293
294 return NCSI_RESPONSE_ACK;
295}
296
297ncsi_response_type_t ncsi_validate_std_response(uint8_t* buf, uint32_t len,
298 uint8_t cmd_type) {
299 const uint32_t expected_size = ncsi_get_response_size(cmd_type);
300 return ncsi_validate_response(buf, len, cmd_type, false, expected_size);
301}
302
303ncsi_response_type_t ncsi_validate_oem_response(uint8_t* buf, uint32_t len,
304 uint8_t cmd_type) {
305 const uint32_t expected_size = ncsi_oem_get_response_size(cmd_type);
306 return ncsi_validate_response(buf, len, cmd_type, true, expected_size);
307}