usb-net.c 43 KB

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