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