dev-network.c 44 KB

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