2
0

dev-network.c 45 KB

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