/src/tinyusb/lib/networking/rndis_reports.c
Line | Count | Source |
1 | | /* |
2 | | The original version of this code was lrndis/usbd_rndis_core.c from https://github.com/fetisov/lrndis |
3 | | It has since been overhauled to suit this application |
4 | | */ |
5 | | |
6 | | /* |
7 | | * The MIT License (MIT) |
8 | | * |
9 | | * Copyright (c) 2015 by Sergey Fetisov <fsenok@gmail.com> |
10 | | * |
11 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
12 | | * of this software and associated documentation files (the "Software"), to deal |
13 | | * in the Software without restriction, including without limitation the rights |
14 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
15 | | * copies of the Software, and to permit persons to whom the Software is |
16 | | * furnished to do so, subject to the following conditions: |
17 | | * |
18 | | * The above copyright notice and this permission notice shall be included in all |
19 | | * copies or substantial portions of the Software. |
20 | | * |
21 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
22 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
23 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
24 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
25 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
26 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
27 | | * SOFTWARE. |
28 | | */ |
29 | | |
30 | | #include <stdalign.h> |
31 | | #include <string.h> |
32 | | #include "tusb.h" |
33 | | |
34 | | #if CFG_TUD_ECM_RNDIS |
35 | | |
36 | | #include "rndis_protocol.h" |
37 | | #include "netif/ethernet.h" |
38 | | |
39 | | #define RNDIS_VENDOR "TinyUSB" /* NIC vendor name */ |
40 | | |
41 | | static const uint8_t *const station_hwaddr = tud_network_mac_address; |
42 | | static const uint8_t *const permanent_hwaddr = tud_network_mac_address; |
43 | | |
44 | | static usb_eth_stat_t usb_eth_stat = { 0, 0, 0, 0 }; |
45 | | static uint32_t oid_packet_filter = 0x0000000; |
46 | | TU_ATTR_UNUSED static rndis_state_t rndis_state; |
47 | | |
48 | | static const uint32_t OIDSupportedList[] = |
49 | | { |
50 | | OID_GEN_SUPPORTED_LIST, |
51 | | OID_GEN_HARDWARE_STATUS, |
52 | | OID_GEN_MEDIA_SUPPORTED, |
53 | | OID_GEN_MEDIA_IN_USE, |
54 | | OID_GEN_MAXIMUM_FRAME_SIZE, |
55 | | OID_GEN_LINK_SPEED, |
56 | | OID_GEN_TRANSMIT_BLOCK_SIZE, |
57 | | OID_GEN_RECEIVE_BLOCK_SIZE, |
58 | | OID_GEN_VENDOR_ID, |
59 | | OID_GEN_VENDOR_DESCRIPTION, |
60 | | OID_GEN_VENDOR_DRIVER_VERSION, |
61 | | OID_GEN_CURRENT_PACKET_FILTER, |
62 | | OID_GEN_MAXIMUM_TOTAL_SIZE, |
63 | | OID_GEN_PROTOCOL_OPTIONS, |
64 | | OID_GEN_MAC_OPTIONS, |
65 | | OID_GEN_MEDIA_CONNECT_STATUS, |
66 | | OID_GEN_MAXIMUM_SEND_PACKETS, |
67 | | OID_802_3_PERMANENT_ADDRESS, |
68 | | OID_802_3_CURRENT_ADDRESS, |
69 | | OID_802_3_MULTICAST_LIST, |
70 | | OID_802_3_MAXIMUM_LIST_SIZE, |
71 | | OID_802_3_MAC_OPTIONS |
72 | | }; |
73 | | |
74 | 0 | #define OID_LIST_LENGTH TU_ARRAY_SIZE(OIDSupportedList) |
75 | | #define ENC_BUF_SIZE (OID_LIST_LENGTH * 4 + 32) |
76 | | |
77 | | static void *encapsulated_buffer; |
78 | | |
79 | | // Number of bytes actually received for the message in encapsulated_buffer. |
80 | | // Host controlled; used to bound the offsets carried inside a SET message. |
81 | | static uint32_t encapsulated_len; |
82 | | |
83 | 0 | static void rndis_report(void) { |
84 | 0 | uint8_t ndis_report[8] = { 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; |
85 | 0 | netd_report(ndis_report, sizeof(ndis_report)); |
86 | 0 | } |
87 | | |
88 | | static void rndis_query_cmplt32(int status, uint32_t data) |
89 | 0 | { |
90 | 0 | rndis_query_cmplt_t *c; |
91 | 0 | c = (rndis_query_cmplt_t *)encapsulated_buffer; |
92 | 0 | c->MessageType = REMOTE_NDIS_QUERY_CMPLT; |
93 | 0 | c->MessageLength = sizeof(rndis_query_cmplt_t) + 4; |
94 | 0 | c->InformationBufferLength = 4; |
95 | 0 | c->InformationBufferOffset = 16; |
96 | 0 | c->Status = status; |
97 | 0 | memcpy(c + 1, &data, sizeof(data)); |
98 | 0 | rndis_report(); |
99 | 0 | } |
100 | | |
101 | | static void rndis_query_cmplt(int status, const void *data, int size) |
102 | 0 | { |
103 | 0 | rndis_query_cmplt_t *c; |
104 | 0 | c = (rndis_query_cmplt_t *)encapsulated_buffer; |
105 | 0 | c->MessageType = REMOTE_NDIS_QUERY_CMPLT; |
106 | 0 | c->MessageLength = sizeof(rndis_query_cmplt_t) + size; |
107 | 0 | c->InformationBufferLength = size; |
108 | 0 | c->InformationBufferOffset = 16; |
109 | 0 | c->Status = status; |
110 | 0 | memcpy(c + 1, data, size); |
111 | 0 | rndis_report(); |
112 | 0 | } |
113 | | |
114 | | #define MAC_OPT NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA | \ |
115 | | NDIS_MAC_OPTION_RECEIVE_SERIALIZED | \ |
116 | | NDIS_MAC_OPTION_TRANSFERS_NOT_PEND | \ |
117 | | NDIS_MAC_OPTION_NO_LOOPBACK |
118 | | |
119 | | static const char *rndis_vendor = RNDIS_VENDOR; |
120 | | |
121 | | static void rndis_query(void) |
122 | 0 | { |
123 | 0 | switch (((rndis_query_msg_t *)encapsulated_buffer)->Oid) |
124 | 0 | { |
125 | 0 | case OID_GEN_SUPPORTED_LIST: rndis_query_cmplt(RNDIS_STATUS_SUCCESS, OIDSupportedList, 4 * OID_LIST_LENGTH); return; |
126 | 0 | case OID_GEN_VENDOR_DRIVER_VERSION: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 0x00001000); return; |
127 | 0 | case OID_802_3_CURRENT_ADDRESS: rndis_query_cmplt(RNDIS_STATUS_SUCCESS, station_hwaddr, 6); return; |
128 | 0 | case OID_802_3_PERMANENT_ADDRESS: rndis_query_cmplt(RNDIS_STATUS_SUCCESS, permanent_hwaddr, 6); return; |
129 | 0 | case OID_GEN_MEDIA_SUPPORTED: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, NDIS_MEDIUM_802_3); return; |
130 | 0 | case OID_GEN_MEDIA_IN_USE: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, NDIS_MEDIUM_802_3); return; |
131 | 0 | case OID_GEN_PHYSICAL_MEDIUM: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, NDIS_MEDIUM_802_3); return; |
132 | 0 | case OID_GEN_HARDWARE_STATUS: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 0); return; |
133 | 0 | case OID_GEN_LINK_SPEED: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, tud_network_link_bitrate() / 100U); return; // 100 bit/s units |
134 | 0 | case OID_GEN_VENDOR_ID: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 0x00FFFFFF); return; |
135 | 0 | case OID_GEN_VENDOR_DESCRIPTION: rndis_query_cmplt(RNDIS_STATUS_SUCCESS, rndis_vendor, strlen(rndis_vendor) + 1); return; |
136 | 0 | case OID_GEN_CURRENT_PACKET_FILTER: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, oid_packet_filter); return; |
137 | 0 | case OID_GEN_MAXIMUM_FRAME_SIZE: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, CFG_TUD_NET_MTU - SIZEOF_ETH_HDR); return; |
138 | 0 | case OID_GEN_MAXIMUM_TOTAL_SIZE: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, CFG_TUD_NET_MTU); return; |
139 | 0 | case OID_GEN_TRANSMIT_BLOCK_SIZE: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, CFG_TUD_NET_MTU); return; |
140 | 0 | case OID_GEN_RECEIVE_BLOCK_SIZE: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, CFG_TUD_NET_MTU); return; |
141 | 0 | case OID_GEN_MEDIA_CONNECT_STATUS: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, NDIS_MEDIA_STATE_CONNECTED); return; |
142 | 0 | case OID_GEN_RNDIS_CONFIG_PARAMETER: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 0); return; |
143 | 0 | case OID_802_3_MAXIMUM_LIST_SIZE: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 1); return; |
144 | 0 | case OID_802_3_MULTICAST_LIST: rndis_query_cmplt32(RNDIS_STATUS_NOT_SUPPORTED, 0); return; |
145 | 0 | case OID_802_3_MAC_OPTIONS: rndis_query_cmplt32(RNDIS_STATUS_NOT_SUPPORTED, 0); return; |
146 | 0 | case OID_GEN_MAC_OPTIONS: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, /*MAC_OPT*/ 0); return; |
147 | 0 | case OID_802_3_RCV_ERROR_ALIGNMENT: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 0); return; |
148 | 0 | case OID_802_3_XMIT_ONE_COLLISION: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 0); return; |
149 | 0 | case OID_802_3_XMIT_MORE_COLLISIONS: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 0); return; |
150 | 0 | case OID_GEN_XMIT_OK: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, usb_eth_stat.txok); return; |
151 | 0 | case OID_GEN_RCV_OK: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, usb_eth_stat.rxok); return; |
152 | 0 | case OID_GEN_RCV_ERROR: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, usb_eth_stat.rxbad); return; |
153 | 0 | case OID_GEN_XMIT_ERROR: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, usb_eth_stat.txbad); return; |
154 | 0 | case OID_GEN_RCV_NO_BUFFER: rndis_query_cmplt32(RNDIS_STATUS_SUCCESS, 0); return; |
155 | 0 | default: rndis_query_cmplt(RNDIS_STATUS_FAILURE, NULL, 0); return; |
156 | 0 | } |
157 | 0 | } |
158 | | |
159 | | // Resolve the information buffer of a SET message. offset is measured from |
160 | | // &m->RequestId per the RNDIS InformationBufferOffset definition; both offset |
161 | | // and len are host controlled. Returns NULL when the requested window is not |
162 | | // fully covered by the bytes received (encapsulated_len), so a crafted offset |
163 | | // cannot read past the control buffer. Subtraction form avoids wrap. |
164 | | static const void *rndis_set_infobuf(const rndis_set_msg_t *m, uint32_t offset, uint32_t len) |
165 | 0 | { |
166 | 0 | const uint32_t base = (uint32_t) offsetof(rndis_set_msg_t, RequestId); |
167 | 0 | if (encapsulated_len < base) return NULL; |
168 | 0 | const uint32_t avail = encapsulated_len - base; |
169 | 0 | if (offset > avail || len > avail - offset) return NULL; |
170 | 0 | return (const uint8_t *) &(m->RequestId) + offset; |
171 | 0 | } |
172 | | |
173 | | static void rndis_handle_config_parm(const char *data, int keyoffset, int valoffset, int keylen, int vallen) |
174 | 0 | { |
175 | 0 | (void)data; |
176 | 0 | (void)keyoffset; |
177 | 0 | (void)valoffset; |
178 | 0 | (void)keylen; |
179 | 0 | (void)vallen; |
180 | 0 | } |
181 | | |
182 | | static void rndis_packetFilter(uint32_t newfilter) |
183 | 0 | { |
184 | 0 | (void)newfilter; |
185 | 0 | } |
186 | | |
187 | | static void rndis_handle_set_msg(void) |
188 | 0 | { |
189 | 0 | rndis_set_cmplt_t *c; |
190 | 0 | rndis_set_msg_t *m; |
191 | 0 | rndis_Oid_t oid; |
192 | |
|
193 | 0 | c = (rndis_set_cmplt_t *)encapsulated_buffer; |
194 | 0 | m = (rndis_set_msg_t *)encapsulated_buffer; |
195 | |
|
196 | 0 | oid = m->Oid; |
197 | 0 | c->MessageType = REMOTE_NDIS_SET_CMPLT; |
198 | 0 | c->MessageLength = sizeof(rndis_set_cmplt_t); |
199 | 0 | c->Status = RNDIS_STATUS_SUCCESS; |
200 | |
|
201 | 0 | switch (oid) |
202 | 0 | { |
203 | | /* Parameters set up in 'Advanced' tab */ |
204 | 0 | case OID_GEN_RNDIS_CONFIG_PARAMETER: |
205 | 0 | { |
206 | 0 | const rndis_config_parameter_t *p; |
207 | 0 | p = (const rndis_config_parameter_t *) rndis_set_infobuf(m, m->InformationBufferOffset, sizeof(rndis_config_parameter_t)); |
208 | 0 | if (p == NULL) { c->Status = RNDIS_STATUS_INVALID_DATA; break; } |
209 | 0 | rndis_handle_config_parm((const char *) p, p->ParameterNameOffset, p->ParameterValueOffset, p->ParameterNameLength, p->ParameterValueLength); |
210 | 0 | } |
211 | 0 | break; |
212 | | |
213 | | /* Mandatory general OIDs */ |
214 | 0 | case OID_GEN_CURRENT_PACKET_FILTER: |
215 | 0 | { |
216 | 0 | const uint8_t *infbuf = rndis_set_infobuf(m, m->InformationBufferOffset, 4); |
217 | 0 | if (infbuf == NULL) { c->Status = RNDIS_STATUS_INVALID_DATA; break; } |
218 | 0 | memcpy(&oid_packet_filter, infbuf, 4); |
219 | 0 | } |
220 | 0 | if (oid_packet_filter) |
221 | 0 | { |
222 | 0 | rndis_packetFilter(oid_packet_filter); |
223 | 0 | rndis_state = rndis_data_initialized; |
224 | 0 | } |
225 | 0 | else |
226 | 0 | { |
227 | 0 | rndis_state = rndis_initialized; |
228 | 0 | } |
229 | 0 | break; |
230 | | |
231 | 0 | case OID_GEN_CURRENT_LOOKAHEAD: |
232 | 0 | break; |
233 | | |
234 | 0 | case OID_GEN_PROTOCOL_OPTIONS: |
235 | 0 | break; |
236 | | |
237 | | /* Mandatory 802_3 OIDs */ |
238 | 0 | case OID_802_3_MULTICAST_LIST: |
239 | 0 | break; |
240 | | |
241 | | /* Power Management: fails for now */ |
242 | 0 | case OID_PNP_ADD_WAKE_UP_PATTERN: |
243 | 0 | case OID_PNP_REMOVE_WAKE_UP_PATTERN: |
244 | 0 | case OID_PNP_ENABLE_WAKE_UP: |
245 | 0 | default: |
246 | 0 | c->Status = RNDIS_STATUS_FAILURE; |
247 | 0 | break; |
248 | 0 | } |
249 | | |
250 | | /* c->MessageID is same as before */ |
251 | 0 | rndis_report(); |
252 | 0 | return; |
253 | 0 | } |
254 | | |
255 | | void rndis_class_set_handler(uint8_t *data, int size) |
256 | 0 | { |
257 | 0 | encapsulated_buffer = data; |
258 | 0 | encapsulated_len = (size < 0) ? 0u : (uint32_t) size; |
259 | |
|
260 | 0 | switch (((rndis_generic_msg_t *)encapsulated_buffer)->MessageType) |
261 | 0 | { |
262 | 0 | case REMOTE_NDIS_INITIALIZE_MSG: |
263 | 0 | { |
264 | 0 | rndis_initialize_cmplt_t *m; |
265 | 0 | m = ((rndis_initialize_cmplt_t *)encapsulated_buffer); |
266 | | /* m->MessageID is same as before */ |
267 | 0 | m->MessageType = REMOTE_NDIS_INITIALIZE_CMPLT; |
268 | 0 | m->MessageLength = sizeof(rndis_initialize_cmplt_t); |
269 | 0 | m->MajorVersion = RNDIS_MAJOR_VERSION; |
270 | 0 | m->MinorVersion = RNDIS_MINOR_VERSION; |
271 | 0 | m->Status = RNDIS_STATUS_SUCCESS; |
272 | 0 | m->DeviceFlags = RNDIS_DF_CONNECTIONLESS; |
273 | 0 | m->Medium = RNDIS_MEDIUM_802_3; |
274 | 0 | m->MaxPacketsPerTransfer = 1; |
275 | 0 | m->MaxTransferSize = CFG_TUD_NET_MTU + sizeof(rndis_data_packet_t); |
276 | 0 | m->PacketAlignmentFactor = 0; |
277 | 0 | m->AfListOffset = 0; |
278 | 0 | m->AfListSize = 0; |
279 | 0 | rndis_state = rndis_initialized; |
280 | 0 | rndis_report(); |
281 | 0 | } |
282 | 0 | break; |
283 | | |
284 | 0 | case REMOTE_NDIS_QUERY_MSG: |
285 | 0 | rndis_query(); |
286 | 0 | break; |
287 | | |
288 | 0 | case REMOTE_NDIS_SET_MSG: |
289 | 0 | rndis_handle_set_msg(); |
290 | 0 | break; |
291 | | |
292 | 0 | case REMOTE_NDIS_RESET_MSG: |
293 | 0 | { |
294 | 0 | rndis_reset_cmplt_t * m; |
295 | 0 | m = ((rndis_reset_cmplt_t *)encapsulated_buffer); |
296 | 0 | rndis_state = rndis_uninitialized; |
297 | 0 | m->MessageType = REMOTE_NDIS_RESET_CMPLT; |
298 | 0 | m->MessageLength = sizeof(rndis_reset_cmplt_t); |
299 | 0 | m->Status = RNDIS_STATUS_SUCCESS; |
300 | 0 | m->AddressingReset = 1; /* Make it look like we did something */ |
301 | | /* m->AddressingReset = 0; - Windows halts if set to 1 for some reason */ |
302 | 0 | rndis_report(); |
303 | 0 | } |
304 | 0 | break; |
305 | | |
306 | 0 | case REMOTE_NDIS_KEEPALIVE_MSG: |
307 | 0 | { |
308 | 0 | rndis_keepalive_cmplt_t * m; |
309 | 0 | m = (rndis_keepalive_cmplt_t *)encapsulated_buffer; |
310 | 0 | m->MessageType = REMOTE_NDIS_KEEPALIVE_CMPLT; |
311 | 0 | m->MessageLength = sizeof(rndis_keepalive_cmplt_t); |
312 | 0 | m->Status = RNDIS_STATUS_SUCCESS; |
313 | 0 | } |
314 | | /* We have data to send back */ |
315 | 0 | rndis_report(); |
316 | 0 | break; |
317 | | |
318 | 0 | default: |
319 | 0 | break; |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | | #endif |