dev-network.c 45 KB

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