Statistics
| Branch: | Revision:

root / hw / usb-net.c @ b55266b5

History | View | Annotate | Download (44 kB)

1
/*
2
 * QEMU USB Net devices
3
 *
4
 * Copyright (c) 2006 Thomas Sailer
5
 * Copyright (c) 2008 Andrzej Zaborowski
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25

    
26
#include "qemu-common.h"
27
#include "usb.h"
28
#include "net.h"
29
#include "sys-queue.h"
30

    
31
/*#define TRAFFIC_DEBUG*/
32
/* Thanks to NetChip Technologies for donating this product ID.
33
 * It's for devices with only CDC Ethernet configurations.
34
 */
35
#define CDC_VENDOR_NUM          0x0525  /* NetChip */
36
#define CDC_PRODUCT_NUM         0xa4a1  /* Linux-USB Ethernet Gadget */
37
/* For hardware that can talk RNDIS and either of the above protocols,
38
 * use this ID ... the windows INF files will know it.
39
 */
40
#define RNDIS_VENDOR_NUM        0x0525  /* NetChip */
41
#define RNDIS_PRODUCT_NUM       0xa4a2  /* Ethernet/RNDIS Gadget */
42

    
43
enum usbstring_idx {
44
    STRING_MANUFACTURER                = 1,
45
    STRING_PRODUCT,
46
    STRING_ETHADDR,
47
    STRING_DATA,
48
    STRING_CONTROL,
49
    STRING_RNDIS_CONTROL,
50
    STRING_CDC,
51
    STRING_SUBSET,
52
    STRING_RNDIS,
53
    STRING_SERIALNUMBER,
54
};
55

    
56
#define DEV_CONFIG_VALUE                1        /* CDC or a subset */
57
#define DEV_RNDIS_CONFIG_VALUE                2        /* RNDIS; optional */
58

    
59
#define USB_CDC_SUBCLASS_ACM                0x02
60
#define USB_CDC_SUBCLASS_ETHERNET        0x06
61

    
62
#define USB_CDC_PROTO_NONE                0
63
#define USB_CDC_ACM_PROTO_VENDOR        0xff
64

    
65
#define USB_CDC_HEADER_TYPE                0x00        /* header_desc */
66
#define USB_CDC_CALL_MANAGEMENT_TYPE        0x01        /* call_mgmt_descriptor */
67
#define USB_CDC_ACM_TYPE                0x02        /* acm_descriptor */
68
#define USB_CDC_UNION_TYPE                0x06        /* union_desc */
69
#define USB_CDC_ETHERNET_TYPE                0x0f        /* ether_desc */
70

    
71
#define USB_DT_CS_INTERFACE                0x24
72
#define USB_DT_CS_ENDPOINT                0x25
73

    
74
#define ClassInterfaceRequest                \
75
    ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
76
#define ClassInterfaceOutRequest        \
77
    ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
78

    
79
#define USB_CDC_SEND_ENCAPSULATED_COMMAND        0x00
80
#define USB_CDC_GET_ENCAPSULATED_RESPONSE        0x01
81
#define USB_CDC_REQ_SET_LINE_CODING                0x20
82
#define USB_CDC_REQ_GET_LINE_CODING                0x21
83
#define USB_CDC_REQ_SET_CONTROL_LINE_STATE        0x22
84
#define USB_CDC_REQ_SEND_BREAK                        0x23
85
#define USB_CDC_SET_ETHERNET_MULTICAST_FILTERS        0x40
86
#define USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER        0x41
87
#define USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER        0x42
88
#define USB_CDC_SET_ETHERNET_PACKET_FILTER        0x43
89
#define USB_CDC_GET_ETHERNET_STATISTIC                0x44
90

    
91
#define LOG2_STATUS_INTERVAL_MSEC        5    /* 1 << 5 == 32 msec */
92
#define STATUS_BYTECOUNT                16   /* 8 byte header + data */
93

    
94
#define ETH_FRAME_LEN                        1514 /* Max. octets in frame sans FCS */
95

    
96
/*
97
 * mostly the same descriptor as the linux gadget rndis driver
98
 */
99
static const uint8_t qemu_net_dev_descriptor[] = {
100
    0x12,                        /*  u8 bLength; */
101
    USB_DT_DEVICE,                /*  u8 bDescriptorType; Device */
102
    0x00, 0x02,                        /*  u16 bcdUSB; v2.0 */
103
    USB_CLASS_COMM,                /*  u8  bDeviceClass; */
104
    0x00,                        /*  u8  bDeviceSubClass; */
105
    0x00,                        /*  u8  bDeviceProtocol; [ low/full only ] */
106
    0x40,                        /*  u8  bMaxPacketSize0 */
107
    RNDIS_VENDOR_NUM & 0xff, RNDIS_VENDOR_NUM >> 8,        /*  u16 idVendor; */
108
    RNDIS_PRODUCT_NUM & 0xff, RNDIS_PRODUCT_NUM >> 8,        /*  u16 idProduct; */
109
    0x00, 0x00,                        /*  u16 bcdDevice */
110
    STRING_MANUFACTURER,        /*  u8  iManufacturer; */
111
    STRING_PRODUCT,                /*  u8  iProduct; */
112
    STRING_SERIALNUMBER,        /*  u8  iSerialNumber; */
113
    0x02,                        /*  u8  bNumConfigurations; */
114
};
115

    
116
static const uint8_t qemu_net_rndis_config_descriptor[] = {
117
    /* Configuration Descriptor */
118
    0x09,                        /*  u8  bLength */
119
    USB_DT_CONFIG,                /*  u8  bDescriptorType */
120
    0x43, 0x00,                        /*  le16 wTotalLength */
121
    0x02,                        /*  u8  bNumInterfaces */
122
    DEV_RNDIS_CONFIG_VALUE,        /*  u8  bConfigurationValue */
123
    STRING_RNDIS,                /*  u8  iConfiguration */
124
    0xc0,                        /*  u8  bmAttributes */
125
    0x32,                        /*  u8  bMaxPower */
126
    /* RNDIS Control Interface */
127
    0x09,                        /*  u8  bLength */
128
    USB_DT_INTERFACE,                /*  u8  bDescriptorType */
129
    0x00,                        /*  u8  bInterfaceNumber */
130
    0x00,                        /*  u8  bAlternateSetting */
131
    0x01,                        /*  u8  bNumEndpoints */
132
    USB_CLASS_COMM,                /*  u8  bInterfaceClass */
133
    USB_CDC_SUBCLASS_ACM,        /*  u8  bInterfaceSubClass */
134
    USB_CDC_ACM_PROTO_VENDOR,        /*  u8  bInterfaceProtocol */
135
    STRING_RNDIS_CONTROL,        /*  u8  iInterface */
136
    /* Header Descriptor */
137
    0x05,                        /*  u8    bLength */
138
    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
139
    USB_CDC_HEADER_TYPE,        /*  u8    bDescriptorSubType */
140
    0x10, 0x01,                        /*  le16  bcdCDC */
141
    /* Call Management Descriptor */
142
    0x05,                        /*  u8    bLength */
143
    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
144
    USB_CDC_CALL_MANAGEMENT_TYPE,        /*  u8    bDescriptorSubType */
145
    0x00,                        /*  u8    bmCapabilities */
146
    0x01,                        /*  u8    bDataInterface */
147
    /* ACM Descriptor */
148
    0x04,                        /*  u8    bLength */
149
    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
150
    USB_CDC_ACM_TYPE,                /*  u8    bDescriptorSubType */
151
    0x00,                        /*  u8    bmCapabilities */
152
    /* Union Descriptor */
153
    0x05,                        /*  u8    bLength */
154
    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
155
    USB_CDC_UNION_TYPE,                /*  u8    bDescriptorSubType */
156
    0x00,                        /*  u8    bMasterInterface0 */
157
    0x01,                        /*  u8    bSlaveInterface0 */
158
    /* Status Descriptor */
159
    0x07,                        /*  u8  bLength */
160
    USB_DT_ENDPOINT,                /*  u8  bDescriptorType */
161
    USB_DIR_IN | 1,                /*  u8  bEndpointAddress */
162
    USB_ENDPOINT_XFER_INT,        /*  u8  bmAttributes */
163
    STATUS_BYTECOUNT & 0xff, STATUS_BYTECOUNT >> 8, /*  le16 wMaxPacketSize */
164
    1 << LOG2_STATUS_INTERVAL_MSEC,        /*  u8  bInterval */
165
    /* RNDIS Data Interface */
166
    0x09,                        /*  u8  bLength */
167
    USB_DT_INTERFACE,                /*  u8  bDescriptorType */
168
    0x01,                        /*  u8  bInterfaceNumber */
169
    0x00,                        /*  u8  bAlternateSetting */
170
    0x02,                        /*  u8  bNumEndpoints */
171
    USB_CLASS_CDC_DATA,                /*  u8  bInterfaceClass */
172
    0x00,                        /*  u8  bInterfaceSubClass */
173
    0x00,                        /*  u8  bInterfaceProtocol */
174
    STRING_DATA,                /*  u8  iInterface */
175
    /* Source Endpoint */
176
    0x07,                        /*  u8  bLength */
177
    USB_DT_ENDPOINT,                /*  u8  bDescriptorType */
178
    USB_DIR_IN | 2,                /*  u8  bEndpointAddress */
179
    USB_ENDPOINT_XFER_BULK,        /*  u8  bmAttributes */
180
    0x40, 0x00,                        /*  le16 wMaxPacketSize */
181
    0x00,                        /*  u8  bInterval */
182
    /* Sink Endpoint */
183
    0x07,                        /*  u8  bLength */
184
    USB_DT_ENDPOINT,                /*  u8  bDescriptorType */
185
    USB_DIR_OUT | 2,                /*  u8  bEndpointAddress */
186
    USB_ENDPOINT_XFER_BULK,        /*  u8  bmAttributes */
187
    0x40, 0x00,                        /*  le16 wMaxPacketSize */
188
    0x00                        /*  u8  bInterval */
189
};
190

    
191
static const uint8_t qemu_net_cdc_config_descriptor[] = {
192
    /* Configuration Descriptor */
193
    0x09,                        /*  u8  bLength */
194
    USB_DT_CONFIG,                /*  u8  bDescriptorType */
195
    0x50, 0x00,                        /*  le16 wTotalLength */
196
    0x02,                        /*  u8  bNumInterfaces */
197
    DEV_CONFIG_VALUE,                /*  u8  bConfigurationValue */
198
    STRING_CDC,                        /*  u8  iConfiguration */
199
    0xc0,                        /*  u8  bmAttributes */
200
    0x32,                        /*  u8  bMaxPower */
201
    /* CDC Control Interface */
202
    0x09,                        /*  u8  bLength */
203
    USB_DT_INTERFACE,                /*  u8  bDescriptorType */
204
    0x00,                        /*  u8  bInterfaceNumber */
205
    0x00,                        /*  u8  bAlternateSetting */
206
    0x01,                        /*  u8  bNumEndpoints */
207
    USB_CLASS_COMM,                /*  u8  bInterfaceClass */
208
    USB_CDC_SUBCLASS_ETHERNET,        /*  u8  bInterfaceSubClass */
209
    USB_CDC_PROTO_NONE,                /*  u8  bInterfaceProtocol */
210
    STRING_CONTROL,                /*  u8  iInterface */
211
    /* Header Descriptor */
212
    0x05,                        /*  u8    bLength */
213
    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
214
    USB_CDC_HEADER_TYPE,        /*  u8    bDescriptorSubType */
215
    0x10, 0x01,                        /*  le16  bcdCDC */
216
    /* Union Descriptor */
217
    0x05,                        /*  u8    bLength */
218
    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
219
    USB_CDC_UNION_TYPE,                /*  u8    bDescriptorSubType */
220
    0x00,                        /*  u8    bMasterInterface0 */
221
    0x01,                        /*  u8    bSlaveInterface0 */
222
    /* Ethernet Descriptor */
223
    0x0d,                        /*  u8    bLength */
224
    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
225
    USB_CDC_ETHERNET_TYPE,        /*  u8    bDescriptorSubType */
226
    STRING_ETHADDR,                /*  u8    iMACAddress */
227
    0x00, 0x00, 0x00, 0x00,        /*  le32  bmEthernetStatistics */
228
    ETH_FRAME_LEN & 0xff, ETH_FRAME_LEN >> 8,        /*  le16  wMaxSegmentSize */
229
    0x00, 0x00,                        /*  le16  wNumberMCFilters */
230
    0x00,                        /*  u8    bNumberPowerFilters */
231
    /* Status Descriptor */
232
    0x07,                        /*  u8  bLength */
233
    USB_DT_ENDPOINT,                /*  u8  bDescriptorType */
234
    USB_DIR_IN | 1,                /*  u8  bEndpointAddress */
235
    USB_ENDPOINT_XFER_INT,        /*  u8  bmAttributes */
236
    STATUS_BYTECOUNT & 0xff, STATUS_BYTECOUNT >> 8, /*  le16 wMaxPacketSize */
237
    1 << LOG2_STATUS_INTERVAL_MSEC,        /*  u8  bInterval */
238
    /* CDC Data (nop) Interface */
239
    0x09,                        /*  u8  bLength */
240
    USB_DT_INTERFACE,                /*  u8  bDescriptorType */
241
    0x01,                        /*  u8  bInterfaceNumber */
242
    0x00,                        /*  u8  bAlternateSetting */
243
    0x00,                        /*  u8  bNumEndpoints */
244
    USB_CLASS_CDC_DATA,                /*  u8  bInterfaceClass */
245
    0x00,                        /*  u8  bInterfaceSubClass */
246
    0x00,                        /*  u8  bInterfaceProtocol */
247
    0x00,                        /*  u8  iInterface */
248
    /* CDC Data Interface */
249
    0x09,                        /*  u8  bLength */
250
    USB_DT_INTERFACE,                /*  u8  bDescriptorType */
251
    0x01,                        /*  u8  bInterfaceNumber */
252
    0x01,                        /*  u8  bAlternateSetting */
253
    0x02,                        /*  u8  bNumEndpoints */
254
    USB_CLASS_CDC_DATA,                /*  u8  bInterfaceClass */
255
    0x00,                        /*  u8  bInterfaceSubClass */
256
    0x00,                        /*  u8  bInterfaceProtocol */
257
    STRING_DATA,                /*  u8  iInterface */
258
    /* Source Endpoint */
259
    0x07,                        /*  u8  bLength */
260
    USB_DT_ENDPOINT,                /*  u8  bDescriptorType */
261
    USB_DIR_IN | 2,                /*  u8  bEndpointAddress */
262
    USB_ENDPOINT_XFER_BULK,        /*  u8  bmAttributes */
263
    0x40, 0x00,                        /*  le16 wMaxPacketSize */
264
    0x00,                        /*  u8  bInterval */
265
    /* Sink Endpoint */
266
    0x07,                        /*  u8  bLength */
267
    USB_DT_ENDPOINT,                /*  u8  bDescriptorType */
268
    USB_DIR_OUT | 2,                /*  u8  bEndpointAddress */
269
    USB_ENDPOINT_XFER_BULK,        /*  u8  bmAttributes */
270
    0x40, 0x00,                        /*  le16 wMaxPacketSize */
271
    0x00                        /*  u8  bInterval */
272
};
273

    
274
/*
275
 * RNDIS Definitions - in theory not specific to USB.
276
 */
277
#define RNDIS_MAXIMUM_FRAME_SIZE        1518
278
#define RNDIS_MAX_TOTAL_SIZE                1558
279

    
280
/* Remote NDIS Versions */
281
#define RNDIS_MAJOR_VERSION                1
282
#define RNDIS_MINOR_VERSION                0
283

    
284
/* Status Values */
285
#define RNDIS_STATUS_SUCCESS                0x00000000U /* Success */
286
#define RNDIS_STATUS_FAILURE                0xc0000001U /* Unspecified error */
287
#define RNDIS_STATUS_INVALID_DATA        0xc0010015U /* Invalid data */
288
#define RNDIS_STATUS_NOT_SUPPORTED        0xc00000bbU /* Unsupported request */
289
#define RNDIS_STATUS_MEDIA_CONNECT        0x4001000bU /* Device connected */
290
#define RNDIS_STATUS_MEDIA_DISCONNECT        0x4001000cU /* Device disconnected */
291

    
292
/* Message Set for Connectionless (802.3) Devices */
293
enum {
294
    RNDIS_PACKET_MSG                = 1,
295
    RNDIS_INITIALIZE_MSG        = 2,        /* Initialize device */
296
    RNDIS_HALT_MSG                = 3,
297
    RNDIS_QUERY_MSG                = 4,
298
    RNDIS_SET_MSG                = 5,
299
    RNDIS_RESET_MSG                = 6,
300
    RNDIS_INDICATE_STATUS_MSG        = 7,
301
    RNDIS_KEEPALIVE_MSG                = 8,
302
};
303

    
304
/* Message completion */
305
enum {
306
    RNDIS_INITIALIZE_CMPLT        = 0x80000002U,
307
    RNDIS_QUERY_CMPLT                = 0x80000004U,
308
    RNDIS_SET_CMPLT                = 0x80000005U,
309
    RNDIS_RESET_CMPLT                = 0x80000006U,
310
    RNDIS_KEEPALIVE_CMPLT        = 0x80000008U,
311
};
312

    
313
/* Device Flags */
314
enum {
315
    RNDIS_DF_CONNECTIONLESS        = 1,
316
    RNDIS_DF_CONNECTIONORIENTED        = 2,
317
};
318

    
319
#define RNDIS_MEDIUM_802_3                0x00000000U
320

    
321
/* from drivers/net/sk98lin/h/skgepnmi.h */
322
#define OID_PNP_CAPABILITIES                0xfd010100
323
#define OID_PNP_SET_POWER                0xfd010101
324
#define OID_PNP_QUERY_POWER                0xfd010102
325
#define OID_PNP_ADD_WAKE_UP_PATTERN        0xfd010103
326
#define OID_PNP_REMOVE_WAKE_UP_PATTERN        0xfd010104
327
#define OID_PNP_ENABLE_WAKE_UP                0xfd010106
328

    
329
typedef uint32_t le32;
330

    
331
typedef struct rndis_init_msg_type {
332
    le32 MessageType;
333
    le32 MessageLength;
334
    le32 RequestID;
335
    le32 MajorVersion;
336
    le32 MinorVersion;
337
    le32 MaxTransferSize;
338
} rndis_init_msg_type;
339

    
340
typedef struct rndis_init_cmplt_type {
341
    le32 MessageType;
342
    le32 MessageLength;
343
    le32 RequestID;
344
    le32 Status;
345
    le32 MajorVersion;
346
    le32 MinorVersion;
347
    le32 DeviceFlags;
348
    le32 Medium;
349
    le32 MaxPacketsPerTransfer;
350
    le32 MaxTransferSize;
351
    le32 PacketAlignmentFactor;
352
    le32 AFListOffset;
353
    le32 AFListSize;
354
} rndis_init_cmplt_type;
355

    
356
typedef struct rndis_halt_msg_type {
357
    le32 MessageType;
358
    le32 MessageLength;
359
    le32 RequestID;
360
} rndis_halt_msg_type;
361

    
362
typedef struct rndis_query_msg_type {
363
    le32 MessageType;
364
    le32 MessageLength;
365
    le32 RequestID;
366
    le32 OID;
367
    le32 InformationBufferLength;
368
    le32 InformationBufferOffset;
369
    le32 DeviceVcHandle;
370
} rndis_query_msg_type;
371

    
372
typedef struct rndis_query_cmplt_type {
373
    le32 MessageType;
374
    le32 MessageLength;
375
    le32 RequestID;
376
    le32 Status;
377
    le32 InformationBufferLength;
378
    le32 InformationBufferOffset;
379
} rndis_query_cmplt_type;
380

    
381
typedef struct rndis_set_msg_type {
382
    le32 MessageType;
383
    le32 MessageLength;
384
    le32 RequestID;
385
    le32 OID;
386
    le32 InformationBufferLength;
387
    le32 InformationBufferOffset;
388
    le32 DeviceVcHandle;
389
} rndis_set_msg_type;
390

    
391
typedef struct rndis_set_cmplt_type {
392
    le32 MessageType;
393
    le32 MessageLength;
394
    le32 RequestID;
395
    le32 Status;
396
} rndis_set_cmplt_type;
397

    
398
typedef struct rndis_reset_msg_type {
399
    le32 MessageType;
400
    le32 MessageLength;
401
    le32 Reserved;
402
} rndis_reset_msg_type;
403

    
404
typedef struct rndis_reset_cmplt_type {
405
    le32 MessageType;
406
    le32 MessageLength;
407
    le32 Status;
408
    le32 AddressingReset;
409
} rndis_reset_cmplt_type;
410

    
411
typedef struct rndis_indicate_status_msg_type {
412
    le32 MessageType;
413
    le32 MessageLength;
414
    le32 Status;
415
    le32 StatusBufferLength;
416
    le32 StatusBufferOffset;
417
} rndis_indicate_status_msg_type;
418

    
419
typedef struct rndis_keepalive_msg_type {
420
    le32 MessageType;
421
    le32 MessageLength;
422
    le32 RequestID;
423
} rndis_keepalive_msg_type;
424

    
425
typedef struct rndis_keepalive_cmplt_type {
426
    le32 MessageType;
427
    le32 MessageLength;
428
    le32 RequestID;
429
    le32 Status;
430
} rndis_keepalive_cmplt_type;
431

    
432
struct rndis_packet_msg_type {
433
    le32 MessageType;
434
    le32 MessageLength;
435
    le32 DataOffset;
436
    le32 DataLength;
437
    le32 OOBDataOffset;
438
    le32 OOBDataLength;
439
    le32 NumOOBDataElements;
440
    le32 PerPacketInfoOffset;
441
    le32 PerPacketInfoLength;
442
    le32 VcHandle;
443
    le32 Reserved;
444
};
445

    
446
struct rndis_config_parameter {
447
    le32 ParameterNameOffset;
448
    le32 ParameterNameLength;
449
    le32 ParameterType;
450
    le32 ParameterValueOffset;
451
    le32 ParameterValueLength;
452
};
453

    
454
/* implementation specific */
455
enum rndis_state
456
{
457
    RNDIS_UNINITIALIZED,
458
    RNDIS_INITIALIZED,
459
    RNDIS_DATA_INITIALIZED,
460
};
461

    
462
/* from ndis.h */
463
enum ndis_oid {
464
    /* Required Object IDs (OIDs) */
465
    OID_GEN_SUPPORTED_LIST                = 0x00010101,
466
    OID_GEN_HARDWARE_STATUS                = 0x00010102,
467
    OID_GEN_MEDIA_SUPPORTED                = 0x00010103,
468
    OID_GEN_MEDIA_IN_USE                = 0x00010104,
469
    OID_GEN_MAXIMUM_LOOKAHEAD                = 0x00010105,
470
    OID_GEN_MAXIMUM_FRAME_SIZE                = 0x00010106,
471
    OID_GEN_LINK_SPEED                        = 0x00010107,
472
    OID_GEN_TRANSMIT_BUFFER_SPACE        = 0x00010108,
473
    OID_GEN_RECEIVE_BUFFER_SPACE        = 0x00010109,
474
    OID_GEN_TRANSMIT_BLOCK_SIZE                = 0x0001010a,
475
    OID_GEN_RECEIVE_BLOCK_SIZE                = 0x0001010b,
476
    OID_GEN_VENDOR_ID                        = 0x0001010c,
477
    OID_GEN_VENDOR_DESCRIPTION                = 0x0001010d,
478
    OID_GEN_CURRENT_PACKET_FILTER        = 0x0001010e,
479
    OID_GEN_CURRENT_LOOKAHEAD                = 0x0001010f,
480
    OID_GEN_DRIVER_VERSION                = 0x00010110,
481
    OID_GEN_MAXIMUM_TOTAL_SIZE                = 0x00010111,
482
    OID_GEN_PROTOCOL_OPTIONS                = 0x00010112,
483
    OID_GEN_MAC_OPTIONS                        = 0x00010113,
484
    OID_GEN_MEDIA_CONNECT_STATUS        = 0x00010114,
485
    OID_GEN_MAXIMUM_SEND_PACKETS        = 0x00010115,
486
    OID_GEN_VENDOR_DRIVER_VERSION        = 0x00010116,
487
    OID_GEN_SUPPORTED_GUIDS                = 0x00010117,
488
    OID_GEN_NETWORK_LAYER_ADDRESSES        = 0x00010118,
489
    OID_GEN_TRANSPORT_HEADER_OFFSET        = 0x00010119,
490
    OID_GEN_MACHINE_NAME                = 0x0001021a,
491
    OID_GEN_RNDIS_CONFIG_PARAMETER        = 0x0001021b,
492
    OID_GEN_VLAN_ID                        = 0x0001021c,
493

    
494
    /* Optional OIDs */
495
    OID_GEN_MEDIA_CAPABILITIES                = 0x00010201,
496
    OID_GEN_PHYSICAL_MEDIUM                = 0x00010202,
497

    
498
    /* Required statistics OIDs */
499
    OID_GEN_XMIT_OK                        = 0x00020101,
500
    OID_GEN_RCV_OK                        = 0x00020102,
501
    OID_GEN_XMIT_ERROR                        = 0x00020103,
502
    OID_GEN_RCV_ERROR                        = 0x00020104,
503
    OID_GEN_RCV_NO_BUFFER                = 0x00020105,
504

    
505
    /* Optional statistics OIDs */
506
    OID_GEN_DIRECTED_BYTES_XMIT                = 0x00020201,
507
    OID_GEN_DIRECTED_FRAMES_XMIT        = 0x00020202,
508
    OID_GEN_MULTICAST_BYTES_XMIT        = 0x00020203,
509
    OID_GEN_MULTICAST_FRAMES_XMIT        = 0x00020204,
510
    OID_GEN_BROADCAST_BYTES_XMIT        = 0x00020205,
511
    OID_GEN_BROADCAST_FRAMES_XMIT        = 0x00020206,
512
    OID_GEN_DIRECTED_BYTES_RCV                = 0x00020207,
513
    OID_GEN_DIRECTED_FRAMES_RCV                = 0x00020208,
514
    OID_GEN_MULTICAST_BYTES_RCV                = 0x00020209,
515
    OID_GEN_MULTICAST_FRAMES_RCV        = 0x0002020a,
516
    OID_GEN_BROADCAST_BYTES_RCV                = 0x0002020b,
517
    OID_GEN_BROADCAST_FRAMES_RCV        = 0x0002020c,
518
    OID_GEN_RCV_CRC_ERROR                = 0x0002020d,
519
    OID_GEN_TRANSMIT_QUEUE_LENGTH        = 0x0002020e,
520
    OID_GEN_GET_TIME_CAPS                = 0x0002020f,
521
    OID_GEN_GET_NETCARD_TIME                = 0x00020210,
522
    OID_GEN_NETCARD_LOAD                = 0x00020211,
523
    OID_GEN_DEVICE_PROFILE                = 0x00020212,
524
    OID_GEN_INIT_TIME_MS                = 0x00020213,
525
    OID_GEN_RESET_COUNTS                = 0x00020214,
526
    OID_GEN_MEDIA_SENSE_COUNTS                = 0x00020215,
527
    OID_GEN_FRIENDLY_NAME                = 0x00020216,
528
    OID_GEN_MINIPORT_INFO                = 0x00020217,
529
    OID_GEN_RESET_VERIFY_PARAMETERS        = 0x00020218,
530

    
531
    /* IEEE 802.3 (Ethernet) OIDs */
532
    OID_802_3_PERMANENT_ADDRESS                = 0x01010101,
533
    OID_802_3_CURRENT_ADDRESS                = 0x01010102,
534
    OID_802_3_MULTICAST_LIST                = 0x01010103,
535
    OID_802_3_MAXIMUM_LIST_SIZE                = 0x01010104,
536
    OID_802_3_MAC_OPTIONS                = 0x01010105,
537
    OID_802_3_RCV_ERROR_ALIGNMENT        = 0x01020101,
538
    OID_802_3_XMIT_ONE_COLLISION        = 0x01020102,
539
    OID_802_3_XMIT_MORE_COLLISIONS        = 0x01020103,
540
    OID_802_3_XMIT_DEFERRED                = 0x01020201,
541
    OID_802_3_XMIT_MAX_COLLISIONS        = 0x01020202,
542
    OID_802_3_RCV_OVERRUN                = 0x01020203,
543
    OID_802_3_XMIT_UNDERRUN                = 0x01020204,
544
    OID_802_3_XMIT_HEARTBEAT_FAILURE        = 0x01020205,
545
    OID_802_3_XMIT_TIMES_CRS_LOST        = 0x01020206,
546
    OID_802_3_XMIT_LATE_COLLISIONS        = 0x01020207,
547
};
548

    
549
static const uint32_t oid_supported_list[] =
550
{
551
    /* the general stuff */
552
    OID_GEN_SUPPORTED_LIST,
553
    OID_GEN_HARDWARE_STATUS,
554
    OID_GEN_MEDIA_SUPPORTED,
555
    OID_GEN_MEDIA_IN_USE,
556
    OID_GEN_MAXIMUM_FRAME_SIZE,
557
    OID_GEN_LINK_SPEED,
558
    OID_GEN_TRANSMIT_BLOCK_SIZE,
559
    OID_GEN_RECEIVE_BLOCK_SIZE,
560
    OID_GEN_VENDOR_ID,
561
    OID_GEN_VENDOR_DESCRIPTION,
562
    OID_GEN_VENDOR_DRIVER_VERSION,
563
    OID_GEN_CURRENT_PACKET_FILTER,
564
    OID_GEN_MAXIMUM_TOTAL_SIZE,
565
    OID_GEN_MEDIA_CONNECT_STATUS,
566
    OID_GEN_PHYSICAL_MEDIUM,
567

    
568
    /* the statistical stuff */
569
    OID_GEN_XMIT_OK,
570
    OID_GEN_RCV_OK,
571
    OID_GEN_XMIT_ERROR,
572
    OID_GEN_RCV_ERROR,
573
    OID_GEN_RCV_NO_BUFFER,
574

    
575
    /* IEEE 802.3 */
576
    /* the general stuff */
577
    OID_802_3_PERMANENT_ADDRESS,
578
    OID_802_3_CURRENT_ADDRESS,
579
    OID_802_3_MULTICAST_LIST,
580
    OID_802_3_MAC_OPTIONS,
581
    OID_802_3_MAXIMUM_LIST_SIZE,
582

    
583
    /* the statistical stuff */
584
    OID_802_3_RCV_ERROR_ALIGNMENT,
585
    OID_802_3_XMIT_ONE_COLLISION,
586
    OID_802_3_XMIT_MORE_COLLISIONS,
587
};
588

    
589
#define NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA        (1 << 0)
590
#define NDIS_MAC_OPTION_RECEIVE_SERIALIZED        (1 << 1)
591
#define NDIS_MAC_OPTION_TRANSFERS_NOT_PEND        (1 << 2)
592
#define NDIS_MAC_OPTION_NO_LOOPBACK                (1 << 3)
593
#define NDIS_MAC_OPTION_FULL_DUPLEX                (1 << 4)
594
#define NDIS_MAC_OPTION_EOTX_INDICATION                (1 << 5)
595
#define NDIS_MAC_OPTION_8021P_PRIORITY                (1 << 6)
596

    
597
struct rndis_response {
598
    TAILQ_ENTRY(rndis_response) entries;
599
    uint32_t length;
600
    uint8_t buf[0];
601
};
602

    
603
typedef struct USBNetState {
604
    USBDevice dev;
605

    
606
    unsigned int rndis;
607
    enum rndis_state rndis_state;
608
    uint32_t medium;
609
    uint32_t speed;
610
    uint32_t media_state;
611
    uint16_t filter;
612
    uint32_t vendorid;
613
    uint8_t mac[6];
614

    
615
    unsigned int out_ptr;
616
    uint8_t out_buf[2048];
617

    
618
    USBPacket *inpkt;
619
    unsigned int in_ptr, in_len;
620
    uint8_t in_buf[2048];
621

    
622
    char usbstring_mac[13];
623
    VLANClientState *vc;
624
    TAILQ_HEAD(rndis_resp_head, rndis_response) rndis_resp;
625
} USBNetState;
626

    
627
static int ndis_query(USBNetState *s, uint32_t oid,
628
                      uint8_t *inbuf, unsigned int inlen, uint8_t *outbuf,
629
                      size_t outlen)
630
{
631
    unsigned int i, count;
632

    
633
    switch (oid) {
634
    /* general oids (table 4-1) */
635
    /* mandatory */
636
    case OID_GEN_SUPPORTED_LIST:
637
        count = sizeof(oid_supported_list) / sizeof(uint32_t);
638
        for (i = 0; i < count; i++)
639
            ((le32 *) outbuf)[i] = cpu_to_le32(oid_supported_list[i]);
640
        return sizeof(oid_supported_list);
641

    
642
    /* mandatory */
643
    case OID_GEN_HARDWARE_STATUS:
644
        *((le32 *) outbuf) = cpu_to_le32(0);
645
        return sizeof(le32);
646

    
647
    /* mandatory */
648
    case OID_GEN_MEDIA_SUPPORTED:
649
        *((le32 *) outbuf) = cpu_to_le32(s->medium);
650
        return sizeof(le32);
651

    
652
    /* mandatory */
653
    case OID_GEN_MEDIA_IN_USE:
654
        *((le32 *) outbuf) = cpu_to_le32(s->medium);
655
        return sizeof(le32);
656

    
657
    /* mandatory */
658
    case OID_GEN_MAXIMUM_FRAME_SIZE:
659
        *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
660
        return sizeof(le32);
661

    
662
    /* mandatory */
663
    case OID_GEN_LINK_SPEED:
664
        *((le32 *) outbuf) = cpu_to_le32(s->speed);
665
        return sizeof(le32);
666

    
667
    /* mandatory */
668
    case OID_GEN_TRANSMIT_BLOCK_SIZE:
669
        *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
670
        return sizeof(le32);
671

    
672
    /* mandatory */
673
    case OID_GEN_RECEIVE_BLOCK_SIZE:
674
        *((le32 *) outbuf) = cpu_to_le32(ETH_FRAME_LEN);
675
        return sizeof(le32);
676

    
677
    /* mandatory */
678
    case OID_GEN_VENDOR_ID:
679
        *((le32 *) outbuf) = cpu_to_le32(s->vendorid);
680
        return sizeof(le32);
681

    
682
    /* mandatory */
683
    case OID_GEN_VENDOR_DESCRIPTION:
684
        pstrcpy((char *)outbuf, outlen, "QEMU USB RNDIS Net");
685
        return strlen((char *)outbuf) + 1;
686

    
687
    case OID_GEN_VENDOR_DRIVER_VERSION:
688
        *((le32 *) outbuf) = cpu_to_le32(1);
689
        return sizeof(le32);
690

    
691
    /* mandatory */
692
    case OID_GEN_CURRENT_PACKET_FILTER:
693
        *((le32 *) outbuf) = cpu_to_le32(s->filter);
694
        return sizeof(le32);
695

    
696
    /* mandatory */
697
    case OID_GEN_MAXIMUM_TOTAL_SIZE:
698
        *((le32 *) outbuf) = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
699
        return sizeof(le32);
700

    
701
    /* mandatory */
702
    case OID_GEN_MEDIA_CONNECT_STATUS:
703
        *((le32 *) outbuf) = cpu_to_le32(s->media_state);
704
        return sizeof(le32);
705

    
706
    case OID_GEN_PHYSICAL_MEDIUM:
707
        *((le32 *) outbuf) = cpu_to_le32(0);
708
        return sizeof(le32);
709

    
710
    case OID_GEN_MAC_OPTIONS:
711
        *((le32 *) outbuf) = cpu_to_le32(
712
                        NDIS_MAC_OPTION_RECEIVE_SERIALIZED |
713
                        NDIS_MAC_OPTION_FULL_DUPLEX);
714
        return sizeof(le32);
715

    
716
    /* statistics OIDs (table 4-2) */
717
    /* mandatory */
718
    case OID_GEN_XMIT_OK:
719
        *((le32 *) outbuf) = cpu_to_le32(0);
720
        return sizeof(le32);
721

    
722
    /* mandatory */
723
    case OID_GEN_RCV_OK:
724
        *((le32 *) outbuf) = cpu_to_le32(0);
725
        return sizeof(le32);
726

    
727
    /* mandatory */
728
    case OID_GEN_XMIT_ERROR:
729
        *((le32 *) outbuf) = cpu_to_le32(0);
730
        return sizeof(le32);
731

    
732
    /* mandatory */
733
    case OID_GEN_RCV_ERROR:
734
        *((le32 *) outbuf) = cpu_to_le32(0);
735
        return sizeof(le32);
736

    
737
    /* mandatory */
738
    case OID_GEN_RCV_NO_BUFFER:
739
        *((le32 *) outbuf) = cpu_to_le32(0);
740
        return sizeof(le32);
741

    
742
    /* ieee802.3 OIDs (table 4-3) */
743
    /* mandatory */
744
    case OID_802_3_PERMANENT_ADDRESS:
745
        memcpy(outbuf, s->mac, 6);
746
        return 6;
747

    
748
    /* mandatory */
749
    case OID_802_3_CURRENT_ADDRESS:
750
        memcpy(outbuf, s->mac, 6);
751
        return 6;
752

    
753
    /* mandatory */
754
    case OID_802_3_MULTICAST_LIST:
755
        *((le32 *) outbuf) = cpu_to_le32(0xe0000000);
756
        return sizeof(le32);
757

    
758
    /* mandatory */
759
    case OID_802_3_MAXIMUM_LIST_SIZE:
760
        *((le32 *) outbuf) = cpu_to_le32(1);
761
        return sizeof(le32);
762

    
763
    case OID_802_3_MAC_OPTIONS:
764
        return 0;
765

    
766
    /* ieee802.3 statistics OIDs (table 4-4) */
767
    /* mandatory */
768
    case OID_802_3_RCV_ERROR_ALIGNMENT:
769
        *((le32 *) outbuf) = cpu_to_le32(0);
770
        return sizeof(le32);
771

    
772
    /* mandatory */
773
    case OID_802_3_XMIT_ONE_COLLISION:
774
        *((le32 *) outbuf) = cpu_to_le32(0);
775
        return sizeof(le32);
776

    
777
    /* mandatory */
778
    case OID_802_3_XMIT_MORE_COLLISIONS:
779
        *((le32 *) outbuf) = cpu_to_le32(0);
780
        return sizeof(le32);
781

    
782
    default:
783
        fprintf(stderr, "usbnet: unknown OID 0x%08x\n", oid);
784
        return 0;
785
    }
786
    return -1;
787
}
788

    
789
static int ndis_set(USBNetState *s, uint32_t oid,
790
                uint8_t *inbuf, unsigned int inlen)
791
{
792
    switch (oid) {
793
    case OID_GEN_CURRENT_PACKET_FILTER:
794
        s->filter = le32_to_cpup((le32 *) inbuf);
795
        if (s->filter) {
796
            s->rndis_state = RNDIS_DATA_INITIALIZED;
797
        } else {
798
            s->rndis_state = RNDIS_INITIALIZED;
799
        }
800
        return 0;
801

    
802
    case OID_802_3_MULTICAST_LIST:
803
        return 0;
804
    }
805
    return -1;
806
}
807

    
808
static int rndis_get_response(USBNetState *s, uint8_t *buf)
809
{
810
    int ret = 0;
811
    struct rndis_response *r = s->rndis_resp.tqh_first;
812

    
813
    if (!r)
814
        return ret;
815

    
816
    TAILQ_REMOVE(&s->rndis_resp, r, entries);
817
    ret = r->length;
818
    memcpy(buf, r->buf, r->length);
819
    qemu_free(r);
820

    
821
    return ret;
822
}
823

    
824
static void *rndis_queue_response(USBNetState *s, unsigned int length)
825
{
826
    struct rndis_response *r =
827
            qemu_mallocz(sizeof(struct rndis_response) + length);
828

    
829
    TAILQ_INSERT_TAIL(&s->rndis_resp, r, entries);
830
    r->length = length;
831

    
832
    return &r->buf[0];
833
}
834

    
835
static void rndis_clear_responsequeue(USBNetState *s)
836
{
837
    struct rndis_response *r;
838

    
839
    while ((r = s->rndis_resp.tqh_first)) {
840
        TAILQ_REMOVE(&s->rndis_resp, r, entries);
841
        qemu_free(r);
842
    }
843
}
844

    
845
static int rndis_init_response(USBNetState *s, rndis_init_msg_type *buf)
846
{
847
    rndis_init_cmplt_type *resp =
848
            rndis_queue_response(s, sizeof(rndis_init_cmplt_type));
849

    
850
    if (!resp)
851
        return USB_RET_STALL;
852

    
853
    resp->MessageType = cpu_to_le32(RNDIS_INITIALIZE_CMPLT);
854
    resp->MessageLength = cpu_to_le32(sizeof(rndis_init_cmplt_type));
855
    resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
856
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
857
    resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
858
    resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
859
    resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
860
    resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
861
    resp->MaxPacketsPerTransfer = cpu_to_le32(1);
862
    resp->MaxTransferSize = cpu_to_le32(ETH_FRAME_LEN +
863
                    sizeof(struct rndis_packet_msg_type) + 22);
864
    resp->PacketAlignmentFactor = cpu_to_le32(0);
865
    resp->AFListOffset = cpu_to_le32(0);
866
    resp->AFListSize = cpu_to_le32(0);
867
    return 0;
868
}
869

    
870
static int rndis_query_response(USBNetState *s,
871
                rndis_query_msg_type *buf, unsigned int length)
872
{
873
    rndis_query_cmplt_type *resp;
874
    /* oid_supported_list is the largest data reply */
875
    uint8_t infobuf[sizeof(oid_supported_list)];
876
    uint32_t bufoffs, buflen;
877
    int infobuflen;
878
    unsigned int resplen;
879

    
880
    bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
881
    buflen = le32_to_cpu(buf->InformationBufferLength);
882
    if (bufoffs + buflen > length)
883
        return USB_RET_STALL;
884

    
885
    infobuflen = ndis_query(s, le32_to_cpu(buf->OID),
886
                            bufoffs + (uint8_t *) buf, buflen, infobuf,
887
                            sizeof(infobuf));
888
    resplen = sizeof(rndis_query_cmplt_type) +
889
            ((infobuflen < 0) ? 0 : infobuflen);
890
    resp = rndis_queue_response(s, resplen);
891
    if (!resp)
892
        return USB_RET_STALL;
893

    
894
    resp->MessageType = cpu_to_le32(RNDIS_QUERY_CMPLT);
895
    resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
896
    resp->MessageLength = cpu_to_le32(resplen);
897

    
898
    if (infobuflen < 0) {
899
        /* OID not supported */
900
        resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
901
        resp->InformationBufferLength = cpu_to_le32(0);
902
        resp->InformationBufferOffset = cpu_to_le32(0);
903
        return 0;
904
    }
905

    
906
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
907
    resp->InformationBufferOffset =
908
            cpu_to_le32(infobuflen ? sizeof(rndis_query_cmplt_type) - 8 : 0);
909
    resp->InformationBufferLength = cpu_to_le32(infobuflen);
910
    memcpy(resp + 1, infobuf, infobuflen);
911

    
912
    return 0;
913
}
914

    
915
static int rndis_set_response(USBNetState *s,
916
                rndis_set_msg_type *buf, unsigned int length)
917
{
918
    rndis_set_cmplt_type *resp =
919
            rndis_queue_response(s, sizeof(rndis_set_cmplt_type));
920
    uint32_t bufoffs, buflen;
921
    int ret;
922

    
923
    if (!resp)
924
        return USB_RET_STALL;
925

    
926
    bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
927
    buflen = le32_to_cpu(buf->InformationBufferLength);
928
    if (bufoffs + buflen > length)
929
        return USB_RET_STALL;
930

    
931
    ret = ndis_set(s, le32_to_cpu(buf->OID),
932
                    bufoffs + (uint8_t *) buf, buflen);
933
    resp->MessageType = cpu_to_le32(RNDIS_SET_CMPLT);
934
    resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
935
    resp->MessageLength = cpu_to_le32(sizeof(rndis_set_cmplt_type));
936
    if (ret < 0) {
937
        /* OID not supported */
938
        resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
939
        return 0;
940
    }
941
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
942

    
943
    return 0;
944
}
945

    
946
static int rndis_reset_response(USBNetState *s, rndis_reset_msg_type *buf)
947
{
948
    rndis_reset_cmplt_type *resp =
949
            rndis_queue_response(s, sizeof(rndis_reset_cmplt_type));
950

    
951
    if (!resp)
952
        return USB_RET_STALL;
953

    
954
    resp->MessageType = cpu_to_le32(RNDIS_RESET_CMPLT);
955
    resp->MessageLength = cpu_to_le32(sizeof(rndis_reset_cmplt_type));
956
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
957
    resp->AddressingReset = cpu_to_le32(1); /* reset information */
958

    
959
    return 0;
960
}
961

    
962
static int rndis_keepalive_response(USBNetState *s,
963
                rndis_keepalive_msg_type *buf)
964
{
965
    rndis_keepalive_cmplt_type *resp =
966
            rndis_queue_response(s, sizeof(rndis_keepalive_cmplt_type));
967

    
968
    if (!resp)
969
        return USB_RET_STALL;
970

    
971
    resp->MessageType = cpu_to_le32(RNDIS_KEEPALIVE_CMPLT);
972
    resp->MessageLength = cpu_to_le32(sizeof(rndis_keepalive_cmplt_type));
973
    resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
974
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
975

    
976
    return 0;
977
}
978

    
979
static int rndis_parse(USBNetState *s, uint8_t *data, int length)
980
{
981
    uint32_t msg_type, msg_length;
982
    le32 *tmp = (le32 *) data;
983

    
984
    msg_type = le32_to_cpup(tmp++);
985
    msg_length = le32_to_cpup(tmp++);
986

    
987
    switch (msg_type) {
988
    case RNDIS_INITIALIZE_MSG:
989
        s->rndis_state = RNDIS_INITIALIZED;
990
        return rndis_init_response(s, (rndis_init_msg_type *) data);
991

    
992
    case RNDIS_HALT_MSG:
993
        s->rndis_state = RNDIS_UNINITIALIZED;
994
        return 0;
995

    
996
    case RNDIS_QUERY_MSG:
997
        return rndis_query_response(s, (rndis_query_msg_type *) data, length);
998

    
999
    case RNDIS_SET_MSG:
1000
        return rndis_set_response(s, (rndis_set_msg_type *) data, length);
1001

    
1002
    case RNDIS_RESET_MSG:
1003
        rndis_clear_responsequeue(s);
1004
        s->out_ptr = s->in_ptr = s->in_len = 0;
1005
        return rndis_reset_response(s, (rndis_reset_msg_type *) data);
1006

    
1007
    case RNDIS_KEEPALIVE_MSG:
1008
        /* For USB: host does this every 5 seconds */
1009
        return rndis_keepalive_response(s, (rndis_keepalive_msg_type *) data);
1010
    }
1011

    
1012
    return USB_RET_STALL;
1013
}
1014

    
1015
static void usb_net_handle_reset(USBDevice *dev)
1016
{
1017
}
1018

    
1019
static const char * const usb_net_stringtable[] = {
1020
    [STRING_MANUFACTURER]        = "QEMU",
1021
    [STRING_PRODUCT]                = "RNDIS/QEMU USB Network Device",
1022
    [STRING_ETHADDR]                = "400102030405",
1023
    [STRING_DATA]                = "QEMU USB Net Data Interface",
1024
    [STRING_CONTROL]                = "QEMU USB Net Control Interface",
1025
    [STRING_RNDIS_CONTROL]        = "QEMU USB Net RNDIS Control Interface",
1026
    [STRING_CDC]                = "QEMU USB Net CDC",
1027
    [STRING_SUBSET]                = "QEMU USB Net Subset",
1028
    [STRING_RNDIS]                = "QEMU USB Net RNDIS",
1029
    [STRING_SERIALNUMBER]        = "1",
1030
};
1031

    
1032
static int usb_net_handle_control(USBDevice *dev, int request, int value,
1033
                int index, int length, uint8_t *data)
1034
{
1035
    USBNetState *s = (USBNetState *) dev;
1036
    int ret = 0;
1037

    
1038
    switch(request) {
1039
    case DeviceRequest | USB_REQ_GET_STATUS:
1040
        data[0] = (1 << USB_DEVICE_SELF_POWERED) |
1041
                (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
1042
        data[1] = 0x00;
1043
        ret = 2;
1044
        break;
1045

    
1046
    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
1047
        if (value == USB_DEVICE_REMOTE_WAKEUP) {
1048
            dev->remote_wakeup = 0;
1049
        } else {
1050
            goto fail;
1051
        }
1052
        ret = 0;
1053
        break;
1054

    
1055
    case DeviceOutRequest | USB_REQ_SET_FEATURE:
1056
        if (value == USB_DEVICE_REMOTE_WAKEUP) {
1057
            dev->remote_wakeup = 1;
1058
        } else {
1059
            goto fail;
1060
        }
1061
        ret = 0;
1062
        break;
1063

    
1064
    case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1065
        dev->addr = value;
1066
        ret = 0;
1067
        break;
1068

    
1069
    case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:
1070
        if (!s->rndis || value || index != 0)
1071
            goto fail;
1072
#ifdef TRAFFIC_DEBUG
1073
        {
1074
            unsigned int i;
1075
            fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");
1076
            for (i = 0; i < length; i++) {
1077
                if (!(i & 15))
1078
                    fprintf(stderr, "\n%04x:", i);
1079
                fprintf(stderr, " %02x", data[i]);
1080
            }
1081
            fprintf(stderr, "\n\n");
1082
        }
1083
#endif
1084
        ret = rndis_parse(s, data, length);
1085
        break;
1086

    
1087
    case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:
1088
        if (!s->rndis || value || index != 0)
1089
            goto fail;
1090
        ret = rndis_get_response(s, data);
1091
        if (!ret) {
1092
            data[0] = 0;
1093
            ret = 1;
1094
        }
1095
#ifdef TRAFFIC_DEBUG
1096
        {
1097
            unsigned int i;
1098
            fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");
1099
            for (i = 0; i < ret; i++) {
1100
                if (!(i & 15))
1101
                    fprintf(stderr, "\n%04x:", i);
1102
                fprintf(stderr, " %02x", data[i]);
1103
            }
1104
            fprintf(stderr, "\n\n");
1105
        }
1106
#endif
1107
        break;
1108

    
1109
    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1110
        switch(value >> 8) {
1111
        case USB_DT_DEVICE:
1112
            ret = sizeof(qemu_net_dev_descriptor);
1113
            memcpy(data, qemu_net_dev_descriptor, ret);
1114
            break;
1115

    
1116
        case USB_DT_CONFIG:
1117
            switch (value & 0xff) {
1118
            case 0:
1119
                ret = sizeof(qemu_net_rndis_config_descriptor);
1120
                memcpy(data, qemu_net_rndis_config_descriptor, ret);
1121
                break;
1122

    
1123
            case 1:
1124
                ret = sizeof(qemu_net_cdc_config_descriptor);
1125
                memcpy(data, qemu_net_cdc_config_descriptor, ret);
1126
                break;
1127

    
1128
            default:
1129
                goto fail;
1130
            }
1131

    
1132
            data[2] = ret & 0xff;
1133
            data[3] = ret >> 8;
1134
            break;
1135

    
1136
        case USB_DT_STRING:
1137
            switch (value & 0xff) {
1138
            case 0:
1139
                /* language ids */
1140
                data[0] = 4;
1141
                data[1] = 3;
1142
                data[2] = 0x09;
1143
                data[3] = 0x04;
1144
                ret = 4;
1145
                break;
1146

    
1147
            case STRING_ETHADDR:
1148
                ret = set_usb_string(data, s->usbstring_mac);
1149
                break;
1150

    
1151
            default:
1152
                if (usb_net_stringtable[value & 0xff]) {
1153
                    ret = set_usb_string(data,
1154
                                    usb_net_stringtable[value & 0xff]);
1155
                    break;
1156
                }
1157

    
1158
                goto fail;
1159
            }
1160
            break;
1161

    
1162
        default:
1163
            goto fail;
1164
        }
1165
        break;
1166

    
1167
    case DeviceRequest | USB_REQ_GET_CONFIGURATION:
1168
        data[0] = s->rndis ? DEV_RNDIS_CONFIG_VALUE : DEV_CONFIG_VALUE;
1169
        ret = 1;
1170
        break;
1171

    
1172
    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1173
        switch (value & 0xff) {
1174
        case DEV_CONFIG_VALUE:
1175
            s->rndis = 0;
1176
            break;
1177

    
1178
        case DEV_RNDIS_CONFIG_VALUE:
1179
            s->rndis = 1;
1180
            break;
1181

    
1182
        default:
1183
            goto fail;
1184
        }
1185
        ret = 0;
1186
        break;
1187

    
1188
    case DeviceRequest | USB_REQ_GET_INTERFACE:
1189
    case InterfaceRequest | USB_REQ_GET_INTERFACE:
1190
        data[0] = 0;
1191
        ret = 1;
1192
        break;
1193

    
1194
    case DeviceOutRequest | USB_REQ_SET_INTERFACE:
1195
    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1196
        ret = 0;
1197
        break;
1198

    
1199
    default:
1200
    fail:
1201
        fprintf(stderr, "usbnet: failed control transaction: "
1202
                        "request 0x%x value 0x%x index 0x%x length 0x%x\n",
1203
                        request, value, index, length);
1204
        ret = USB_RET_STALL;
1205
        break;
1206
    }
1207
    return ret;
1208
}
1209

    
1210
static int usb_net_handle_statusin(USBNetState *s, USBPacket *p)
1211
{
1212
    int ret = 8;
1213

    
1214
    if (p->len < 8)
1215
        return USB_RET_STALL;
1216

    
1217
    ((le32 *) p->data)[0] = cpu_to_le32(1);
1218
    ((le32 *) p->data)[1] = cpu_to_le32(0);
1219
    if (!s->rndis_resp.tqh_first)
1220
        ret = USB_RET_NAK;
1221

    
1222
#ifdef TRAFFIC_DEBUG
1223
    fprintf(stderr, "usbnet: interrupt poll len %u return %d", p->len, ret);
1224
    {
1225
        int i;
1226
        fprintf(stderr, ":");
1227
        for (i = 0; i < ret; i++) {
1228
            if (!(i & 15))
1229
                fprintf(stderr, "\n%04x:", i);
1230
            fprintf(stderr, " %02x", p->data[i]);
1231
        }
1232
        fprintf(stderr, "\n\n");
1233
    }
1234
#endif
1235

    
1236
    return ret;
1237
}
1238

    
1239
static int usb_net_handle_datain(USBNetState *s, USBPacket *p)
1240
{
1241
    int ret = USB_RET_NAK;
1242

    
1243
    if (s->in_ptr > s->in_len) {
1244
        s->in_ptr = s->in_len = 0;
1245
        ret = USB_RET_NAK;
1246
        return ret;
1247
    }
1248
    if (!s->in_len) {
1249
        ret = USB_RET_NAK;
1250
        return ret;
1251
    }
1252
    ret = s->in_len - s->in_ptr;
1253
    if (ret > p->len)
1254
        ret = p->len;
1255
    memcpy(p->data, &s->in_buf[s->in_ptr], ret);
1256
    s->in_ptr += ret;
1257
    if (s->in_ptr >= s->in_len &&
1258
                    (s->rndis || (s->in_len & (64 - 1)) || !ret)) {
1259
        /* no short packet necessary */
1260
        s->in_ptr = s->in_len = 0;
1261
    }
1262

    
1263
#ifdef TRAFFIC_DEBUG
1264
    fprintf(stderr, "usbnet: data in len %u return %d", p->len, ret);
1265
    {
1266
        int i;
1267
        fprintf(stderr, ":");
1268
        for (i = 0; i < ret; i++) {
1269
            if (!(i & 15))
1270
                fprintf(stderr, "\n%04x:", i);
1271
            fprintf(stderr, " %02x", p->data[i]);
1272
        }
1273
        fprintf(stderr, "\n\n");
1274
    }
1275
#endif
1276

    
1277
    return ret;
1278
}
1279

    
1280
static int usb_net_handle_dataout(USBNetState *s, USBPacket *p)
1281
{
1282
    int ret = p->len;
1283
    int sz = sizeof(s->out_buf) - s->out_ptr;
1284
    struct rndis_packet_msg_type *msg =
1285
            (struct rndis_packet_msg_type *) s->out_buf;
1286
    uint32_t len;
1287

    
1288
#ifdef TRAFFIC_DEBUG
1289
    fprintf(stderr, "usbnet: data out len %u\n", p->len);
1290
    {
1291
        int i;
1292
        fprintf(stderr, ":");
1293
        for (i = 0; i < p->len; i++) {
1294
            if (!(i & 15))
1295
                fprintf(stderr, "\n%04x:", i);
1296
            fprintf(stderr, " %02x", p->data[i]);
1297
        }
1298
        fprintf(stderr, "\n\n");
1299
    }
1300
#endif
1301

    
1302
    if (sz > ret)
1303
        sz = ret;
1304
    memcpy(&s->out_buf[s->out_ptr], p->data, sz);
1305
    s->out_ptr += sz;
1306

    
1307
    if (!s->rndis) {
1308
        if (ret < 64) {
1309
            qemu_send_packet(s->vc, s->out_buf, s->out_ptr);
1310
            s->out_ptr = 0;
1311
        }
1312
        return ret;
1313
    }
1314
    len = le32_to_cpu(msg->MessageLength);
1315
    if (s->out_ptr < 8 || s->out_ptr < len)
1316
        return ret;
1317
    if (le32_to_cpu(msg->MessageType) == RNDIS_PACKET_MSG) {
1318
        uint32_t offs = 8 + le32_to_cpu(msg->DataOffset);
1319
        uint32_t size = le32_to_cpu(msg->DataLength);
1320
        if (offs + size <= len)
1321
            qemu_send_packet(s->vc, s->out_buf + offs, size);
1322
    }
1323
    s->out_ptr -= len;
1324
    memmove(s->out_buf, &s->out_buf[len], s->out_ptr);
1325

    
1326
    return ret;
1327
}
1328

    
1329
static int usb_net_handle_data(USBDevice *dev, USBPacket *p)
1330
{
1331
    USBNetState *s = (USBNetState *) dev;
1332
    int ret = 0;
1333

    
1334
    switch(p->pid) {
1335
    case USB_TOKEN_IN:
1336
        switch (p->devep) {
1337
        case 1:
1338
            ret = usb_net_handle_statusin(s, p);
1339
            break;
1340

    
1341
        case 2:
1342
            ret = usb_net_handle_datain(s, p);
1343
            break;
1344

    
1345
        default:
1346
            goto fail;
1347
        }
1348
        break;
1349

    
1350
    case USB_TOKEN_OUT:
1351
        switch (p->devep) {
1352
        case 2:
1353
            ret = usb_net_handle_dataout(s, p);
1354
            break;
1355

    
1356
        default:
1357
            goto fail;
1358
        }
1359
        break;
1360

    
1361
    default:
1362
    fail:
1363
        ret = USB_RET_STALL;
1364
        break;
1365
    }
1366
    if (ret == USB_RET_STALL)
1367
        fprintf(stderr, "usbnet: failed data transaction: "
1368
                        "pid 0x%x ep 0x%x len 0x%x\n",
1369
                        p->pid, p->devep, p->len);
1370
    return ret;
1371
}
1372

    
1373
static void usbnet_receive(void *opaque, const uint8_t *buf, int size)
1374
{
1375
    USBNetState *s = opaque;
1376
    struct rndis_packet_msg_type *msg;
1377

    
1378
    if (s->rndis) {
1379
        msg = (struct rndis_packet_msg_type *) s->in_buf;
1380
        if (!s->rndis_state == RNDIS_DATA_INITIALIZED)
1381
            return;
1382
        if (size + sizeof(struct rndis_packet_msg_type) > sizeof(s->in_buf))
1383
            return;
1384

    
1385
        memset(msg, 0, sizeof(struct rndis_packet_msg_type));
1386
        msg->MessageType = cpu_to_le32(RNDIS_PACKET_MSG);
1387
        msg->MessageLength = cpu_to_le32(size + sizeof(struct rndis_packet_msg_type));
1388
        msg->DataOffset = cpu_to_le32(sizeof(struct rndis_packet_msg_type) - 8);
1389
        msg->DataLength = cpu_to_le32(size);
1390
        /* msg->OOBDataOffset;
1391
         * msg->OOBDataLength;
1392
         * msg->NumOOBDataElements;
1393
         * msg->PerPacketInfoOffset;
1394
         * msg->PerPacketInfoLength;
1395
         * msg->VcHandle;
1396
         * msg->Reserved;
1397
         */
1398
        memcpy(msg + 1, buf, size);
1399
        s->in_len = size + sizeof(struct rndis_packet_msg_type);
1400
    } else {
1401
        if (size > sizeof(s->in_buf))
1402
            return;
1403
        memcpy(s->in_buf, buf, size);
1404
        s->in_len = size;
1405
    }
1406
    s->in_ptr = 0;
1407
}
1408

    
1409
static int usbnet_can_receive(void *opaque)
1410
{
1411
    USBNetState *s = opaque;
1412

    
1413
    if (s->rndis && !s->rndis_state == RNDIS_DATA_INITIALIZED)
1414
        return 1;
1415

    
1416
    return !s->in_len;
1417
}
1418

    
1419
static void usb_net_handle_destroy(USBDevice *dev)
1420
{
1421
    USBNetState *s = (USBNetState *) dev;
1422

    
1423
    /* TODO: remove the nd_table[] entry */
1424
    qemu_del_vlan_client(s->vc);
1425
    rndis_clear_responsequeue(s);
1426
    qemu_free(s);
1427
}
1428

    
1429
USBDevice *usb_net_init(NICInfo *nd)
1430
{
1431
    USBNetState *s;
1432

    
1433
    s = qemu_mallocz(sizeof(USBNetState));
1434
    if (!s)
1435
        return NULL;
1436
    s->dev.speed = USB_SPEED_FULL;
1437
    s->dev.handle_packet = usb_generic_handle_packet;
1438

    
1439
    s->dev.handle_reset = usb_net_handle_reset;
1440
    s->dev.handle_control = usb_net_handle_control;
1441
    s->dev.handle_data = usb_net_handle_data;
1442
    s->dev.handle_destroy = usb_net_handle_destroy;
1443

    
1444
    s->rndis = 1;
1445
    s->rndis_state = RNDIS_UNINITIALIZED;
1446
    s->medium = 0;        /* NDIS_MEDIUM_802_3 */
1447
    s->speed = 1000000; /* 100MBps, in 100Bps units */
1448
    s->media_state = 0;        /* NDIS_MEDIA_STATE_CONNECTED */;
1449
    s->filter = 0;
1450
    s->vendorid = 0x1234;
1451

    
1452
    memcpy(s->mac, nd->macaddr, 6);
1453
    TAILQ_INIT(&s->rndis_resp);
1454

    
1455
    pstrcpy(s->dev.devname, sizeof(s->dev.devname),
1456
                    "QEMU USB Network Interface");
1457
    s->vc = qemu_new_vlan_client(nd->vlan,
1458
                    usbnet_receive, usbnet_can_receive, s);
1459

    
1460
    snprintf(s->usbstring_mac, sizeof(s->usbstring_mac),
1461
                    "%02x%02x%02x%02x%02x%02x",
1462
                    0x40, s->mac[1], s->mac[2],
1463
                    s->mac[3], s->mac[4], s->mac[5]);
1464
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1465
                    "usbnet macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
1466
                    s->mac[0], s->mac[1], s->mac[2],
1467
                    s->mac[3], s->mac[4], s->mac[5]);
1468
    fprintf(stderr, "usbnet: initialized mac %02x:%02x:%02x:%02x:%02x:%02x\n",
1469
                    s->mac[0], s->mac[1], s->mac[2],
1470
                    s->mac[3], s->mac[4], s->mac[5]);
1471

    
1472
    return (USBDevice *) s;
1473
}