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