rndis.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. /*
  2. * RNDIS MSG parser
  3. *
  4. * Authors: Benedikt Spranger, Pengutronix
  5. * Robert Schwebel, Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This software was originally developed in conformance with
  12. * Microsoft's Remote NDIS Specification License Agreement.
  13. *
  14. * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
  15. * Fixed message length bug in init_response
  16. *
  17. * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
  18. * Fixed rndis_rm_hdr length bug.
  19. *
  20. * Copyright (C) 2004 by David Brownell
  21. * updates to merge with Linux 2.6, better match RNDIS spec
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/idr.h>
  28. #include <linux/list.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/slab.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/netdevice.h>
  33. #include <asm/io.h>
  34. #include <asm/byteorder.h>
  35. #include <asm/unaligned.h>
  36. #include "u_rndis.h"
  37. #undef VERBOSE_DEBUG
  38. #include "rndis.h"
  39. /* The driver for your USB chip needs to support ep0 OUT to work with
  40. * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
  41. *
  42. * Windows hosts need an INF file like Documentation/usb/linux.inf
  43. * and will be happier if you provide the host_addr module parameter.
  44. */
  45. #if 0
  46. static int rndis_debug = 0;
  47. module_param (rndis_debug, int, 0);
  48. MODULE_PARM_DESC (rndis_debug, "enable debugging");
  49. #else
  50. #define rndis_debug 0
  51. #endif
  52. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  53. #define NAME_TEMPLATE "driver/rndis-%03d"
  54. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  55. static DEFINE_IDA(rndis_ida);
  56. /* Driver Version */
  57. static const __le32 rndis_driver_version = cpu_to_le32(1);
  58. /* Function Prototypes */
  59. static rndis_resp_t *rndis_add_response(struct rndis_params *params,
  60. u32 length);
  61. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  62. static const struct file_operations rndis_proc_fops;
  63. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  64. /* supported OIDs */
  65. static const u32 oid_supported_list[] =
  66. {
  67. /* the general stuff */
  68. RNDIS_OID_GEN_SUPPORTED_LIST,
  69. RNDIS_OID_GEN_HARDWARE_STATUS,
  70. RNDIS_OID_GEN_MEDIA_SUPPORTED,
  71. RNDIS_OID_GEN_MEDIA_IN_USE,
  72. RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
  73. RNDIS_OID_GEN_LINK_SPEED,
  74. RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
  75. RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
  76. RNDIS_OID_GEN_VENDOR_ID,
  77. RNDIS_OID_GEN_VENDOR_DESCRIPTION,
  78. RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
  79. RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
  80. RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
  81. RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
  82. RNDIS_OID_GEN_PHYSICAL_MEDIUM,
  83. /* the statistical stuff */
  84. RNDIS_OID_GEN_XMIT_OK,
  85. RNDIS_OID_GEN_RCV_OK,
  86. RNDIS_OID_GEN_XMIT_ERROR,
  87. RNDIS_OID_GEN_RCV_ERROR,
  88. RNDIS_OID_GEN_RCV_NO_BUFFER,
  89. #ifdef RNDIS_OPTIONAL_STATS
  90. RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
  91. RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
  92. RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
  93. RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
  94. RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
  95. RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
  96. RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
  97. RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
  98. RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
  99. RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
  100. RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
  101. RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
  102. RNDIS_OID_GEN_RCV_CRC_ERROR,
  103. RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
  104. #endif /* RNDIS_OPTIONAL_STATS */
  105. /* mandatory 802.3 */
  106. /* the general stuff */
  107. RNDIS_OID_802_3_PERMANENT_ADDRESS,
  108. RNDIS_OID_802_3_CURRENT_ADDRESS,
  109. RNDIS_OID_802_3_MULTICAST_LIST,
  110. RNDIS_OID_802_3_MAC_OPTIONS,
  111. RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
  112. /* the statistical stuff */
  113. RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
  114. RNDIS_OID_802_3_XMIT_ONE_COLLISION,
  115. RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
  116. #ifdef RNDIS_OPTIONAL_STATS
  117. RNDIS_OID_802_3_XMIT_DEFERRED,
  118. RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
  119. RNDIS_OID_802_3_RCV_OVERRUN,
  120. RNDIS_OID_802_3_XMIT_UNDERRUN,
  121. RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
  122. RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
  123. RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
  124. #endif /* RNDIS_OPTIONAL_STATS */
  125. #ifdef RNDIS_PM
  126. /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
  127. * don't say what they mean ... and the NDIS specs are often
  128. * confusing and/or ambiguous in this context. (That is, more
  129. * so than their specs for the other OIDs.)
  130. *
  131. * FIXME someone who knows what these should do, please
  132. * implement them!
  133. */
  134. /* power management */
  135. OID_PNP_CAPABILITIES,
  136. OID_PNP_QUERY_POWER,
  137. OID_PNP_SET_POWER,
  138. #ifdef RNDIS_WAKEUP
  139. /* wake up host */
  140. OID_PNP_ENABLE_WAKE_UP,
  141. OID_PNP_ADD_WAKE_UP_PATTERN,
  142. OID_PNP_REMOVE_WAKE_UP_PATTERN,
  143. #endif /* RNDIS_WAKEUP */
  144. #endif /* RNDIS_PM */
  145. };
  146. /* NDIS Functions */
  147. static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
  148. unsigned buf_len, rndis_resp_t *r)
  149. {
  150. int retval = -ENOTSUPP;
  151. u32 length = 4; /* usually */
  152. __le32 *outbuf;
  153. int i, count;
  154. rndis_query_cmplt_type *resp;
  155. struct net_device *net;
  156. struct rtnl_link_stats64 temp;
  157. const struct rtnl_link_stats64 *stats;
  158. if (!r) return -ENOMEM;
  159. resp = (rndis_query_cmplt_type *)r->buf;
  160. if (!resp) return -ENOMEM;
  161. if (buf_len && rndis_debug > 1) {
  162. pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
  163. for (i = 0; i < buf_len; i += 16) {
  164. pr_debug("%03d: %08x %08x %08x %08x\n", i,
  165. get_unaligned_le32(&buf[i]),
  166. get_unaligned_le32(&buf[i + 4]),
  167. get_unaligned_le32(&buf[i + 8]),
  168. get_unaligned_le32(&buf[i + 12]));
  169. }
  170. }
  171. /* response goes here, right after the header */
  172. outbuf = (__le32 *)&resp[1];
  173. resp->InformationBufferOffset = cpu_to_le32(16);
  174. net = params->dev;
  175. stats = dev_get_stats(net, &temp);
  176. switch (OID) {
  177. /* general oids (table 4-1) */
  178. /* mandatory */
  179. case RNDIS_OID_GEN_SUPPORTED_LIST:
  180. pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
  181. length = sizeof(oid_supported_list);
  182. count = length / sizeof(u32);
  183. for (i = 0; i < count; i++)
  184. outbuf[i] = cpu_to_le32(oid_supported_list[i]);
  185. retval = 0;
  186. break;
  187. /* mandatory */
  188. case RNDIS_OID_GEN_HARDWARE_STATUS:
  189. pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
  190. /* Bogus question!
  191. * Hardware must be ready to receive high level protocols.
  192. * BTW:
  193. * reddite ergo quae sunt Caesaris Caesari
  194. * et quae sunt Dei Deo!
  195. */
  196. *outbuf = cpu_to_le32(0);
  197. retval = 0;
  198. break;
  199. /* mandatory */
  200. case RNDIS_OID_GEN_MEDIA_SUPPORTED:
  201. pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
  202. *outbuf = cpu_to_le32(params->medium);
  203. retval = 0;
  204. break;
  205. /* mandatory */
  206. case RNDIS_OID_GEN_MEDIA_IN_USE:
  207. pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
  208. /* one medium, one transport... (maybe you do it better) */
  209. *outbuf = cpu_to_le32(params->medium);
  210. retval = 0;
  211. break;
  212. /* mandatory */
  213. case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
  214. pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
  215. if (params->dev) {
  216. *outbuf = cpu_to_le32(params->dev->mtu);
  217. retval = 0;
  218. }
  219. break;
  220. /* mandatory */
  221. case RNDIS_OID_GEN_LINK_SPEED:
  222. if (rndis_debug > 1)
  223. pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
  224. if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED)
  225. *outbuf = cpu_to_le32(0);
  226. else
  227. *outbuf = cpu_to_le32(params->speed);
  228. retval = 0;
  229. break;
  230. /* mandatory */
  231. case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
  232. pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
  233. if (params->dev) {
  234. *outbuf = cpu_to_le32(params->dev->mtu);
  235. retval = 0;
  236. }
  237. break;
  238. /* mandatory */
  239. case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
  240. pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
  241. if (params->dev) {
  242. *outbuf = cpu_to_le32(params->dev->mtu);
  243. retval = 0;
  244. }
  245. break;
  246. /* mandatory */
  247. case RNDIS_OID_GEN_VENDOR_ID:
  248. pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
  249. *outbuf = cpu_to_le32(params->vendorID);
  250. retval = 0;
  251. break;
  252. /* mandatory */
  253. case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
  254. pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
  255. if (params->vendorDescr) {
  256. length = strlen(params->vendorDescr);
  257. memcpy(outbuf, params->vendorDescr, length);
  258. } else {
  259. outbuf[0] = 0;
  260. }
  261. retval = 0;
  262. break;
  263. case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
  264. pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
  265. /* Created as LE */
  266. *outbuf = rndis_driver_version;
  267. retval = 0;
  268. break;
  269. /* mandatory */
  270. case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
  271. pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
  272. *outbuf = cpu_to_le32(*params->filter);
  273. retval = 0;
  274. break;
  275. /* mandatory */
  276. case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
  277. pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
  278. *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
  279. retval = 0;
  280. break;
  281. /* mandatory */
  282. case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
  283. if (rndis_debug > 1)
  284. pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
  285. *outbuf = cpu_to_le32(params->media_state);
  286. retval = 0;
  287. break;
  288. case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
  289. pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
  290. *outbuf = cpu_to_le32(0);
  291. retval = 0;
  292. break;
  293. /* The RNDIS specification is incomplete/wrong. Some versions
  294. * of MS-Windows expect OIDs that aren't specified there. Other
  295. * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
  296. */
  297. case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */
  298. pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
  299. *outbuf = cpu_to_le32(
  300. RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
  301. | RNDIS_MAC_OPTION_FULL_DUPLEX);
  302. retval = 0;
  303. break;
  304. /* statistics OIDs (table 4-2) */
  305. /* mandatory */
  306. case RNDIS_OID_GEN_XMIT_OK:
  307. if (rndis_debug > 1)
  308. pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__);
  309. if (stats) {
  310. *outbuf = cpu_to_le32(stats->tx_packets
  311. - stats->tx_errors - stats->tx_dropped);
  312. retval = 0;
  313. }
  314. break;
  315. /* mandatory */
  316. case RNDIS_OID_GEN_RCV_OK:
  317. if (rndis_debug > 1)
  318. pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__);
  319. if (stats) {
  320. *outbuf = cpu_to_le32(stats->rx_packets
  321. - stats->rx_errors - stats->rx_dropped);
  322. retval = 0;
  323. }
  324. break;
  325. /* mandatory */
  326. case RNDIS_OID_GEN_XMIT_ERROR:
  327. if (rndis_debug > 1)
  328. pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__);
  329. if (stats) {
  330. *outbuf = cpu_to_le32(stats->tx_errors);
  331. retval = 0;
  332. }
  333. break;
  334. /* mandatory */
  335. case RNDIS_OID_GEN_RCV_ERROR:
  336. if (rndis_debug > 1)
  337. pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
  338. if (stats) {
  339. *outbuf = cpu_to_le32(stats->rx_errors);
  340. retval = 0;
  341. }
  342. break;
  343. /* mandatory */
  344. case RNDIS_OID_GEN_RCV_NO_BUFFER:
  345. pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
  346. if (stats) {
  347. *outbuf = cpu_to_le32(stats->rx_dropped);
  348. retval = 0;
  349. }
  350. break;
  351. /* ieee802.3 OIDs (table 4-3) */
  352. /* mandatory */
  353. case RNDIS_OID_802_3_PERMANENT_ADDRESS:
  354. pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
  355. if (params->dev) {
  356. length = ETH_ALEN;
  357. memcpy(outbuf, params->host_mac, length);
  358. retval = 0;
  359. }
  360. break;
  361. /* mandatory */
  362. case RNDIS_OID_802_3_CURRENT_ADDRESS:
  363. pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
  364. if (params->dev) {
  365. length = ETH_ALEN;
  366. memcpy(outbuf, params->host_mac, length);
  367. retval = 0;
  368. }
  369. break;
  370. /* mandatory */
  371. case RNDIS_OID_802_3_MULTICAST_LIST:
  372. pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
  373. /* Multicast base address only */
  374. *outbuf = cpu_to_le32(0xE0000000);
  375. retval = 0;
  376. break;
  377. /* mandatory */
  378. case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
  379. pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
  380. /* Multicast base address only */
  381. *outbuf = cpu_to_le32(1);
  382. retval = 0;
  383. break;
  384. case RNDIS_OID_802_3_MAC_OPTIONS:
  385. pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
  386. *outbuf = cpu_to_le32(0);
  387. retval = 0;
  388. break;
  389. /* ieee802.3 statistics OIDs (table 4-4) */
  390. /* mandatory */
  391. case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
  392. pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
  393. if (stats) {
  394. *outbuf = cpu_to_le32(stats->rx_frame_errors);
  395. retval = 0;
  396. }
  397. break;
  398. /* mandatory */
  399. case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
  400. pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
  401. *outbuf = cpu_to_le32(0);
  402. retval = 0;
  403. break;
  404. /* mandatory */
  405. case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
  406. pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
  407. *outbuf = cpu_to_le32(0);
  408. retval = 0;
  409. break;
  410. default:
  411. pr_warning("%s: query unknown OID 0x%08X\n",
  412. __func__, OID);
  413. }
  414. if (retval < 0)
  415. length = 0;
  416. resp->InformationBufferLength = cpu_to_le32(length);
  417. r->length = length + sizeof(*resp);
  418. resp->MessageLength = cpu_to_le32(r->length);
  419. return retval;
  420. }
  421. static int gen_ndis_set_resp(struct rndis_params *params, u32 OID,
  422. u8 *buf, u32 buf_len, rndis_resp_t *r)
  423. {
  424. rndis_set_cmplt_type *resp;
  425. int i, retval = -ENOTSUPP;
  426. if (!r)
  427. return -ENOMEM;
  428. resp = (rndis_set_cmplt_type *)r->buf;
  429. if (!resp)
  430. return -ENOMEM;
  431. if (buf_len && rndis_debug > 1) {
  432. pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
  433. for (i = 0; i < buf_len; i += 16) {
  434. pr_debug("%03d: %08x %08x %08x %08x\n", i,
  435. get_unaligned_le32(&buf[i]),
  436. get_unaligned_le32(&buf[i + 4]),
  437. get_unaligned_le32(&buf[i + 8]),
  438. get_unaligned_le32(&buf[i + 12]));
  439. }
  440. }
  441. switch (OID) {
  442. case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
  443. /* these NDIS_PACKET_TYPE_* bitflags are shared with
  444. * cdc_filter; it's not RNDIS-specific
  445. * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
  446. * PROMISCUOUS, DIRECTED,
  447. * MULTICAST, ALL_MULTICAST, BROADCAST
  448. */
  449. *params->filter = (u16)get_unaligned_le32(buf);
  450. pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
  451. __func__, *params->filter);
  452. /* this call has a significant side effect: it's
  453. * what makes the packet flow start and stop, like
  454. * activating the CDC Ethernet altsetting.
  455. */
  456. retval = 0;
  457. if (*params->filter) {
  458. params->state = RNDIS_DATA_INITIALIZED;
  459. netif_carrier_on(params->dev);
  460. if (netif_running(params->dev))
  461. netif_wake_queue(params->dev);
  462. } else {
  463. params->state = RNDIS_INITIALIZED;
  464. netif_carrier_off(params->dev);
  465. netif_stop_queue(params->dev);
  466. }
  467. break;
  468. case RNDIS_OID_802_3_MULTICAST_LIST:
  469. /* I think we can ignore this */
  470. pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
  471. retval = 0;
  472. break;
  473. default:
  474. pr_warning("%s: set unknown OID 0x%08X, size %d\n",
  475. __func__, OID, buf_len);
  476. }
  477. return retval;
  478. }
  479. /*
  480. * Response Functions
  481. */
  482. static int rndis_init_response(struct rndis_params *params,
  483. rndis_init_msg_type *buf)
  484. {
  485. rndis_init_cmplt_type *resp;
  486. rndis_resp_t *r;
  487. if (!params->dev)
  488. return -ENOTSUPP;
  489. r = rndis_add_response(params, sizeof(rndis_init_cmplt_type));
  490. if (!r)
  491. return -ENOMEM;
  492. resp = (rndis_init_cmplt_type *)r->buf;
  493. resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
  494. resp->MessageLength = cpu_to_le32(52);
  495. resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
  496. resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
  497. resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
  498. resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
  499. resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
  500. resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
  501. resp->MaxPacketsPerTransfer = cpu_to_le32(1);
  502. resp->MaxTransferSize = cpu_to_le32(
  503. params->dev->mtu
  504. + sizeof(struct ethhdr)
  505. + sizeof(struct rndis_packet_msg_type)
  506. + 22);
  507. resp->PacketAlignmentFactor = cpu_to_le32(0);
  508. resp->AFListOffset = cpu_to_le32(0);
  509. resp->AFListSize = cpu_to_le32(0);
  510. params->resp_avail(params->v);
  511. return 0;
  512. }
  513. static int rndis_query_response(struct rndis_params *params,
  514. rndis_query_msg_type *buf)
  515. {
  516. rndis_query_cmplt_type *resp;
  517. rndis_resp_t *r;
  518. /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
  519. if (!params->dev)
  520. return -ENOTSUPP;
  521. /*
  522. * we need more memory:
  523. * gen_ndis_query_resp expects enough space for
  524. * rndis_query_cmplt_type followed by data.
  525. * oid_supported_list is the largest data reply
  526. */
  527. r = rndis_add_response(params,
  528. sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
  529. if (!r)
  530. return -ENOMEM;
  531. resp = (rndis_query_cmplt_type *)r->buf;
  532. resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
  533. resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
  534. if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID),
  535. le32_to_cpu(buf->InformationBufferOffset)
  536. + 8 + (u8 *)buf,
  537. le32_to_cpu(buf->InformationBufferLength),
  538. r)) {
  539. /* OID not supported */
  540. resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
  541. resp->MessageLength = cpu_to_le32(sizeof *resp);
  542. resp->InformationBufferLength = cpu_to_le32(0);
  543. resp->InformationBufferOffset = cpu_to_le32(0);
  544. } else
  545. resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
  546. params->resp_avail(params->v);
  547. return 0;
  548. }
  549. static int rndis_set_response(struct rndis_params *params,
  550. rndis_set_msg_type *buf)
  551. {
  552. u32 BufLength, BufOffset;
  553. rndis_set_cmplt_type *resp;
  554. rndis_resp_t *r;
  555. r = rndis_add_response(params, sizeof(rndis_set_cmplt_type));
  556. if (!r)
  557. return -ENOMEM;
  558. resp = (rndis_set_cmplt_type *)r->buf;
  559. BufLength = le32_to_cpu(buf->InformationBufferLength);
  560. BufOffset = le32_to_cpu(buf->InformationBufferOffset);
  561. #ifdef VERBOSE_DEBUG
  562. pr_debug("%s: Length: %d\n", __func__, BufLength);
  563. pr_debug("%s: Offset: %d\n", __func__, BufOffset);
  564. pr_debug("%s: InfoBuffer: ", __func__);
  565. for (i = 0; i < BufLength; i++) {
  566. pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
  567. }
  568. pr_debug("\n");
  569. #endif
  570. resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
  571. resp->MessageLength = cpu_to_le32(16);
  572. resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
  573. if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID),
  574. ((u8 *)buf) + 8 + BufOffset, BufLength, r))
  575. resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
  576. else
  577. resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
  578. params->resp_avail(params->v);
  579. return 0;
  580. }
  581. static int rndis_reset_response(struct rndis_params *params,
  582. rndis_reset_msg_type *buf)
  583. {
  584. rndis_reset_cmplt_type *resp;
  585. rndis_resp_t *r;
  586. u8 *xbuf;
  587. u32 length;
  588. /* drain the response queue */
  589. while ((xbuf = rndis_get_next_response(params, &length)))
  590. rndis_free_response(params, xbuf);
  591. r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type));
  592. if (!r)
  593. return -ENOMEM;
  594. resp = (rndis_reset_cmplt_type *)r->buf;
  595. resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
  596. resp->MessageLength = cpu_to_le32(16);
  597. resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
  598. /* resent information */
  599. resp->AddressingReset = cpu_to_le32(1);
  600. params->resp_avail(params->v);
  601. return 0;
  602. }
  603. static int rndis_keepalive_response(struct rndis_params *params,
  604. rndis_keepalive_msg_type *buf)
  605. {
  606. rndis_keepalive_cmplt_type *resp;
  607. rndis_resp_t *r;
  608. /* host "should" check only in RNDIS_DATA_INITIALIZED state */
  609. r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type));
  610. if (!r)
  611. return -ENOMEM;
  612. resp = (rndis_keepalive_cmplt_type *)r->buf;
  613. resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
  614. resp->MessageLength = cpu_to_le32(16);
  615. resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
  616. resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
  617. params->resp_avail(params->v);
  618. return 0;
  619. }
  620. /*
  621. * Device to Host Comunication
  622. */
  623. static int rndis_indicate_status_msg(struct rndis_params *params, u32 status)
  624. {
  625. rndis_indicate_status_msg_type *resp;
  626. rndis_resp_t *r;
  627. if (params->state == RNDIS_UNINITIALIZED)
  628. return -ENOTSUPP;
  629. r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type));
  630. if (!r)
  631. return -ENOMEM;
  632. resp = (rndis_indicate_status_msg_type *)r->buf;
  633. resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
  634. resp->MessageLength = cpu_to_le32(20);
  635. resp->Status = cpu_to_le32(status);
  636. resp->StatusBufferLength = cpu_to_le32(0);
  637. resp->StatusBufferOffset = cpu_to_le32(0);
  638. params->resp_avail(params->v);
  639. return 0;
  640. }
  641. int rndis_signal_connect(struct rndis_params *params)
  642. {
  643. params->media_state = RNDIS_MEDIA_STATE_CONNECTED;
  644. return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT);
  645. }
  646. EXPORT_SYMBOL_GPL(rndis_signal_connect);
  647. int rndis_signal_disconnect(struct rndis_params *params)
  648. {
  649. params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
  650. return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT);
  651. }
  652. EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
  653. void rndis_uninit(struct rndis_params *params)
  654. {
  655. u8 *buf;
  656. u32 length;
  657. if (!params)
  658. return;
  659. params->state = RNDIS_UNINITIALIZED;
  660. /* drain the response queue */
  661. while ((buf = rndis_get_next_response(params, &length)))
  662. rndis_free_response(params, buf);
  663. }
  664. EXPORT_SYMBOL_GPL(rndis_uninit);
  665. void rndis_set_host_mac(struct rndis_params *params, const u8 *addr)
  666. {
  667. params->host_mac = addr;
  668. }
  669. EXPORT_SYMBOL_GPL(rndis_set_host_mac);
  670. /*
  671. * Message Parser
  672. */
  673. int rndis_msg_parser(struct rndis_params *params, u8 *buf)
  674. {
  675. u32 MsgType, MsgLength;
  676. __le32 *tmp;
  677. if (!buf)
  678. return -ENOMEM;
  679. tmp = (__le32 *)buf;
  680. MsgType = get_unaligned_le32(tmp++);
  681. MsgLength = get_unaligned_le32(tmp++);
  682. if (!params)
  683. return -ENOTSUPP;
  684. /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
  685. * rx/tx statistics and link status, in addition to KEEPALIVE traffic
  686. * and normal HC level polling to see if there's any IN traffic.
  687. */
  688. /* For USB: responses may take up to 10 seconds */
  689. switch (MsgType) {
  690. case RNDIS_MSG_INIT:
  691. pr_debug("%s: RNDIS_MSG_INIT\n",
  692. __func__);
  693. params->state = RNDIS_INITIALIZED;
  694. return rndis_init_response(params, (rndis_init_msg_type *)buf);
  695. case RNDIS_MSG_HALT:
  696. pr_debug("%s: RNDIS_MSG_HALT\n",
  697. __func__);
  698. params->state = RNDIS_UNINITIALIZED;
  699. if (params->dev) {
  700. netif_carrier_off(params->dev);
  701. netif_stop_queue(params->dev);
  702. }
  703. return 0;
  704. case RNDIS_MSG_QUERY:
  705. return rndis_query_response(params,
  706. (rndis_query_msg_type *)buf);
  707. case RNDIS_MSG_SET:
  708. return rndis_set_response(params, (rndis_set_msg_type *)buf);
  709. case RNDIS_MSG_RESET:
  710. pr_debug("%s: RNDIS_MSG_RESET\n",
  711. __func__);
  712. return rndis_reset_response(params,
  713. (rndis_reset_msg_type *)buf);
  714. case RNDIS_MSG_KEEPALIVE:
  715. /* For USB: host does this every 5 seconds */
  716. if (rndis_debug > 1)
  717. pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
  718. __func__);
  719. return rndis_keepalive_response(params,
  720. (rndis_keepalive_msg_type *)
  721. buf);
  722. default:
  723. /* At least Windows XP emits some undefined RNDIS messages.
  724. * In one case those messages seemed to relate to the host
  725. * suspending itself.
  726. */
  727. pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
  728. __func__, MsgType, MsgLength);
  729. print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
  730. buf, MsgLength);
  731. break;
  732. }
  733. return -ENOTSUPP;
  734. }
  735. EXPORT_SYMBOL_GPL(rndis_msg_parser);
  736. static inline int rndis_get_nr(void)
  737. {
  738. return ida_simple_get(&rndis_ida, 0, 0, GFP_KERNEL);
  739. }
  740. static inline void rndis_put_nr(int nr)
  741. {
  742. ida_simple_remove(&rndis_ida, nr);
  743. }
  744. struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v)
  745. {
  746. struct rndis_params *params;
  747. int i;
  748. if (!resp_avail)
  749. return ERR_PTR(-EINVAL);
  750. i = rndis_get_nr();
  751. if (i < 0) {
  752. pr_debug("failed\n");
  753. return ERR_PTR(-ENODEV);
  754. }
  755. params = kzalloc(sizeof(*params), GFP_KERNEL);
  756. if (!params) {
  757. rndis_put_nr(i);
  758. return ERR_PTR(-ENOMEM);
  759. }
  760. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  761. {
  762. struct proc_dir_entry *proc_entry;
  763. char name[20];
  764. sprintf(name, NAME_TEMPLATE, i);
  765. proc_entry = proc_create_data(name, 0660, NULL,
  766. &rndis_proc_fops, params);
  767. if (!proc_entry) {
  768. kfree(params);
  769. rndis_put_nr(i);
  770. return ERR_PTR(-EIO);
  771. }
  772. }
  773. #endif
  774. params->confignr = i;
  775. params->used = 1;
  776. params->state = RNDIS_UNINITIALIZED;
  777. params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
  778. params->resp_avail = resp_avail;
  779. params->v = v;
  780. INIT_LIST_HEAD(&params->resp_queue);
  781. pr_debug("%s: configNr = %d\n", __func__, i);
  782. return params;
  783. }
  784. EXPORT_SYMBOL_GPL(rndis_register);
  785. void rndis_deregister(struct rndis_params *params)
  786. {
  787. int i;
  788. pr_debug("%s:\n", __func__);
  789. if (!params)
  790. return;
  791. i = params->confignr;
  792. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  793. {
  794. char name[20];
  795. sprintf(name, NAME_TEMPLATE, i);
  796. remove_proc_entry(name, NULL);
  797. }
  798. #endif
  799. kfree(params);
  800. rndis_put_nr(i);
  801. }
  802. EXPORT_SYMBOL_GPL(rndis_deregister);
  803. int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev,
  804. u16 *cdc_filter)
  805. {
  806. pr_debug("%s:\n", __func__);
  807. if (!dev)
  808. return -EINVAL;
  809. if (!params)
  810. return -1;
  811. params->dev = dev;
  812. params->filter = cdc_filter;
  813. return 0;
  814. }
  815. EXPORT_SYMBOL_GPL(rndis_set_param_dev);
  816. int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID,
  817. const char *vendorDescr)
  818. {
  819. pr_debug("%s:\n", __func__);
  820. if (!vendorDescr) return -1;
  821. if (!params)
  822. return -1;
  823. params->vendorID = vendorID;
  824. params->vendorDescr = vendorDescr;
  825. return 0;
  826. }
  827. EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
  828. int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed)
  829. {
  830. pr_debug("%s: %u %u\n", __func__, medium, speed);
  831. if (!params)
  832. return -1;
  833. params->medium = medium;
  834. params->speed = speed;
  835. return 0;
  836. }
  837. EXPORT_SYMBOL_GPL(rndis_set_param_medium);
  838. void rndis_add_hdr(struct sk_buff *skb)
  839. {
  840. struct rndis_packet_msg_type *header;
  841. if (!skb)
  842. return;
  843. header = (void *)skb_push(skb, sizeof(*header));
  844. memset(header, 0, sizeof *header);
  845. header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
  846. header->MessageLength = cpu_to_le32(skb->len);
  847. header->DataOffset = cpu_to_le32(36);
  848. header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
  849. }
  850. EXPORT_SYMBOL_GPL(rndis_add_hdr);
  851. void rndis_free_response(struct rndis_params *params, u8 *buf)
  852. {
  853. rndis_resp_t *r, *n;
  854. list_for_each_entry_safe(r, n, &params->resp_queue, list) {
  855. if (r->buf == buf) {
  856. list_del(&r->list);
  857. kfree(r);
  858. }
  859. }
  860. }
  861. EXPORT_SYMBOL_GPL(rndis_free_response);
  862. u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
  863. {
  864. rndis_resp_t *r, *n;
  865. if (!length) return NULL;
  866. list_for_each_entry_safe(r, n, &params->resp_queue, list) {
  867. if (!r->send) {
  868. r->send = 1;
  869. *length = r->length;
  870. return r->buf;
  871. }
  872. }
  873. return NULL;
  874. }
  875. EXPORT_SYMBOL_GPL(rndis_get_next_response);
  876. static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
  877. {
  878. rndis_resp_t *r;
  879. /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
  880. r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
  881. if (!r) return NULL;
  882. r->buf = (u8 *)(r + 1);
  883. r->length = length;
  884. r->send = 0;
  885. list_add_tail(&r->list, &params->resp_queue);
  886. return r;
  887. }
  888. int rndis_rm_hdr(struct gether *port,
  889. struct sk_buff *skb,
  890. struct sk_buff_head *list)
  891. {
  892. /* tmp points to a struct rndis_packet_msg_type */
  893. __le32 *tmp = (void *)skb->data;
  894. /* MessageType, MessageLength */
  895. if (cpu_to_le32(RNDIS_MSG_PACKET)
  896. != get_unaligned(tmp++)) {
  897. dev_kfree_skb_any(skb);
  898. return -EINVAL;
  899. }
  900. tmp++;
  901. /* DataOffset, DataLength */
  902. if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
  903. dev_kfree_skb_any(skb);
  904. return -EOVERFLOW;
  905. }
  906. skb_trim(skb, get_unaligned_le32(tmp++));
  907. skb_queue_tail(list, skb);
  908. return 0;
  909. }
  910. EXPORT_SYMBOL_GPL(rndis_rm_hdr);
  911. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  912. static int rndis_proc_show(struct seq_file *m, void *v)
  913. {
  914. rndis_params *param = m->private;
  915. seq_printf(m,
  916. "Config Nr. %d\n"
  917. "used : %s\n"
  918. "state : %s\n"
  919. "medium : 0x%08X\n"
  920. "speed : %d\n"
  921. "cable : %s\n"
  922. "vendor ID : 0x%08X\n"
  923. "vendor : %s\n",
  924. param->confignr, (param->used) ? "y" : "n",
  925. ({ char *s = "?";
  926. switch (param->state) {
  927. case RNDIS_UNINITIALIZED:
  928. s = "RNDIS_UNINITIALIZED"; break;
  929. case RNDIS_INITIALIZED:
  930. s = "RNDIS_INITIALIZED"; break;
  931. case RNDIS_DATA_INITIALIZED:
  932. s = "RNDIS_DATA_INITIALIZED"; break;
  933. } s; }),
  934. param->medium,
  935. (param->media_state) ? 0 : param->speed*100,
  936. (param->media_state) ? "disconnected" : "connected",
  937. param->vendorID, param->vendorDescr);
  938. return 0;
  939. }
  940. static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
  941. size_t count, loff_t *ppos)
  942. {
  943. rndis_params *p = PDE_DATA(file_inode(file));
  944. u32 speed = 0;
  945. int i, fl_speed = 0;
  946. for (i = 0; i < count; i++) {
  947. char c;
  948. if (get_user(c, buffer))
  949. return -EFAULT;
  950. switch (c) {
  951. case '0':
  952. case '1':
  953. case '2':
  954. case '3':
  955. case '4':
  956. case '5':
  957. case '6':
  958. case '7':
  959. case '8':
  960. case '9':
  961. fl_speed = 1;
  962. speed = speed * 10 + c - '0';
  963. break;
  964. case 'C':
  965. case 'c':
  966. rndis_signal_connect(p);
  967. break;
  968. case 'D':
  969. case 'd':
  970. rndis_signal_disconnect(p);
  971. break;
  972. default:
  973. if (fl_speed) p->speed = speed;
  974. else pr_debug("%c is not valid\n", c);
  975. break;
  976. }
  977. buffer++;
  978. }
  979. return count;
  980. }
  981. static int rndis_proc_open(struct inode *inode, struct file *file)
  982. {
  983. return single_open(file, rndis_proc_show, PDE_DATA(inode));
  984. }
  985. static const struct file_operations rndis_proc_fops = {
  986. .owner = THIS_MODULE,
  987. .open = rndis_proc_open,
  988. .read = seq_read,
  989. .llseek = seq_lseek,
  990. .release = single_release,
  991. .write = rndis_proc_write,
  992. };
  993. #define NAME_TEMPLATE "driver/rndis-%03d"
  994. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */