Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/usb/dev-network.c
Line
Count
Source
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/osdep.h"
27
#include "qapi/error.h"
28
#include "hw/core/qdev-properties.h"
29
#include "hw/usb/usb.h"
30
#include "migration/vmstate.h"
31
#include "desc.h"
32
#include "net/net.h"
33
#include "qemu/error-report.h"
34
#include "qemu/queue.h"
35
#include "qemu/config-file.h"
36
#include "system/system.h"
37
#include "qemu/iov.h"
38
#include "qemu/module.h"
39
#include "qemu/cutils.h"
40
#include "qom/object.h"
41
42
/*#define TRAFFIC_DEBUG*/
43
/* Thanks to NetChip Technologies for donating this product ID.
44
 * It's for devices with only CDC Ethernet configurations.
45
 */
46
#define CDC_VENDOR_NUM          0x0525  /* NetChip */
47
#define CDC_PRODUCT_NUM         0xa4a1  /* Linux-USB Ethernet Gadget */
48
/* For hardware that can talk RNDIS and either of the above protocols,
49
 * use this ID ... the windows INF files will know it.
50
 */
51
#define RNDIS_VENDOR_NUM        0x0525  /* NetChip */
52
#define RNDIS_PRODUCT_NUM       0xa4a2  /* Ethernet/RNDIS Gadget */
53
54
enum usbstring_idx {
55
    STRING_MANUFACTURER         = 1,
56
    STRING_PRODUCT,
57
    STRING_ETHADDR,
58
    STRING_DATA,
59
    STRING_CONTROL,
60
    STRING_RNDIS_CONTROL,
61
    STRING_CDC,
62
    STRING_SUBSET,
63
    STRING_RNDIS,
64
    STRING_SERIALNUMBER,
65
};
66
67
#define DEV_CONFIG_VALUE                1       /* CDC or a subset */
68
0
#define DEV_RNDIS_CONFIG_VALUE          2       /* RNDIS; optional */
69
70
#define USB_CDC_SUBCLASS_ACM            0x02
71
#define USB_CDC_SUBCLASS_ETHERNET       0x06
72
73
#define USB_CDC_PROTO_NONE              0
74
#define USB_CDC_ACM_PROTO_VENDOR        0xff
75
76
#define USB_CDC_HEADER_TYPE             0x00    /* header_desc */
77
#define USB_CDC_CALL_MANAGEMENT_TYPE    0x01    /* call_mgmt_descriptor */
78
#define USB_CDC_ACM_TYPE                0x02    /* acm_descriptor */
79
#define USB_CDC_UNION_TYPE              0x06    /* union_desc */
80
#define USB_CDC_ETHERNET_TYPE           0x0f    /* ether_desc */
81
82
0
#define USB_CDC_SEND_ENCAPSULATED_COMMAND       0x00
83
0
#define USB_CDC_GET_ENCAPSULATED_RESPONSE       0x01
84
#define USB_CDC_REQ_SET_LINE_CODING             0x20
85
#define USB_CDC_REQ_GET_LINE_CODING             0x21
86
#define USB_CDC_REQ_SET_CONTROL_LINE_STATE      0x22
87
#define USB_CDC_REQ_SEND_BREAK                  0x23
88
#define USB_CDC_SET_ETHERNET_MULTICAST_FILTERS  0x40
89
#define USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER  0x41
90
#define USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER  0x42
91
0
#define USB_CDC_SET_ETHERNET_PACKET_FILTER      0x43
92
#define USB_CDC_GET_ETHERNET_STATISTIC          0x44
93
94
0
#define USB_CDC_NETWORK_CONNECTION      0x00
95
96
#define LOG2_STATUS_INTERVAL_MSEC       5    /* 1 << 5 == 32 msec */
97
#define STATUS_BYTECOUNT                16   /* 8 byte header + data */
98
99
0
#define ETH_FRAME_LEN                   1514 /* Max. octets in frame sans FCS */
100
101
static const USBDescStrings usb_net_stringtable = {
102
    [STRING_MANUFACTURER]       = "QEMU",
103
    [STRING_PRODUCT]            = "RNDIS/QEMU USB Network Device",
104
    [STRING_ETHADDR]            = "400102030405",
105
    [STRING_DATA]               = "QEMU USB Net Data Interface",
106
    [STRING_CONTROL]            = "QEMU USB Net Control Interface",
107
    [STRING_RNDIS_CONTROL]      = "QEMU USB Net RNDIS Control Interface",
108
    [STRING_CDC]                = "QEMU USB Net CDC",
109
    [STRING_SUBSET]             = "QEMU USB Net Subset",
110
    [STRING_RNDIS]              = "QEMU USB Net RNDIS",
111
    [STRING_SERIALNUMBER]       = "1",
112
};
113
114
static const USBDescIface desc_iface_rndis[] = {
115
    {
116
        /* RNDIS Control Interface */
117
        .bInterfaceNumber              = 0,
118
        .bNumEndpoints                 = 1,
119
        .bInterfaceClass               = USB_CLASS_COMM,
120
        .bInterfaceSubClass            = USB_CDC_SUBCLASS_ACM,
121
        .bInterfaceProtocol            = USB_CDC_ACM_PROTO_VENDOR,
122
        .iInterface                    = STRING_RNDIS_CONTROL,
123
        .ndesc                         = 4,
124
        .descs = (USBDescOther[]) {
125
            {
126
                /* Header Descriptor */
127
                .data = (uint8_t[]) {
128
                    0x05,                       /*  u8    bLength */
129
                    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
130
                    USB_CDC_HEADER_TYPE,        /*  u8    bDescriptorSubType */
131
                    0x10, 0x01,                 /*  le16  bcdCDC */
132
                },
133
            },{
134
                /* Call Management Descriptor */
135
                .data = (uint8_t[]) {
136
                    0x05,                       /*  u8    bLength */
137
                    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
138
                    USB_CDC_CALL_MANAGEMENT_TYPE, /*  u8    bDescriptorSubType */
139
                    0x00,                       /*  u8    bmCapabilities */
140
                    0x01,                       /*  u8    bDataInterface */
141
                },
142
            },{
143
                /* ACM Descriptor */
144
                .data = (uint8_t[]) {
145
                    0x04,                       /*  u8    bLength */
146
                    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
147
                    USB_CDC_ACM_TYPE,           /*  u8    bDescriptorSubType */
148
                    0x00,                       /*  u8    bmCapabilities */
149
                },
150
            },{
151
                /* Union Descriptor */
152
                .data = (uint8_t[]) {
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
                },
159
            },
160
        },
161
        .eps = (USBDescEndpoint[]) {
162
            {
163
                .bEndpointAddress      = USB_DIR_IN | 0x01,
164
                .bmAttributes          = USB_ENDPOINT_XFER_INT,
165
                .wMaxPacketSize        = STATUS_BYTECOUNT,
166
                .bInterval             = 1 << LOG2_STATUS_INTERVAL_MSEC,
167
            },
168
        }
169
    },{
170
        /* RNDIS Data Interface */
171
        .bInterfaceNumber              = 1,
172
        .bNumEndpoints                 = 2,
173
        .bInterfaceClass               = USB_CLASS_CDC_DATA,
174
        .iInterface                    = STRING_DATA,
175
        .eps = (USBDescEndpoint[]) {
176
            {
177
                .bEndpointAddress      = USB_DIR_IN | 0x02,
178
                .bmAttributes          = USB_ENDPOINT_XFER_BULK,
179
                .wMaxPacketSize        = 0x40,
180
            },{
181
                .bEndpointAddress      = USB_DIR_OUT | 0x02,
182
                .bmAttributes          = USB_ENDPOINT_XFER_BULK,
183
                .wMaxPacketSize        = 0x40,
184
            }
185
        }
186
    }
187
};
188
189
static const USBDescIface desc_iface_cdc[] = {
190
    {
191
        /* CDC Control Interface */
192
        .bInterfaceNumber              = 0,
193
        .bNumEndpoints                 = 1,
194
        .bInterfaceClass               = USB_CLASS_COMM,
195
        .bInterfaceSubClass            = USB_CDC_SUBCLASS_ETHERNET,
196
        .bInterfaceProtocol            = USB_CDC_PROTO_NONE,
197
        .iInterface                    = STRING_CONTROL,
198
        .ndesc                         = 3,
199
        .descs = (USBDescOther[]) {
200
            {
201
                /* Header Descriptor */
202
                .data = (uint8_t[]) {
203
                    0x05,                       /*  u8    bLength */
204
                    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
205
                    USB_CDC_HEADER_TYPE,        /*  u8    bDescriptorSubType */
206
                    0x10, 0x01,                 /*  le16  bcdCDC */
207
                },
208
            },{
209
                /* Union Descriptor */
210
                .data = (uint8_t[]) {
211
                    0x05,                       /*  u8    bLength */
212
                    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
213
                    USB_CDC_UNION_TYPE,         /*  u8    bDescriptorSubType */
214
                    0x00,                       /*  u8    bMasterInterface0 */
215
                    0x01,                       /*  u8    bSlaveInterface0 */
216
                },
217
            },{
218
                /* Ethernet Descriptor */
219
                .data = (uint8_t[]) {
220
                    0x0d,                       /*  u8    bLength */
221
                    USB_DT_CS_INTERFACE,        /*  u8    bDescriptorType */
222
                    USB_CDC_ETHERNET_TYPE,      /*  u8    bDescriptorSubType */
223
                    STRING_ETHADDR,             /*  u8    iMACAddress */
224
                    0x00, 0x00, 0x00, 0x00,     /*  le32  bmEthernetStatistics */
225
                    ETH_FRAME_LEN & 0xff,
226
                    ETH_FRAME_LEN >> 8,         /*  le16  wMaxSegmentSize */
227
                    0x00, 0x00,                 /*  le16  wNumberMCFilters */
228
                    0x00,                       /*  u8    bNumberPowerFilters */
229
                },
230
            },
231
        },
232
        .eps = (USBDescEndpoint[]) {
233
            {
234
                .bEndpointAddress      = USB_DIR_IN | 0x01,
235
                .bmAttributes          = USB_ENDPOINT_XFER_INT,
236
                .wMaxPacketSize        = STATUS_BYTECOUNT,
237
                .bInterval             = 1 << LOG2_STATUS_INTERVAL_MSEC,
238
            },
239
        }
240
    },{
241
        /* CDC Data Interface (off) */
242
        .bInterfaceNumber              = 1,
243
        .bAlternateSetting             = 0,
244
        .bNumEndpoints                 = 0,
245
        .bInterfaceClass               = USB_CLASS_CDC_DATA,
246
    },{
247
        /* CDC Data Interface */
248
        .bInterfaceNumber              = 1,
249
        .bAlternateSetting             = 1,
250
        .bNumEndpoints                 = 2,
251
        .bInterfaceClass               = USB_CLASS_CDC_DATA,
252
        .iInterface                    = STRING_DATA,
253
        .eps = (USBDescEndpoint[]) {
254
            {
255
                .bEndpointAddress      = USB_DIR_IN | 0x02,
256
                .bmAttributes          = USB_ENDPOINT_XFER_BULK,
257
                .wMaxPacketSize        = 0x40,
258
            },{
259
                .bEndpointAddress      = USB_DIR_OUT | 0x02,
260
                .bmAttributes          = USB_ENDPOINT_XFER_BULK,
261
                .wMaxPacketSize        = 0x40,
262
            }
263
        }
264
    }
265
};
266
267
static const USBDescDevice desc_device_net = {
268
    .bcdUSB                        = 0x0200,
269
    .bDeviceClass                  = USB_CLASS_COMM,
270
    .bMaxPacketSize0               = 0x40,
271
    .bNumConfigurations            = 2,
272
    .confs = (USBDescConfig[]) {
273
        {
274
            .bNumInterfaces        = 2,
275
            .bConfigurationValue   = DEV_RNDIS_CONFIG_VALUE,
276
            .iConfiguration        = STRING_RNDIS,
277
            .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
278
            .bMaxPower             = 0x32,
279
            .nif = ARRAY_SIZE(desc_iface_rndis),
280
            .ifs = desc_iface_rndis,
281
        },{
282
            .bNumInterfaces        = 2,
283
            .bConfigurationValue   = DEV_CONFIG_VALUE,
284
            .iConfiguration        = STRING_CDC,
285
            .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
286
            .bMaxPower             = 0x32,
287
            .nif = ARRAY_SIZE(desc_iface_cdc),
288
            .ifs = desc_iface_cdc,
289
        }
290
    },
291
};
292
293
static const USBDesc desc_net = {
294
    .id = {
295
        .idVendor          = RNDIS_VENDOR_NUM,
296
        .idProduct         = RNDIS_PRODUCT_NUM,
297
        .bcdDevice         = 0,
298
        .iManufacturer     = STRING_MANUFACTURER,
299
        .iProduct          = STRING_PRODUCT,
300
        .iSerialNumber     = STRING_SERIALNUMBER,
301
    },
302
    .full = &desc_device_net,
303
    .str  = usb_net_stringtable,
304
};
305
306
/*
307
 * RNDIS Definitions - in theory not specific to USB.
308
 */
309
#define RNDIS_MAXIMUM_FRAME_SIZE        1518
310
0
#define RNDIS_MAX_TOTAL_SIZE            1558
311
312
/* Remote NDIS Versions */
313
0
#define RNDIS_MAJOR_VERSION             1
314
0
#define RNDIS_MINOR_VERSION             0
315
316
/* Status Values */
317
0
#define RNDIS_STATUS_SUCCESS            0x00000000U /* Success */
318
#define RNDIS_STATUS_FAILURE            0xc0000001U /* Unspecified error */
319
#define RNDIS_STATUS_INVALID_DATA       0xc0010015U /* Invalid data */
320
0
#define RNDIS_STATUS_NOT_SUPPORTED      0xc00000bbU /* Unsupported request */
321
#define RNDIS_STATUS_MEDIA_CONNECT      0x4001000bU /* Device connected */
322
#define RNDIS_STATUS_MEDIA_DISCONNECT   0x4001000cU /* Device disconnected */
323
324
/* Message Set for Connectionless (802.3) Devices */
325
enum {
326
    RNDIS_PACKET_MSG            = 1,
327
    RNDIS_INITIALIZE_MSG        = 2,    /* Initialize device */
328
    RNDIS_HALT_MSG              = 3,
329
    RNDIS_QUERY_MSG             = 4,
330
    RNDIS_SET_MSG               = 5,
331
    RNDIS_RESET_MSG             = 6,
332
    RNDIS_INDICATE_STATUS_MSG   = 7,
333
    RNDIS_KEEPALIVE_MSG         = 8,
334
};
335
336
/* Message completion */
337
enum {
338
    RNDIS_INITIALIZE_CMPLT      = 0x80000002U,
339
    RNDIS_QUERY_CMPLT           = 0x80000004U,
340
    RNDIS_SET_CMPLT             = 0x80000005U,
341
    RNDIS_RESET_CMPLT           = 0x80000006U,
342
    RNDIS_KEEPALIVE_CMPLT       = 0x80000008U,
343
};
344
345
/* Device Flags */
346
enum {
347
    RNDIS_DF_CONNECTIONLESS     = 1,
348
    RNDIS_DF_CONNECTIONORIENTED = 2,
349
};
350
351
0
#define RNDIS_MEDIUM_802_3              0x00000000U
352
353
/* from drivers/net/sk98lin/h/skgepnmi.h */
354
#define OID_PNP_CAPABILITIES            0xfd010100
355
#define OID_PNP_SET_POWER               0xfd010101
356
#define OID_PNP_QUERY_POWER             0xfd010102
357
#define OID_PNP_ADD_WAKE_UP_PATTERN     0xfd010103
358
#define OID_PNP_REMOVE_WAKE_UP_PATTERN  0xfd010104
359
#define OID_PNP_ENABLE_WAKE_UP          0xfd010106
360
361
typedef uint32_t le32;
362
363
typedef struct rndis_init_msg_type {
364
    le32 MessageType;
365
    le32 MessageLength;
366
    le32 RequestID;
367
    le32 MajorVersion;
368
    le32 MinorVersion;
369
    le32 MaxTransferSize;
370
} rndis_init_msg_type;
371
372
typedef struct rndis_init_cmplt_type {
373
    le32 MessageType;
374
    le32 MessageLength;
375
    le32 RequestID;
376
    le32 Status;
377
    le32 MajorVersion;
378
    le32 MinorVersion;
379
    le32 DeviceFlags;
380
    le32 Medium;
381
    le32 MaxPacketsPerTransfer;
382
    le32 MaxTransferSize;
383
    le32 PacketAlignmentFactor;
384
    le32 AFListOffset;
385
    le32 AFListSize;
386
} rndis_init_cmplt_type;
387
388
typedef struct rndis_halt_msg_type {
389
    le32 MessageType;
390
    le32 MessageLength;
391
    le32 RequestID;
392
} rndis_halt_msg_type;
393
394
typedef struct rndis_query_msg_type {
395
    le32 MessageType;
396
    le32 MessageLength;
397
    le32 RequestID;
398
    le32 OID;
399
    le32 InformationBufferLength;
400
    le32 InformationBufferOffset;
401
    le32 DeviceVcHandle;
402
} rndis_query_msg_type;
403
404
typedef struct rndis_query_cmplt_type {
405
    le32 MessageType;
406
    le32 MessageLength;
407
    le32 RequestID;
408
    le32 Status;
409
    le32 InformationBufferLength;
410
    le32 InformationBufferOffset;
411
} rndis_query_cmplt_type;
412
413
typedef struct rndis_set_msg_type {
414
    le32 MessageType;
415
    le32 MessageLength;
416
    le32 RequestID;
417
    le32 OID;
418
    le32 InformationBufferLength;
419
    le32 InformationBufferOffset;
420
    le32 DeviceVcHandle;
421
} rndis_set_msg_type;
422
423
typedef struct rndis_set_cmplt_type {
424
    le32 MessageType;
425
    le32 MessageLength;
426
    le32 RequestID;
427
    le32 Status;
428
} rndis_set_cmplt_type;
429
430
typedef struct rndis_reset_msg_type {
431
    le32 MessageType;
432
    le32 MessageLength;
433
    le32 Reserved;
434
} rndis_reset_msg_type;
435
436
typedef struct rndis_reset_cmplt_type {
437
    le32 MessageType;
438
    le32 MessageLength;
439
    le32 Status;
440
    le32 AddressingReset;
441
} rndis_reset_cmplt_type;
442
443
typedef struct rndis_indicate_status_msg_type {
444
    le32 MessageType;
445
    le32 MessageLength;
446
    le32 Status;
447
    le32 StatusBufferLength;
448
    le32 StatusBufferOffset;
449
} rndis_indicate_status_msg_type;
450
451
typedef struct rndis_keepalive_msg_type {
452
    le32 MessageType;
453
    le32 MessageLength;
454
    le32 RequestID;
455
} rndis_keepalive_msg_type;
456
457
typedef struct rndis_keepalive_cmplt_type {
458
    le32 MessageType;
459
    le32 MessageLength;
460
    le32 RequestID;
461
    le32 Status;
462
} rndis_keepalive_cmplt_type;
463
464
struct rndis_packet_msg_type {
465
    le32 MessageType;
466
    le32 MessageLength;
467
    le32 DataOffset;
468
    le32 DataLength;
469
    le32 OOBDataOffset;
470
    le32 OOBDataLength;
471
    le32 NumOOBDataElements;
472
    le32 PerPacketInfoOffset;
473
    le32 PerPacketInfoLength;
474
    le32 VcHandle;
475
    le32 Reserved;
476
};
477
478
/* implementation specific */
479
enum rndis_state
480
{
481
    RNDIS_UNINITIALIZED,
482
    RNDIS_INITIALIZED,
483
    RNDIS_DATA_INITIALIZED,
484
};
485
486
/* from ndis.h */
487
enum ndis_oid {
488
    /* Required Object IDs (OIDs) */
489
    OID_GEN_SUPPORTED_LIST              = 0x00010101,
490
    OID_GEN_HARDWARE_STATUS             = 0x00010102,
491
    OID_GEN_MEDIA_SUPPORTED             = 0x00010103,
492
    OID_GEN_MEDIA_IN_USE                = 0x00010104,
493
    OID_GEN_MAXIMUM_LOOKAHEAD           = 0x00010105,
494
    OID_GEN_MAXIMUM_FRAME_SIZE          = 0x00010106,
495
    OID_GEN_LINK_SPEED                  = 0x00010107,
496
    OID_GEN_TRANSMIT_BUFFER_SPACE       = 0x00010108,
497
    OID_GEN_RECEIVE_BUFFER_SPACE        = 0x00010109,
498
    OID_GEN_TRANSMIT_BLOCK_SIZE         = 0x0001010a,
499
    OID_GEN_RECEIVE_BLOCK_SIZE          = 0x0001010b,
500
    OID_GEN_VENDOR_ID                   = 0x0001010c,
501
    OID_GEN_VENDOR_DESCRIPTION          = 0x0001010d,
502
    OID_GEN_CURRENT_PACKET_FILTER       = 0x0001010e,
503
    OID_GEN_CURRENT_LOOKAHEAD           = 0x0001010f,
504
    OID_GEN_DRIVER_VERSION              = 0x00010110,
505
    OID_GEN_MAXIMUM_TOTAL_SIZE          = 0x00010111,
506
    OID_GEN_PROTOCOL_OPTIONS            = 0x00010112,
507
    OID_GEN_MAC_OPTIONS                 = 0x00010113,
508
    OID_GEN_MEDIA_CONNECT_STATUS        = 0x00010114,
509
    OID_GEN_MAXIMUM_SEND_PACKETS        = 0x00010115,
510
    OID_GEN_VENDOR_DRIVER_VERSION       = 0x00010116,
511
    OID_GEN_SUPPORTED_GUIDS             = 0x00010117,
512
    OID_GEN_NETWORK_LAYER_ADDRESSES     = 0x00010118,
513
    OID_GEN_TRANSPORT_HEADER_OFFSET     = 0x00010119,
514
    OID_GEN_MACHINE_NAME                = 0x0001021a,
515
    OID_GEN_RNDIS_CONFIG_PARAMETER      = 0x0001021b,
516
    OID_GEN_VLAN_ID                     = 0x0001021c,
517
518
    /* Optional OIDs */
519
    OID_GEN_MEDIA_CAPABILITIES          = 0x00010201,
520
    OID_GEN_PHYSICAL_MEDIUM             = 0x00010202,
521
522
    /* Required statistics OIDs */
523
    OID_GEN_XMIT_OK                     = 0x00020101,
524
    OID_GEN_RCV_OK                      = 0x00020102,
525
    OID_GEN_XMIT_ERROR                  = 0x00020103,
526
    OID_GEN_RCV_ERROR                   = 0x00020104,
527
    OID_GEN_RCV_NO_BUFFER               = 0x00020105,
528
529
    /* Optional statistics OIDs */
530
    OID_GEN_DIRECTED_BYTES_XMIT         = 0x00020201,
531
    OID_GEN_DIRECTED_FRAMES_XMIT        = 0x00020202,
532
    OID_GEN_MULTICAST_BYTES_XMIT        = 0x00020203,
533
    OID_GEN_MULTICAST_FRAMES_XMIT       = 0x00020204,
534
    OID_GEN_BROADCAST_BYTES_XMIT        = 0x00020205,
535
    OID_GEN_BROADCAST_FRAMES_XMIT       = 0x00020206,
536
    OID_GEN_DIRECTED_BYTES_RCV          = 0x00020207,
537
    OID_GEN_DIRECTED_FRAMES_RCV         = 0x00020208,
538
    OID_GEN_MULTICAST_BYTES_RCV         = 0x00020209,
539
    OID_GEN_MULTICAST_FRAMES_RCV        = 0x0002020a,
540
    OID_GEN_BROADCAST_BYTES_RCV         = 0x0002020b,
541
    OID_GEN_BROADCAST_FRAMES_RCV        = 0x0002020c,
542
    OID_GEN_RCV_CRC_ERROR               = 0x0002020d,
543
    OID_GEN_TRANSMIT_QUEUE_LENGTH       = 0x0002020e,
544
    OID_GEN_GET_TIME_CAPS               = 0x0002020f,
545
    OID_GEN_GET_NETCARD_TIME            = 0x00020210,
546
    OID_GEN_NETCARD_LOAD                = 0x00020211,
547
    OID_GEN_DEVICE_PROFILE              = 0x00020212,
548
    OID_GEN_INIT_TIME_MS                = 0x00020213,
549
    OID_GEN_RESET_COUNTS                = 0x00020214,
550
    OID_GEN_MEDIA_SENSE_COUNTS          = 0x00020215,
551
    OID_GEN_FRIENDLY_NAME               = 0x00020216,
552
    OID_GEN_MINIPORT_INFO               = 0x00020217,
553
    OID_GEN_RESET_VERIFY_PARAMETERS     = 0x00020218,
554
555
    /* IEEE 802.3 (Ethernet) OIDs */
556
    OID_802_3_PERMANENT_ADDRESS         = 0x01010101,
557
    OID_802_3_CURRENT_ADDRESS           = 0x01010102,
558
    OID_802_3_MULTICAST_LIST            = 0x01010103,
559
    OID_802_3_MAXIMUM_LIST_SIZE         = 0x01010104,
560
    OID_802_3_MAC_OPTIONS               = 0x01010105,
561
    OID_802_3_RCV_ERROR_ALIGNMENT       = 0x01020101,
562
    OID_802_3_XMIT_ONE_COLLISION        = 0x01020102,
563
    OID_802_3_XMIT_MORE_COLLISIONS      = 0x01020103,
564
    OID_802_3_XMIT_DEFERRED             = 0x01020201,
565
    OID_802_3_XMIT_MAX_COLLISIONS       = 0x01020202,
566
    OID_802_3_RCV_OVERRUN               = 0x01020203,
567
    OID_802_3_XMIT_UNDERRUN             = 0x01020204,
568
    OID_802_3_XMIT_HEARTBEAT_FAILURE    = 0x01020205,
569
    OID_802_3_XMIT_TIMES_CRS_LOST       = 0x01020206,
570
    OID_802_3_XMIT_LATE_COLLISIONS      = 0x01020207,
571
};
572
573
static const uint32_t oid_supported_list[] =
574
{
575
    /* the general stuff */
576
    OID_GEN_SUPPORTED_LIST,
577
    OID_GEN_HARDWARE_STATUS,
578
    OID_GEN_MEDIA_SUPPORTED,
579
    OID_GEN_MEDIA_IN_USE,
580
    OID_GEN_MAXIMUM_FRAME_SIZE,
581
    OID_GEN_LINK_SPEED,
582
    OID_GEN_TRANSMIT_BLOCK_SIZE,
583
    OID_GEN_RECEIVE_BLOCK_SIZE,
584
    OID_GEN_VENDOR_ID,
585
    OID_GEN_VENDOR_DESCRIPTION,
586
    OID_GEN_VENDOR_DRIVER_VERSION,
587
    OID_GEN_CURRENT_PACKET_FILTER,
588
    OID_GEN_MAXIMUM_TOTAL_SIZE,
589
    OID_GEN_MEDIA_CONNECT_STATUS,
590
    OID_GEN_PHYSICAL_MEDIUM,
591
592
    /* the statistical stuff */
593
    OID_GEN_XMIT_OK,
594
    OID_GEN_RCV_OK,
595
    OID_GEN_XMIT_ERROR,
596
    OID_GEN_RCV_ERROR,
597
    OID_GEN_RCV_NO_BUFFER,
598
599
    /* IEEE 802.3 */
600
    /* the general stuff */
601
    OID_802_3_PERMANENT_ADDRESS,
602
    OID_802_3_CURRENT_ADDRESS,
603
    OID_802_3_MULTICAST_LIST,
604
    OID_802_3_MAC_OPTIONS,
605
    OID_802_3_MAXIMUM_LIST_SIZE,
606
607
    /* the statistical stuff */
608
    OID_802_3_RCV_ERROR_ALIGNMENT,
609
    OID_802_3_XMIT_ONE_COLLISION,
610
    OID_802_3_XMIT_MORE_COLLISIONS,
611
};
612
613
#define NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA     (1 << 0)
614
0
#define NDIS_MAC_OPTION_RECEIVE_SERIALIZED      (1 << 1)
615
#define NDIS_MAC_OPTION_TRANSFERS_NOT_PEND      (1 << 2)
616
#define NDIS_MAC_OPTION_NO_LOOPBACK             (1 << 3)
617
0
#define NDIS_MAC_OPTION_FULL_DUPLEX             (1 << 4)
618
#define NDIS_MAC_OPTION_EOTX_INDICATION         (1 << 5)
619
#define NDIS_MAC_OPTION_8021P_PRIORITY          (1 << 6)
620
621
struct rndis_response {
622
    QTAILQ_ENTRY(rndis_response) entries;
623
    uint32_t length;
624
    uint8_t buf[];
625
};
626
627
struct USBNetState {
628
    USBDevice dev;
629
630
    enum rndis_state rndis_state;
631
    uint32_t medium;
632
    uint32_t speed;
633
    uint32_t media_state;
634
    uint16_t filter;
635
    uint32_t vendorid;
636
637
    uint16_t connection;
638
639
    unsigned int out_ptr;
640
    uint8_t out_buf[2048];
641
642
    unsigned int in_ptr, in_len;
643
    uint8_t in_buf[2048];
644
645
    USBEndpoint *intr;
646
    USBEndpoint *bulk_in;
647
648
    char usbstring_mac[13];
649
    NICState *nic;
650
    NICConf conf;
651
    QTAILQ_HEAD(, rndis_response) rndis_resp;
652
};
653
654
#define TYPE_USB_NET "usb-net"
655
OBJECT_DECLARE_SIMPLE_TYPE(USBNetState, USB_NET)
656
657
static int is_rndis(USBNetState *s)
658
0
{
659
0
    return s->dev.config ?
660
0
            s->dev.config->bConfigurationValue == DEV_RNDIS_CONFIG_VALUE : 0;
661
0
}
662
663
static int ndis_query(USBNetState *s, uint32_t oid,
664
                      uint8_t *inbuf, unsigned int inlen, uint8_t *outbuf,
665
                      size_t outlen)
666
0
{
667
0
    unsigned int i;
668
669
0
    switch (oid) {
670
    /* general oids (table 4-1) */
671
    /* mandatory */
672
0
    case OID_GEN_SUPPORTED_LIST:
673
0
        for (i = 0; i < ARRAY_SIZE(oid_supported_list); i++) {
674
0
            stl_le_p(outbuf + (i * sizeof(le32)), oid_supported_list[i]);
675
0
        }
676
0
        return sizeof(oid_supported_list);
677
678
    /* mandatory */
679
0
    case OID_GEN_HARDWARE_STATUS:
680
0
        stl_le_p(outbuf, 0);
681
0
        return sizeof(le32);
682
683
    /* mandatory */
684
0
    case OID_GEN_MEDIA_SUPPORTED:
685
0
        stl_le_p(outbuf, s->medium);
686
0
        return sizeof(le32);
687
688
    /* mandatory */
689
0
    case OID_GEN_MEDIA_IN_USE:
690
0
        stl_le_p(outbuf, s->medium);
691
0
        return sizeof(le32);
692
693
    /* mandatory */
694
0
    case OID_GEN_MAXIMUM_FRAME_SIZE:
695
0
        stl_le_p(outbuf, ETH_FRAME_LEN);
696
0
        return sizeof(le32);
697
698
    /* mandatory */
699
0
    case OID_GEN_LINK_SPEED:
700
0
        stl_le_p(outbuf, s->speed);
701
0
        return sizeof(le32);
702
703
    /* mandatory */
704
0
    case OID_GEN_TRANSMIT_BLOCK_SIZE:
705
0
        stl_le_p(outbuf, ETH_FRAME_LEN);
706
0
        return sizeof(le32);
707
708
    /* mandatory */
709
0
    case OID_GEN_RECEIVE_BLOCK_SIZE:
710
0
        stl_le_p(outbuf, ETH_FRAME_LEN);
711
0
        return sizeof(le32);
712
713
    /* mandatory */
714
0
    case OID_GEN_VENDOR_ID:
715
0
        stl_le_p(outbuf, s->vendorid);
716
0
        return sizeof(le32);
717
718
    /* mandatory */
719
0
    case OID_GEN_VENDOR_DESCRIPTION:
720
0
        pstrcpy((char *)outbuf, outlen, "QEMU USB RNDIS Net");
721
0
        return strlen((char *)outbuf) + 1;
722
723
0
    case OID_GEN_VENDOR_DRIVER_VERSION:
724
0
        stl_le_p(outbuf, 1);
725
0
        return sizeof(le32);
726
727
    /* mandatory */
728
0
    case OID_GEN_CURRENT_PACKET_FILTER:
729
0
        stl_le_p(outbuf, s->filter);
730
0
        return sizeof(le32);
731
732
    /* mandatory */
733
0
    case OID_GEN_MAXIMUM_TOTAL_SIZE:
734
0
        stl_le_p(outbuf, RNDIS_MAX_TOTAL_SIZE);
735
0
        return sizeof(le32);
736
737
    /* mandatory */
738
0
    case OID_GEN_MEDIA_CONNECT_STATUS:
739
0
        stl_le_p(outbuf, s->media_state);
740
0
        return sizeof(le32);
741
742
0
    case OID_GEN_PHYSICAL_MEDIUM:
743
0
        stl_le_p(outbuf, 0);
744
0
        return sizeof(le32);
745
746
0
    case OID_GEN_MAC_OPTIONS:
747
0
        stl_le_p(outbuf, NDIS_MAC_OPTION_RECEIVE_SERIALIZED |
748
0
                 NDIS_MAC_OPTION_FULL_DUPLEX);
749
0
        return sizeof(le32);
750
751
    /* statistics OIDs (table 4-2) */
752
    /* mandatory */
753
0
    case OID_GEN_XMIT_OK:
754
0
        stl_le_p(outbuf, 0);
755
0
        return sizeof(le32);
756
757
    /* mandatory */
758
0
    case OID_GEN_RCV_OK:
759
0
        stl_le_p(outbuf, 0);
760
0
        return sizeof(le32);
761
762
    /* mandatory */
763
0
    case OID_GEN_XMIT_ERROR:
764
0
        stl_le_p(outbuf, 0);
765
0
        return sizeof(le32);
766
767
    /* mandatory */
768
0
    case OID_GEN_RCV_ERROR:
769
0
        stl_le_p(outbuf, 0);
770
0
        return sizeof(le32);
771
772
    /* mandatory */
773
0
    case OID_GEN_RCV_NO_BUFFER:
774
0
        stl_le_p(outbuf, 0);
775
0
        return sizeof(le32);
776
777
    /* ieee802.3 OIDs (table 4-3) */
778
    /* mandatory */
779
0
    case OID_802_3_PERMANENT_ADDRESS:
780
0
        memcpy(outbuf, s->conf.macaddr.a, 6);
781
0
        return 6;
782
783
    /* mandatory */
784
0
    case OID_802_3_CURRENT_ADDRESS:
785
0
        memcpy(outbuf, s->conf.macaddr.a, 6);
786
0
        return 6;
787
788
    /* mandatory */
789
0
    case OID_802_3_MULTICAST_LIST:
790
0
        stl_le_p(outbuf, 0xe0000000);
791
0
        return sizeof(le32);
792
793
    /* mandatory */
794
0
    case OID_802_3_MAXIMUM_LIST_SIZE:
795
0
        stl_le_p(outbuf, 1);
796
0
        return sizeof(le32);
797
798
0
    case OID_802_3_MAC_OPTIONS:
799
0
        return 0;
800
801
    /* ieee802.3 statistics OIDs (table 4-4) */
802
    /* mandatory */
803
0
    case OID_802_3_RCV_ERROR_ALIGNMENT:
804
0
        stl_le_p(outbuf, 0);
805
0
        return sizeof(le32);
806
807
    /* mandatory */
808
0
    case OID_802_3_XMIT_ONE_COLLISION:
809
0
        stl_le_p(outbuf, 0);
810
0
        return sizeof(le32);
811
812
    /* mandatory */
813
0
    case OID_802_3_XMIT_MORE_COLLISIONS:
814
0
        stl_le_p(outbuf, 0);
815
0
        return sizeof(le32);
816
817
0
    default:
818
0
        fprintf(stderr, "usbnet: unknown OID 0x%08x\n", oid);
819
0
        return 0;
820
0
    }
821
0
    return -1;
822
0
}
823
824
static int ndis_set(USBNetState *s, uint32_t oid,
825
                uint8_t *inbuf, unsigned int inlen)
826
0
{
827
0
    switch (oid) {
828
0
    case OID_GEN_CURRENT_PACKET_FILTER:
829
0
        s->filter = ldl_le_p(inbuf);
830
0
        if (s->filter) {
831
0
            s->rndis_state = RNDIS_DATA_INITIALIZED;
832
0
        } else {
833
0
            s->rndis_state = RNDIS_INITIALIZED;
834
0
        }
835
0
        return 0;
836
837
0
    case OID_802_3_MULTICAST_LIST:
838
0
        return 0;
839
0
    }
840
0
    return -1;
841
0
}
842
843
static int rndis_get_response(USBNetState *s, uint8_t *buf)
844
0
{
845
0
    int ret = 0;
846
0
    struct rndis_response *r = s->rndis_resp.tqh_first;
847
848
0
    if (!r)
849
0
        return ret;
850
851
0
    QTAILQ_REMOVE(&s->rndis_resp, r, entries);
852
0
    ret = r->length;
853
0
    memcpy(buf, r->buf, r->length);
854
0
    g_free(r);
855
856
0
    return ret;
857
0
}
858
859
static void *rndis_queue_response(USBNetState *s, unsigned int length)
860
0
{
861
0
    struct rndis_response *r =
862
0
            g_malloc0(sizeof(struct rndis_response) + length);
863
864
0
    if (QTAILQ_EMPTY(&s->rndis_resp)) {
865
0
        usb_wakeup(s->intr, 0);
866
0
    }
867
868
0
    QTAILQ_INSERT_TAIL(&s->rndis_resp, r, entries);
869
0
    r->length = length;
870
871
0
    return &r->buf[0];
872
0
}
873
874
static void rndis_clear_responsequeue(USBNetState *s)
875
0
{
876
0
    struct rndis_response *r;
877
878
0
    while ((r = s->rndis_resp.tqh_first)) {
879
0
        QTAILQ_REMOVE(&s->rndis_resp, r, entries);
880
0
        g_free(r);
881
0
    }
882
0
}
883
884
static int rndis_init_response(USBNetState *s, rndis_init_msg_type *buf)
885
0
{
886
0
    rndis_init_cmplt_type *resp =
887
0
            rndis_queue_response(s, sizeof(rndis_init_cmplt_type));
888
889
0
    if (!resp)
890
0
        return USB_RET_STALL;
891
892
0
    resp->MessageType = cpu_to_le32(RNDIS_INITIALIZE_CMPLT);
893
0
    resp->MessageLength = cpu_to_le32(sizeof(rndis_init_cmplt_type));
894
0
    resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
895
0
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
896
0
    resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
897
0
    resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
898
0
    resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
899
0
    resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
900
0
    resp->MaxPacketsPerTransfer = cpu_to_le32(1);
901
0
    resp->MaxTransferSize = cpu_to_le32(ETH_FRAME_LEN +
902
0
                    sizeof(struct rndis_packet_msg_type) + 22);
903
0
    resp->PacketAlignmentFactor = cpu_to_le32(0);
904
0
    resp->AFListOffset = cpu_to_le32(0);
905
0
    resp->AFListSize = cpu_to_le32(0);
906
0
    return 0;
907
0
}
908
909
static int rndis_query_response(USBNetState *s,
910
                rndis_query_msg_type *buf, unsigned int length)
911
0
{
912
0
    rndis_query_cmplt_type *resp;
913
    /* oid_supported_list is the largest data reply */
914
0
    uint8_t infobuf[sizeof(oid_supported_list)];
915
0
    uint32_t bufoffs, buflen;
916
0
    int infobuflen;
917
0
    unsigned int resplen;
918
919
0
    bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
920
0
    buflen = le32_to_cpu(buf->InformationBufferLength);
921
0
    if (buflen > length || bufoffs >= length || bufoffs + buflen > length) {
922
0
        return USB_RET_STALL;
923
0
    }
924
925
0
    infobuflen = ndis_query(s, le32_to_cpu(buf->OID),
926
0
                            bufoffs + (uint8_t *) buf, buflen, infobuf,
927
0
                            sizeof(infobuf));
928
0
    resplen = sizeof(rndis_query_cmplt_type) +
929
0
            ((infobuflen < 0) ? 0 : infobuflen);
930
0
    resp = rndis_queue_response(s, resplen);
931
0
    if (!resp)
932
0
        return USB_RET_STALL;
933
934
0
    resp->MessageType = cpu_to_le32(RNDIS_QUERY_CMPLT);
935
0
    resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
936
0
    resp->MessageLength = cpu_to_le32(resplen);
937
938
0
    if (infobuflen < 0) {
939
        /* OID not supported */
940
0
        resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
941
0
        resp->InformationBufferLength = cpu_to_le32(0);
942
0
        resp->InformationBufferOffset = cpu_to_le32(0);
943
0
        return 0;
944
0
    }
945
946
0
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
947
0
    resp->InformationBufferOffset =
948
0
            cpu_to_le32(infobuflen ? sizeof(rndis_query_cmplt_type) - 8 : 0);
949
0
    resp->InformationBufferLength = cpu_to_le32(infobuflen);
950
0
    memcpy(resp + 1, infobuf, infobuflen);
951
952
0
    return 0;
953
0
}
954
955
static int rndis_set_response(USBNetState *s,
956
                rndis_set_msg_type *buf, unsigned int length)
957
0
{
958
0
    rndis_set_cmplt_type *resp =
959
0
            rndis_queue_response(s, sizeof(rndis_set_cmplt_type));
960
0
    uint32_t bufoffs, buflen;
961
0
    int ret;
962
963
0
    if (!resp)
964
0
        return USB_RET_STALL;
965
966
0
    bufoffs = le32_to_cpu(buf->InformationBufferOffset) + 8;
967
0
    buflen = le32_to_cpu(buf->InformationBufferLength);
968
0
    if (buflen > length || bufoffs >= length || bufoffs + buflen > length) {
969
0
        return USB_RET_STALL;
970
0
    }
971
972
0
    ret = ndis_set(s, le32_to_cpu(buf->OID),
973
0
                    bufoffs + (uint8_t *) buf, buflen);
974
0
    resp->MessageType = cpu_to_le32(RNDIS_SET_CMPLT);
975
0
    resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
976
0
    resp->MessageLength = cpu_to_le32(sizeof(rndis_set_cmplt_type));
977
0
    if (ret < 0) {
978
        /* OID not supported */
979
0
        resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
980
0
        return 0;
981
0
    }
982
0
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
983
984
0
    return 0;
985
0
}
986
987
static int rndis_reset_response(USBNetState *s, rndis_reset_msg_type *buf)
988
0
{
989
0
    rndis_reset_cmplt_type *resp =
990
0
            rndis_queue_response(s, sizeof(rndis_reset_cmplt_type));
991
992
0
    if (!resp)
993
0
        return USB_RET_STALL;
994
995
0
    resp->MessageType = cpu_to_le32(RNDIS_RESET_CMPLT);
996
0
    resp->MessageLength = cpu_to_le32(sizeof(rndis_reset_cmplt_type));
997
0
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
998
0
    resp->AddressingReset = cpu_to_le32(1); /* reset information */
999
1000
0
    return 0;
1001
0
}
1002
1003
static int rndis_keepalive_response(USBNetState *s,
1004
                rndis_keepalive_msg_type *buf)
1005
0
{
1006
0
    rndis_keepalive_cmplt_type *resp =
1007
0
            rndis_queue_response(s, sizeof(rndis_keepalive_cmplt_type));
1008
1009
0
    if (!resp)
1010
0
        return USB_RET_STALL;
1011
1012
0
    resp->MessageType = cpu_to_le32(RNDIS_KEEPALIVE_CMPLT);
1013
0
    resp->MessageLength = cpu_to_le32(sizeof(rndis_keepalive_cmplt_type));
1014
0
    resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
1015
0
    resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
1016
1017
0
    return 0;
1018
0
}
1019
1020
/* Prepare to receive the next packet */
1021
static void usb_net_reset_in_buf(USBNetState *s)
1022
0
{
1023
0
    s->in_ptr = s->in_len = 0;
1024
0
    qemu_flush_queued_packets(qemu_get_queue(s->nic));
1025
0
}
1026
1027
static int rndis_parse(USBNetState *s, uint8_t *data, int length)
1028
0
{
1029
0
    uint32_t msg_type = ldl_le_p(data);
1030
1031
0
    switch (msg_type) {
1032
0
    case RNDIS_INITIALIZE_MSG:
1033
0
        s->rndis_state = RNDIS_INITIALIZED;
1034
0
        return rndis_init_response(s, (rndis_init_msg_type *) data);
1035
1036
0
    case RNDIS_HALT_MSG:
1037
0
        s->rndis_state = RNDIS_UNINITIALIZED;
1038
0
        return 0;
1039
1040
0
    case RNDIS_QUERY_MSG:
1041
0
        return rndis_query_response(s, (rndis_query_msg_type *) data, length);
1042
1043
0
    case RNDIS_SET_MSG:
1044
0
        return rndis_set_response(s, (rndis_set_msg_type *) data, length);
1045
1046
0
    case RNDIS_RESET_MSG:
1047
0
        rndis_clear_responsequeue(s);
1048
0
        s->out_ptr = 0;
1049
0
        usb_net_reset_in_buf(s);
1050
0
        return rndis_reset_response(s, (rndis_reset_msg_type *) data);
1051
1052
0
    case RNDIS_KEEPALIVE_MSG:
1053
        /* For USB: host does this every 5 seconds */
1054
0
        return rndis_keepalive_response(s, (rndis_keepalive_msg_type *) data);
1055
0
    }
1056
1057
0
    return USB_RET_STALL;
1058
0
}
1059
1060
static void usb_net_handle_reset(USBDevice *dev)
1061
0
{
1062
0
}
1063
1064
static void usb_net_handle_control(USBDevice *dev, USBPacket *p,
1065
               int request, int value, int index, int length, uint8_t *data)
1066
0
{
1067
0
    USBNetState *s = (USBNetState *) dev;
1068
0
    int ret;
1069
1070
0
    ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
1071
0
    if (ret >= 0) {
1072
0
        return;
1073
0
    }
1074
1075
0
    switch(request) {
1076
0
    case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:
1077
0
        if (!is_rndis(s) || value || index != 0) {
1078
0
            goto fail;
1079
0
        }
1080
#ifdef TRAFFIC_DEBUG
1081
        {
1082
            unsigned int i;
1083
            fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");
1084
            for (i = 0; i < length; i++) {
1085
                if (!(i & 15))
1086
                    fprintf(stderr, "\n%04x:", i);
1087
                fprintf(stderr, " %02x", data[i]);
1088
            }
1089
            fprintf(stderr, "\n\n");
1090
        }
1091
#endif
1092
0
        ret = rndis_parse(s, data, length);
1093
0
        if (ret < 0) {
1094
0
            p->status = ret;
1095
0
        }
1096
0
        break;
1097
1098
0
    case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:
1099
0
        if (!is_rndis(s) || value || index != 0) {
1100
0
            goto fail;
1101
0
        }
1102
0
        p->actual_length = rndis_get_response(s, data);
1103
0
        if (p->actual_length == 0) {
1104
0
            data[0] = 0;
1105
0
            p->actual_length = 1;
1106
0
        }
1107
#ifdef TRAFFIC_DEBUG
1108
        {
1109
            unsigned int i;
1110
            fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");
1111
            for (i = 0; i < p->actual_length; i++) {
1112
                if (!(i & 15))
1113
                    fprintf(stderr, "\n%04x:", i);
1114
                fprintf(stderr, " %02x", data[i]);
1115
            }
1116
            fprintf(stderr, "\n\n");
1117
        }
1118
#endif
1119
0
        break;
1120
1121
0
    case ClassInterfaceOutRequest | USB_CDC_SET_ETHERNET_PACKET_FILTER:
1122
0
        if (is_rndis(s)) {
1123
0
            goto fail;
1124
0
        }
1125
0
        break;
1126
1127
0
    default:
1128
0
    fail:
1129
0
        fprintf(stderr, "usbnet: failed control transaction: "
1130
0
                        "request 0x%x value 0x%x index 0x%x length 0x%x\n",
1131
0
                        request, value, index, length);
1132
0
        p->status = USB_RET_STALL;
1133
0
        break;
1134
0
    }
1135
0
}
1136
1137
static void usb_net_handle_statusin(USBNetState *s, USBPacket *p)
1138
0
{
1139
0
    le32 rbuf[2];
1140
0
    uint16_t ebuf[4];
1141
1142
0
    if (p->iov.size < 8) {
1143
0
        p->status = USB_RET_STALL;
1144
0
        return;
1145
0
    }
1146
1147
0
    if (is_rndis(s)) {
1148
0
        rbuf[0] = cpu_to_le32(1);
1149
0
        rbuf[1] = cpu_to_le32(0);
1150
0
        usb_packet_copy(p, rbuf, 8);
1151
0
        if (!s->rndis_resp.tqh_first) {
1152
0
            p->status = USB_RET_NAK;
1153
0
        }
1154
0
    } else {
1155
0
        ebuf[0] =
1156
0
            cpu_to_be16(ClassInterfaceRequest | USB_CDC_NETWORK_CONNECTION);
1157
0
        ebuf[1] = cpu_to_le16(s->connection);
1158
0
        ebuf[2] = cpu_to_le16(1);
1159
0
        ebuf[3] = cpu_to_le16(0);
1160
0
        usb_packet_copy(p, ebuf, 8);
1161
0
    }
1162
1163
#ifdef TRAFFIC_DEBUG
1164
    fprintf(stderr, "usbnet: interrupt poll len %zu return %d",
1165
            p->iov.size, p->status);
1166
    iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", p->status);
1167
#endif
1168
0
}
1169
1170
static void usb_net_handle_datain(USBNetState *s, USBPacket *p)
1171
0
{
1172
0
    int len;
1173
1174
0
    if (s->in_ptr > s->in_len) {
1175
0
        usb_net_reset_in_buf(s);
1176
0
        p->status = USB_RET_NAK;
1177
0
        return;
1178
0
    }
1179
0
    if (!s->in_len) {
1180
0
        p->status = USB_RET_NAK;
1181
0
        return;
1182
0
    }
1183
0
    len = s->in_len - s->in_ptr;
1184
0
    if (len > p->iov.size) {
1185
0
        len = p->iov.size;
1186
0
    }
1187
0
    usb_packet_copy(p, &s->in_buf[s->in_ptr], len);
1188
0
    s->in_ptr += len;
1189
0
    if (s->in_ptr >= s->in_len &&
1190
0
                    (is_rndis(s) || (s->in_len & (64 - 1)) || !len)) {
1191
        /* no short packet necessary */
1192
0
        usb_net_reset_in_buf(s);
1193
0
    }
1194
1195
#ifdef TRAFFIC_DEBUG
1196
    fprintf(stderr, "usbnet: data in len %zu return %d", p->iov.size, len);
1197
    iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", len);
1198
#endif
1199
0
}
1200
1201
static void usb_net_handle_dataout(USBNetState *s, USBPacket *p)
1202
0
{
1203
0
    int sz = sizeof(s->out_buf) - s->out_ptr;
1204
0
    struct rndis_packet_msg_type *msg =
1205
0
            (struct rndis_packet_msg_type *) s->out_buf;
1206
0
    uint32_t len;
1207
1208
#ifdef TRAFFIC_DEBUG
1209
    fprintf(stderr, "usbnet: data out len %zu\n", p->iov.size);
1210
    iov_hexdump(p->iov.iov, p->iov.niov, stderr, "usbnet", p->iov.size);
1211
#endif
1212
1213
0
    if (sz > p->iov.size) {
1214
0
        sz = p->iov.size;
1215
0
    }
1216
0
    usb_packet_copy(p, &s->out_buf[s->out_ptr], sz);
1217
0
    s->out_ptr += sz;
1218
1219
0
    if (!is_rndis(s)) {
1220
0
        if (p->iov.size % 64 || p->iov.size == 0) {
1221
0
            qemu_send_packet(qemu_get_queue(s->nic), s->out_buf, s->out_ptr);
1222
0
            s->out_ptr = 0;
1223
0
        }
1224
0
        return;
1225
0
    }
1226
0
    len = le32_to_cpu(msg->MessageLength);
1227
0
    if (s->out_ptr < 8 || s->out_ptr < len) {
1228
0
        return;
1229
0
    }
1230
0
    if (le32_to_cpu(msg->MessageType) == RNDIS_PACKET_MSG) {
1231
0
        uint32_t offs = 8 + le32_to_cpu(msg->DataOffset);
1232
0
        uint32_t size = le32_to_cpu(msg->DataLength);
1233
0
        if (offs < len && size < len && offs + size <= len) {
1234
0
            qemu_send_packet(qemu_get_queue(s->nic), s->out_buf + offs, size);
1235
0
        }
1236
0
    }
1237
0
    s->out_ptr -= len;
1238
0
    memmove(s->out_buf, &s->out_buf[len], s->out_ptr);
1239
0
}
1240
1241
static void usb_net_handle_data(USBDevice *dev, USBPacket *p)
1242
0
{
1243
0
    USBNetState *s = (USBNetState *) dev;
1244
1245
0
    switch(p->pid) {
1246
0
    case USB_TOKEN_IN:
1247
0
        switch (p->ep->nr) {
1248
0
        case 1:
1249
0
            usb_net_handle_statusin(s, p);
1250
0
            break;
1251
1252
0
        case 2:
1253
0
            usb_net_handle_datain(s, p);
1254
0
            break;
1255
1256
0
        default:
1257
0
            goto fail;
1258
0
        }
1259
0
        break;
1260
1261
0
    case USB_TOKEN_OUT:
1262
0
        switch (p->ep->nr) {
1263
0
        case 2:
1264
0
            usb_net_handle_dataout(s, p);
1265
0
            break;
1266
1267
0
        default:
1268
0
            goto fail;
1269
0
        }
1270
0
        break;
1271
1272
0
    default:
1273
0
    fail:
1274
0
        p->status = USB_RET_STALL;
1275
0
        break;
1276
0
    }
1277
1278
0
    if (p->status == USB_RET_STALL) {
1279
0
        fprintf(stderr, "usbnet: failed data transaction: "
1280
0
                        "pid 0x%x ep 0x%x len 0x%zx\n",
1281
0
                        p->pid, p->ep->nr, p->iov.size);
1282
0
    }
1283
0
}
1284
1285
static ssize_t usbnet_receive(NetClientState *nc, const uint8_t *buf, size_t size)
1286
0
{
1287
0
    USBNetState *s = qemu_get_nic_opaque(nc);
1288
0
    uint8_t *in_buf = s->in_buf;
1289
0
    size_t total_size = size;
1290
1291
0
    if (!s->dev.config) {
1292
0
        return -1;
1293
0
    }
1294
1295
0
    if (is_rndis(s)) {
1296
0
        if (s->rndis_state != RNDIS_DATA_INITIALIZED) {
1297
0
            return -1;
1298
0
        }
1299
0
        total_size += sizeof(struct rndis_packet_msg_type);
1300
0
    }
1301
0
    if (total_size > sizeof(s->in_buf)) {
1302
0
        return -1;
1303
0
    }
1304
1305
    /* Only accept packet if input buffer is empty */
1306
0
    if (s->in_len > 0) {
1307
0
        return 0;
1308
0
    }
1309
1310
0
    if (is_rndis(s)) {
1311
0
        struct rndis_packet_msg_type *msg;
1312
1313
0
        msg = (struct rndis_packet_msg_type *)in_buf;
1314
0
        memset(msg, 0, sizeof(struct rndis_packet_msg_type));
1315
0
        msg->MessageType = cpu_to_le32(RNDIS_PACKET_MSG);
1316
0
        msg->MessageLength = cpu_to_le32(size + sizeof(*msg));
1317
0
        msg->DataOffset = cpu_to_le32(sizeof(*msg) - 8);
1318
0
        msg->DataLength = cpu_to_le32(size);
1319
        /* msg->OOBDataOffset;
1320
         * msg->OOBDataLength;
1321
         * msg->NumOOBDataElements;
1322
         * msg->PerPacketInfoOffset;
1323
         * msg->PerPacketInfoLength;
1324
         * msg->VcHandle;
1325
         * msg->Reserved;
1326
         */
1327
0
        in_buf += sizeof(*msg);
1328
0
    }
1329
1330
0
    memcpy(in_buf, buf, size);
1331
0
    s->in_len = total_size;
1332
0
    s->in_ptr = 0;
1333
0
    usb_wakeup(s->bulk_in, 0);
1334
0
    return size;
1335
0
}
1336
1337
static void usbnet_cleanup(NetClientState *nc)
1338
0
{
1339
0
    USBNetState *s = qemu_get_nic_opaque(nc);
1340
1341
0
    s->nic = NULL;
1342
0
}
1343
1344
static void usb_net_unrealize(USBDevice *dev)
1345
0
{
1346
0
    USBNetState *s = (USBNetState *) dev;
1347
1348
    /* TODO: remove the nd_table[] entry */
1349
0
    rndis_clear_responsequeue(s);
1350
0
    qemu_del_nic(s->nic);
1351
0
}
1352
1353
static NetClientInfo net_usbnet_info = {
1354
    .type = NET_CLIENT_DRIVER_NIC,
1355
    .size = sizeof(NICState),
1356
    .receive = usbnet_receive,
1357
    .cleanup = usbnet_cleanup,
1358
};
1359
1360
static void usb_net_realize(USBDevice *dev, Error **errp)
1361
0
{
1362
0
    USBNetState *s = USB_NET(dev);
1363
1364
0
    usb_desc_create_serial(dev);
1365
0
    usb_desc_init(dev);
1366
1367
0
    s->rndis_state = RNDIS_UNINITIALIZED;
1368
0
    QTAILQ_INIT(&s->rndis_resp);
1369
1370
0
    s->medium = 0;      /* NDIS_MEDIUM_802_3 */
1371
0
    s->speed = 1000000; /* 100MBps, in 100Bps units */
1372
0
    s->media_state = 0; /* NDIS_MEDIA_STATE_CONNECTED */;
1373
0
    s->filter = 0;
1374
0
    s->vendorid = 0x1234;
1375
0
    s->connection = 1;  /* Connected */
1376
0
    s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
1377
0
    s->bulk_in = usb_ep_get(dev, USB_TOKEN_IN, 2);
1378
1379
0
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
1380
0
    s->nic = qemu_new_nic(&net_usbnet_info, &s->conf,
1381
0
                          object_get_typename(OBJECT(s)), s->dev.qdev.id,
1382
0
                          &s->dev.qdev.mem_reentrancy_guard, s);
1383
0
    qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
1384
0
    snprintf(s->usbstring_mac, sizeof(s->usbstring_mac),
1385
0
             "%02x%02x%02x%02x%02x%02x",
1386
0
             s->conf.macaddr.a[0],
1387
0
             s->conf.macaddr.a[1],
1388
0
             s->conf.macaddr.a[2],
1389
0
             s->conf.macaddr.a[3],
1390
0
             s->conf.macaddr.a[4],
1391
0
             s->conf.macaddr.a[5]);
1392
0
    usb_desc_set_string(dev, STRING_ETHADDR, s->usbstring_mac);
1393
0
}
1394
1395
static void usb_net_instance_init(Object *obj)
1396
0
{
1397
0
    USBDevice *dev = USB_DEVICE(obj);
1398
0
    USBNetState *s = USB_NET(dev);
1399
1400
0
    device_add_bootindex_property(obj, &s->conf.bootindex,
1401
0
                                  "bootindex", "/ethernet-phy@0",
1402
0
                                  &dev->qdev);
1403
0
}
1404
1405
static const VMStateDescription vmstate_usb_net = {
1406
    .name = "usb-net",
1407
    .unmigratable = 1,
1408
};
1409
1410
static const Property net_properties[] = {
1411
    DEFINE_NIC_PROPERTIES(USBNetState, conf),
1412
};
1413
1414
static void usb_net_class_initfn(ObjectClass *klass, const void *data)
1415
61
{
1416
61
    DeviceClass *dc = DEVICE_CLASS(klass);
1417
61
    USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1418
1419
61
    uc->realize        = usb_net_realize;
1420
61
    uc->product_desc   = "QEMU USB Network Interface";
1421
61
    uc->usb_desc       = &desc_net;
1422
61
    uc->handle_reset   = usb_net_handle_reset;
1423
61
    uc->handle_control = usb_net_handle_control;
1424
61
    uc->handle_data    = usb_net_handle_data;
1425
61
    uc->unrealize      = usb_net_unrealize;
1426
61
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1427
61
    dc->fw_name = "network";
1428
61
    dc->vmsd = &vmstate_usb_net;
1429
61
    device_class_set_props(dc, net_properties);
1430
61
}
1431
1432
static const TypeInfo net_info = {
1433
    .name          = TYPE_USB_NET,
1434
    .parent        = TYPE_USB_DEVICE,
1435
    .instance_size = sizeof(USBNetState),
1436
    .class_init    = usb_net_class_initfn,
1437
    .instance_init = usb_net_instance_init,
1438
};
1439
1440
static void usb_net_register_types(void)
1441
61
{
1442
61
    type_register_static(&net_info);
1443
61
}
1444
1445
type_init(usb_net_register_types)