usb-net.c 44 KB

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