2
0

dev-network.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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 "qemu/queue.h"
  30. #include "qemu/config-file.h"
  31. #include "sysemu/sysemu.h"
  32. #include "qemu/iov.h"
  33. /*#define TRAFFIC_DEBUG*/
  34. /* Thanks to NetChip Technologies for donating this product ID.
  35. * It's for devices with only CDC Ethernet configurations.
  36. */
  37. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  38. #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
  39. /* For hardware that can talk RNDIS and either of the above protocols,
  40. * use this ID ... the windows INF files will know it.
  41. */
  42. #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
  43. #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
  44. enum usbstring_idx {
  45. STRING_MANUFACTURER = 1,
  46. STRING_PRODUCT,
  47. STRING_ETHADDR,
  48. STRING_DATA,
  49. STRING_CONTROL,
  50. STRING_RNDIS_CONTROL,
  51. STRING_CDC,
  52. STRING_SUBSET,
  53. STRING_RNDIS,
  54. STRING_SERIALNUMBER,
  55. };
  56. #define DEV_CONFIG_VALUE 1 /* CDC or a subset */
  57. #define DEV_RNDIS_CONFIG_VALUE 2 /* RNDIS; optional */
  58. #define USB_CDC_SUBCLASS_ACM 0x02
  59. #define USB_CDC_SUBCLASS_ETHERNET 0x06
  60. #define USB_CDC_PROTO_NONE 0
  61. #define USB_CDC_ACM_PROTO_VENDOR 0xff
  62. #define USB_CDC_HEADER_TYPE 0x00 /* header_desc */
  63. #define USB_CDC_CALL_MANAGEMENT_TYPE 0x01 /* call_mgmt_descriptor */
  64. #define USB_CDC_ACM_TYPE 0x02 /* acm_descriptor */
  65. #define USB_CDC_UNION_TYPE 0x06 /* union_desc */
  66. #define USB_CDC_ETHERNET_TYPE 0x0f /* ether_desc */
  67. #define USB_CDC_SEND_ENCAPSULATED_COMMAND 0x00
  68. #define USB_CDC_GET_ENCAPSULATED_RESPONSE 0x01
  69. #define USB_CDC_REQ_SET_LINE_CODING 0x20
  70. #define USB_CDC_REQ_GET_LINE_CODING 0x21
  71. #define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
  72. #define USB_CDC_REQ_SEND_BREAK 0x23
  73. #define USB_CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40
  74. #define USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41
  75. #define USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42
  76. #define USB_CDC_SET_ETHERNET_PACKET_FILTER 0x43
  77. #define USB_CDC_GET_ETHERNET_STATISTIC 0x44
  78. #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
  79. #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
  80. #define ETH_FRAME_LEN 1514 /* Max. octets in frame sans FCS */
  81. static const USBDescStrings usb_net_stringtable = {
  82. [STRING_MANUFACTURER] = "QEMU",
  83. [STRING_PRODUCT] = "RNDIS/QEMU USB Network Device",
  84. [STRING_ETHADDR] = "400102030405",
  85. [STRING_DATA] = "QEMU USB Net Data Interface",
  86. [STRING_CONTROL] = "QEMU USB Net Control Interface",
  87. [STRING_RNDIS_CONTROL] = "QEMU USB Net RNDIS Control Interface",
  88. [STRING_CDC] = "QEMU USB Net CDC",
  89. [STRING_SUBSET] = "QEMU USB Net Subset",
  90. [STRING_RNDIS] = "QEMU USB Net RNDIS",
  91. [STRING_SERIALNUMBER] = "1",
  92. };
  93. static const USBDescIface desc_iface_rndis[] = {
  94. {
  95. /* RNDIS Control Interface */
  96. .bInterfaceNumber = 0,
  97. .bNumEndpoints = 1,
  98. .bInterfaceClass = USB_CLASS_COMM,
  99. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  100. .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
  101. .iInterface = STRING_RNDIS_CONTROL,
  102. .ndesc = 4,
  103. .descs = (USBDescOther[]) {
  104. {
  105. /* Header Descriptor */
  106. .data = (uint8_t[]) {
  107. 0x05, /* u8 bLength */
  108. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  109. USB_CDC_HEADER_TYPE, /* u8 bDescriptorSubType */
  110. 0x10, 0x01, /* le16 bcdCDC */
  111. },
  112. },{
  113. /* Call Management Descriptor */
  114. .data = (uint8_t[]) {
  115. 0x05, /* u8 bLength */
  116. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  117. USB_CDC_CALL_MANAGEMENT_TYPE, /* u8 bDescriptorSubType */
  118. 0x00, /* u8 bmCapabilities */
  119. 0x01, /* u8 bDataInterface */
  120. },
  121. },{
  122. /* ACM Descriptor */
  123. .data = (uint8_t[]) {
  124. 0x04, /* u8 bLength */
  125. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  126. USB_CDC_ACM_TYPE, /* u8 bDescriptorSubType */
  127. 0x00, /* u8 bmCapabilities */
  128. },
  129. },{
  130. /* Union Descriptor */
  131. .data = (uint8_t[]) {
  132. 0x05, /* u8 bLength */
  133. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  134. USB_CDC_UNION_TYPE, /* u8 bDescriptorSubType */
  135. 0x00, /* u8 bMasterInterface0 */
  136. 0x01, /* u8 bSlaveInterface0 */
  137. },
  138. },
  139. },
  140. .eps = (USBDescEndpoint[]) {
  141. {
  142. .bEndpointAddress = USB_DIR_IN | 0x01,
  143. .bmAttributes = USB_ENDPOINT_XFER_INT,
  144. .wMaxPacketSize = STATUS_BYTECOUNT,
  145. .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
  146. },
  147. }
  148. },{
  149. /* RNDIS Data Interface */
  150. .bInterfaceNumber = 1,
  151. .bNumEndpoints = 2,
  152. .bInterfaceClass = USB_CLASS_CDC_DATA,
  153. .iInterface = STRING_DATA,
  154. .eps = (USBDescEndpoint[]) {
  155. {
  156. .bEndpointAddress = USB_DIR_IN | 0x02,
  157. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  158. .wMaxPacketSize = 0x40,
  159. },{
  160. .bEndpointAddress = USB_DIR_OUT | 0x02,
  161. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  162. .wMaxPacketSize = 0x40,
  163. }
  164. }
  165. }
  166. };
  167. static const USBDescIface desc_iface_cdc[] = {
  168. {
  169. /* CDC Control Interface */
  170. .bInterfaceNumber = 0,
  171. .bNumEndpoints = 1,
  172. .bInterfaceClass = USB_CLASS_COMM,
  173. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
  174. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  175. .iInterface = STRING_CONTROL,
  176. .ndesc = 3,
  177. .descs = (USBDescOther[]) {
  178. {
  179. /* Header Descriptor */
  180. .data = (uint8_t[]) {
  181. 0x05, /* u8 bLength */
  182. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  183. USB_CDC_HEADER_TYPE, /* u8 bDescriptorSubType */
  184. 0x10, 0x01, /* le16 bcdCDC */
  185. },
  186. },{
  187. /* Union Descriptor */
  188. .data = (uint8_t[]) {
  189. 0x05, /* u8 bLength */
  190. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  191. USB_CDC_UNION_TYPE, /* u8 bDescriptorSubType */
  192. 0x00, /* u8 bMasterInterface0 */
  193. 0x01, /* u8 bSlaveInterface0 */
  194. },
  195. },{
  196. /* Ethernet Descriptor */
  197. .data = (uint8_t[]) {
  198. 0x0d, /* u8 bLength */
  199. USB_DT_CS_INTERFACE, /* u8 bDescriptorType */
  200. USB_CDC_ETHERNET_TYPE, /* u8 bDescriptorSubType */
  201. STRING_ETHADDR, /* u8 iMACAddress */
  202. 0x00, 0x00, 0x00, 0x00, /* le32 bmEthernetStatistics */
  203. ETH_FRAME_LEN & 0xff,
  204. ETH_FRAME_LEN >> 8, /* le16 wMaxSegmentSize */
  205. 0x00, 0x00, /* le16 wNumberMCFilters */
  206. 0x00, /* u8 bNumberPowerFilters */
  207. },
  208. },
  209. },
  210. .eps = (USBDescEndpoint[]) {
  211. {
  212. .bEndpointAddress = USB_DIR_IN | 0x01,
  213. .bmAttributes = USB_ENDPOINT_XFER_INT,
  214. .wMaxPacketSize = STATUS_BYTECOUNT,
  215. .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
  216. },
  217. }
  218. },{
  219. /* CDC Data Interface (off) */
  220. .bInterfaceNumber = 1,
  221. .bAlternateSetting = 0,
  222. .bNumEndpoints = 0,
  223. .bInterfaceClass = USB_CLASS_CDC_DATA,
  224. },{
  225. /* CDC Data Interface */
  226. .bInterfaceNumber = 1,
  227. .bAlternateSetting = 1,
  228. .bNumEndpoints = 2,
  229. .bInterfaceClass = USB_CLASS_CDC_DATA,
  230. .iInterface = STRING_DATA,
  231. .eps = (USBDescEndpoint[]) {
  232. {
  233. .bEndpointAddress = USB_DIR_IN | 0x02,
  234. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  235. .wMaxPacketSize = 0x40,
  236. },{
  237. .bEndpointAddress = USB_DIR_OUT | 0x02,
  238. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  239. .wMaxPacketSize = 0x40,
  240. }
  241. }
  242. }
  243. };
  244. static const USBDescDevice desc_device_net = {
  245. .bcdUSB = 0x0200,
  246. .bDeviceClass = USB_CLASS_COMM,
  247. .bMaxPacketSize0 = 0x40,
  248. .bNumConfigurations = 2,
  249. .confs = (USBDescConfig[]) {
  250. {
  251. .bNumInterfaces = 2,
  252. .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
  253. .iConfiguration = STRING_RNDIS,
  254. .bmAttributes = 0xc0,
  255. .bMaxPower = 0x32,
  256. .nif = ARRAY_SIZE(desc_iface_rndis),
  257. .ifs = desc_iface_rndis,
  258. },{
  259. .bNumInterfaces = 2,
  260. .bConfigurationValue = DEV_CONFIG_VALUE,
  261. .iConfiguration = STRING_CDC,
  262. .bmAttributes = 0xc0,
  263. .bMaxPower = 0x32,
  264. .nif = ARRAY_SIZE(desc_iface_cdc),
  265. .ifs = desc_iface_cdc,
  266. }
  267. },
  268. };
  269. static const USBDesc desc_net = {
  270. .id = {
  271. .idVendor = RNDIS_VENDOR_NUM,
  272. .idProduct = RNDIS_PRODUCT_NUM,
  273. .bcdDevice = 0,
  274. .iManufacturer = STRING_MANUFACTURER,
  275. .iProduct = STRING_PRODUCT,
  276. .iSerialNumber = STRING_SERIALNUMBER,
  277. },
  278. .full = &desc_device_net,
  279. .str = usb_net_stringtable,
  280. };
  281. /*
  282. * RNDIS Definitions - in theory not specific to USB.
  283. */
  284. #define RNDIS_MAXIMUM_FRAME_SIZE 1518
  285. #define RNDIS_MAX_TOTAL_SIZE 1558
  286. /* Remote NDIS Versions */
  287. #define RNDIS_MAJOR_VERSION 1
  288. #define RNDIS_MINOR_VERSION 0
  289. /* Status Values */
  290. #define RNDIS_STATUS_SUCCESS 0x00000000U /* Success */
  291. #define RNDIS_STATUS_FAILURE 0xc0000001U /* Unspecified error */
  292. #define RNDIS_STATUS_INVALID_DATA 0xc0010015U /* Invalid data */
  293. #define RNDIS_STATUS_NOT_SUPPORTED 0xc00000bbU /* Unsupported request */
  294. #define RNDIS_STATUS_MEDIA_CONNECT 0x4001000bU /* Device connected */
  295. #define RNDIS_STATUS_MEDIA_DISCONNECT 0x4001000cU /* Device disconnected */
  296. /* Message Set for Connectionless (802.3) Devices */
  297. enum {
  298. RNDIS_PACKET_MSG = 1,
  299. RNDIS_INITIALIZE_MSG = 2, /* Initialize device */
  300. RNDIS_HALT_MSG = 3,
  301. RNDIS_QUERY_MSG = 4,
  302. RNDIS_SET_MSG = 5,
  303. RNDIS_RESET_MSG = 6,
  304. RNDIS_INDICATE_STATUS_MSG = 7,
  305. RNDIS_KEEPALIVE_MSG = 8,
  306. };
  307. /* Message completion */
  308. enum {
  309. RNDIS_INITIALIZE_CMPLT = 0x80000002U,
  310. RNDIS_QUERY_CMPLT = 0x80000004U,
  311. RNDIS_SET_CMPLT = 0x80000005U,
  312. RNDIS_RESET_CMPLT = 0x80000006U,
  313. RNDIS_KEEPALIVE_CMPLT = 0x80000008U,
  314. };
  315. /* Device Flags */
  316. enum {
  317. RNDIS_DF_CONNECTIONLESS = 1,
  318. RNDIS_DF_CONNECTIONORIENTED = 2,
  319. };
  320. #define RNDIS_MEDIUM_802_3 0x00000000U
  321. /* from drivers/net/sk98lin/h/skgepnmi.h */
  322. #define OID_PNP_CAPABILITIES 0xfd010100
  323. #define OID_PNP_SET_POWER 0xfd010101
  324. #define OID_PNP_QUERY_POWER 0xfd010102
  325. #define OID_PNP_ADD_WAKE_UP_PATTERN 0xfd010103
  326. #define OID_PNP_REMOVE_WAKE_UP_PATTERN 0xfd010104
  327. #define OID_PNP_ENABLE_WAKE_UP 0xfd010106
  328. typedef uint32_t le32;
  329. typedef struct rndis_init_msg_type {
  330. le32 MessageType;
  331. le32 MessageLength;
  332. le32 RequestID;
  333. le32 MajorVersion;
  334. le32 MinorVersion;
  335. le32 MaxTransferSize;
  336. } rndis_init_msg_type;
  337. typedef struct rndis_init_cmplt_type {
  338. le32 MessageType;
  339. le32 MessageLength;
  340. le32 RequestID;
  341. le32 Status;
  342. le32 MajorVersion;
  343. le32 MinorVersion;
  344. le32 DeviceFlags;
  345. le32 Medium;
  346. le32 MaxPacketsPerTransfer;
  347. le32 MaxTransferSize;
  348. le32 PacketAlignmentFactor;
  349. le32 AFListOffset;
  350. le32 AFListSize;
  351. } rndis_init_cmplt_type;
  352. typedef struct rndis_halt_msg_type {
  353. le32 MessageType;
  354. le32 MessageLength;
  355. le32 RequestID;
  356. } rndis_halt_msg_type;
  357. typedef struct rndis_query_msg_type {
  358. le32 MessageType;
  359. le32 MessageLength;
  360. le32 RequestID;
  361. le32 OID;
  362. le32 InformationBufferLength;
  363. le32 InformationBufferOffset;
  364. le32 DeviceVcHandle;
  365. } rndis_query_msg_type;
  366. typedef struct rndis_query_cmplt_type {
  367. le32 MessageType;
  368. le32 MessageLength;
  369. le32 RequestID;
  370. le32 Status;
  371. le32 InformationBufferLength;
  372. le32 InformationBufferOffset;
  373. } rndis_query_cmplt_type;
  374. typedef struct rndis_set_msg_type {
  375. le32 MessageType;
  376. le32 MessageLength;
  377. le32 RequestID;
  378. le32 OID;
  379. le32 InformationBufferLength;
  380. le32 InformationBufferOffset;
  381. le32 DeviceVcHandle;
  382. } rndis_set_msg_type;
  383. typedef struct rndis_set_cmplt_type {
  384. le32 MessageType;
  385. le32 MessageLength;
  386. le32 RequestID;
  387. le32 Status;
  388. } rndis_set_cmplt_type;
  389. typedef struct rndis_reset_msg_type {
  390. le32 MessageType;
  391. le32 MessageLength;
  392. le32 Reserved;
  393. } rndis_reset_msg_type;
  394. typedef struct rndis_reset_cmplt_type {
  395. le32 MessageType;
  396. le32 MessageLength;
  397. le32 Status;
  398. le32 AddressingReset;
  399. } rndis_reset_cmplt_type;
  400. typedef struct rndis_indicate_status_msg_type {
  401. le32 MessageType;
  402. le32 MessageLength;
  403. le32 Status;
  404. le32 StatusBufferLength;
  405. le32 StatusBufferOffset;
  406. } rndis_indicate_status_msg_type;
  407. typedef struct rndis_keepalive_msg_type {
  408. le32 MessageType;
  409. le32 MessageLength;
  410. le32 RequestID;
  411. } rndis_keepalive_msg_type;
  412. typedef struct rndis_keepalive_cmplt_type {
  413. le32 MessageType;
  414. le32 MessageLength;
  415. le32 RequestID;
  416. le32 Status;
  417. } rndis_keepalive_cmplt_type;
  418. struct rndis_packet_msg_type {
  419. le32 MessageType;
  420. le32 MessageLength;
  421. le32 DataOffset;
  422. le32 DataLength;
  423. le32 OOBDataOffset;
  424. le32 OOBDataLength;
  425. le32 NumOOBDataElements;
  426. le32 PerPacketInfoOffset;
  427. le32 PerPacketInfoLength;
  428. le32 VcHandle;
  429. le32 Reserved;
  430. };
  431. struct rndis_config_parameter {
  432. le32 ParameterNameOffset;
  433. le32 ParameterNameLength;
  434. le32 ParameterType;
  435. le32 ParameterValueOffset;
  436. le32 ParameterValueLength;
  437. };
  438. /* implementation specific */
  439. enum rndis_state
  440. {
  441. RNDIS_UNINITIALIZED,
  442. RNDIS_INITIALIZED,
  443. RNDIS_DATA_INITIALIZED,
  444. };
  445. /* from ndis.h */
  446. enum ndis_oid {
  447. /* Required Object IDs (OIDs) */
  448. OID_GEN_SUPPORTED_LIST = 0x00010101,
  449. OID_GEN_HARDWARE_STATUS = 0x00010102,
  450. OID_GEN_MEDIA_SUPPORTED = 0x00010103,
  451. OID_GEN_MEDIA_IN_USE = 0x00010104,
  452. OID_GEN_MAXIMUM_LOOKAHEAD = 0x00010105,
  453. OID_GEN_MAXIMUM_FRAME_SIZE = 0x00010106,
  454. OID_GEN_LINK_SPEED = 0x00010107,
  455. OID_GEN_TRANSMIT_BUFFER_SPACE = 0x00010108,
  456. OID_GEN_RECEIVE_BUFFER_SPACE = 0x00010109,
  457. OID_GEN_TRANSMIT_BLOCK_SIZE = 0x0001010a,
  458. OID_GEN_RECEIVE_BLOCK_SIZE = 0x0001010b,
  459. OID_GEN_VENDOR_ID = 0x0001010c,
  460. OID_GEN_VENDOR_DESCRIPTION = 0x0001010d,
  461. OID_GEN_CURRENT_PACKET_FILTER = 0x0001010e,
  462. OID_GEN_CURRENT_LOOKAHEAD = 0x0001010f,
  463. OID_GEN_DRIVER_VERSION = 0x00010110,
  464. OID_GEN_MAXIMUM_TOTAL_SIZE = 0x00010111,
  465. OID_GEN_PROTOCOL_OPTIONS = 0x00010112,
  466. OID_GEN_MAC_OPTIONS = 0x00010113,
  467. OID_GEN_MEDIA_CONNECT_STATUS = 0x00010114,
  468. OID_GEN_MAXIMUM_SEND_PACKETS = 0x00010115,
  469. OID_GEN_VENDOR_DRIVER_VERSION = 0x00010116,
  470. OID_GEN_SUPPORTED_GUIDS = 0x00010117,
  471. OID_GEN_NETWORK_LAYER_ADDRESSES = 0x00010118,
  472. OID_GEN_TRANSPORT_HEADER_OFFSET = 0x00010119,
  473. OID_GEN_MACHINE_NAME = 0x0001021a,
  474. OID_GEN_RNDIS_CONFIG_PARAMETER = 0x0001021b,
  475. OID_GEN_VLAN_ID = 0x0001021c,
  476. /* Optional OIDs */
  477. OID_GEN_MEDIA_CAPABILITIES = 0x00010201,
  478. OID_GEN_PHYSICAL_MEDIUM = 0x00010202,
  479. /* Required statistics OIDs */
  480. OID_GEN_XMIT_OK = 0x00020101,
  481. OID_GEN_RCV_OK = 0x00020102,
  482. OID_GEN_XMIT_ERROR = 0x00020103,
  483. OID_GEN_RCV_ERROR = 0x00020104,
  484. OID_GEN_RCV_NO_BUFFER = 0x00020105,
  485. /* Optional statistics OIDs */
  486. OID_GEN_DIRECTED_BYTES_XMIT = 0x00020201,
  487. OID_GEN_DIRECTED_FRAMES_XMIT = 0x00020202,
  488. OID_GEN_MULTICAST_BYTES_XMIT = 0x00020203,
  489. OID_GEN_MULTICAST_FRAMES_XMIT = 0x00020204,
  490. OID_GEN_BROADCAST_BYTES_XMIT = 0x00020205,
  491. OID_GEN_BROADCAST_FRAMES_XMIT = 0x00020206,
  492. OID_GEN_DIRECTED_BYTES_RCV = 0x00020207,
  493. OID_GEN_DIRECTED_FRAMES_RCV = 0x00020208,
  494. OID_GEN_MULTICAST_BYTES_RCV = 0x00020209,
  495. OID_GEN_MULTICAST_FRAMES_RCV = 0x0002020a,
  496. OID_GEN_BROADCAST_BYTES_RCV = 0x0002020b,
  497. OID_GEN_BROADCAST_FRAMES_RCV = 0x0002020c,
  498. OID_GEN_RCV_CRC_ERROR = 0x0002020d,
  499. OID_GEN_TRANSMIT_QUEUE_LENGTH = 0x0002020e,
  500. OID_GEN_GET_TIME_CAPS = 0x0002020f,
  501. OID_GEN_GET_NETCARD_TIME = 0x00020210,
  502. OID_GEN_NETCARD_LOAD = 0x00020211,
  503. OID_GEN_DEVICE_PROFILE = 0x00020212,
  504. OID_GEN_INIT_TIME_MS = 0x00020213,
  505. OID_GEN_RESET_COUNTS = 0x00020214,
  506. OID_GEN_MEDIA_SENSE_COUNTS = 0x00020215,
  507. OID_GEN_FRIENDLY_NAME = 0x00020216,
  508. OID_GEN_MINIPORT_INFO = 0x00020217,
  509. OID_GEN_RESET_VERIFY_PARAMETERS = 0x00020218,
  510. /* IEEE 802.3 (Ethernet) OIDs */
  511. OID_802_3_PERMANENT_ADDRESS = 0x01010101,
  512. OID_802_3_CURRENT_ADDRESS = 0x01010102,
  513. OID_802_3_MULTICAST_LIST = 0x01010103,
  514. OID_802_3_MAXIMUM_LIST_SIZE = 0x01010104,
  515. OID_802_3_MAC_OPTIONS = 0x01010105,
  516. OID_802_3_RCV_ERROR_ALIGNMENT = 0x01020101,
  517. OID_802_3_XMIT_ONE_COLLISION = 0x01020102,
  518. OID_802_3_XMIT_MORE_COLLISIONS = 0x01020103,
  519. OID_802_3_XMIT_DEFERRED = 0x01020201,
  520. OID_802_3_XMIT_MAX_COLLISIONS = 0x01020202,
  521. OID_802_3_RCV_OVERRUN = 0x01020203,
  522. OID_802_3_XMIT_UNDERRUN = 0x01020204,
  523. OID_802_3_XMIT_HEARTBEAT_FAILURE = 0x01020205,
  524. OID_802_3_XMIT_TIMES_CRS_LOST = 0x01020206,
  525. OID_802_3_XMIT_LATE_COLLISIONS = 0x01020207,
  526. };
  527. static const uint32_t oid_supported_list[] =
  528. {
  529. /* the general stuff */
  530. OID_GEN_SUPPORTED_LIST,
  531. OID_GEN_HARDWARE_STATUS,
  532. OID_GEN_MEDIA_SUPPORTED,
  533. OID_GEN_MEDIA_IN_USE,
  534. OID_GEN_MAXIMUM_FRAME_SIZE,
  535. OID_GEN_LINK_SPEED,
  536. OID_GEN_TRANSMIT_BLOCK_SIZE,
  537. OID_GEN_RECEIVE_BLOCK_SIZE,
  538. OID_GEN_VENDOR_ID,
  539. OID_GEN_VENDOR_DESCRIPTION,
  540. OID_GEN_VENDOR_DRIVER_VERSION,
  541. OID_GEN_CURRENT_PACKET_FILTER,
  542. OID_GEN_MAXIMUM_TOTAL_SIZE,
  543. OID_GEN_MEDIA_CONNECT_STATUS,
  544. OID_GEN_PHYSICAL_MEDIUM,
  545. /* the statistical stuff */
  546. OID_GEN_XMIT_OK,
  547. OID_GEN_RCV_OK,
  548. OID_GEN_XMIT_ERROR,
  549. OID_GEN_RCV_ERROR,
  550. OID_GEN_RCV_NO_BUFFER,
  551. /* IEEE 802.3 */
  552. /* the general stuff */
  553. OID_802_3_PERMANENT_ADDRESS,
  554. OID_802_3_CURRENT_ADDRESS,
  555. OID_802_3_MULTICAST_LIST,
  556. OID_802_3_MAC_OPTIONS,
  557. OID_802_3_MAXIMUM_LIST_SIZE,
  558. /* the statistical stuff */
  559. OID_802_3_RCV_ERROR_ALIGNMENT,
  560. OID_802_3_XMIT_ONE_COLLISION,
  561. OID_802_3_XMIT_MORE_COLLISIONS,
  562. };
  563. #define NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA (1 << 0)
  564. #define NDIS_MAC_OPTION_RECEIVE_SERIALIZED (1 << 1)
  565. #define NDIS_MAC_OPTION_TRANSFERS_NOT_PEND (1 << 2)
  566. #define NDIS_MAC_OPTION_NO_LOOPBACK (1 << 3)
  567. #define NDIS_MAC_OPTION_FULL_DUPLEX (1 << 4)
  568. #define NDIS_MAC_OPTION_EOTX_INDICATION (1 << 5)
  569. #define NDIS_MAC_OPTION_8021P_PRIORITY (1 << 6)
  570. struct rndis_response {
  571. QTAILQ_ENTRY(rndis_response) entries;
  572. uint32_t length;
  573. uint8_t buf[0];
  574. };
  575. typedef struct USBNetState {
  576. USBDevice dev;
  577. enum rndis_state rndis_state;
  578. uint32_t medium;
  579. uint32_t speed;
  580. uint32_t media_state;
  581. uint16_t filter;
  582. uint32_t vendorid;
  583. unsigned int out_ptr;
  584. uint8_t out_buf[2048];
  585. USBPacket *inpkt;
  586. unsigned int in_ptr, in_len;
  587. uint8_t in_buf[2048];
  588. 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);
  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 (error_is_set(&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. dc->fw_name = "network";
  1246. dc->vmsd = &vmstate_usb_net;
  1247. dc->props = net_properties;
  1248. }
  1249. static const TypeInfo net_info = {
  1250. .name = "usb-net",
  1251. .parent = TYPE_USB_DEVICE,
  1252. .instance_size = sizeof(USBNetState),
  1253. .class_init = usb_net_class_initfn,
  1254. };
  1255. static void usb_net_register_types(void)
  1256. {
  1257. type_register_static(&net_info);
  1258. usb_legacy_register("usb-net", "net", usb_net_init);
  1259. }
  1260. type_init(usb_net_register_types)