usb-net.c 43 KB

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