rndis_host.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Host Side support for RNDIS Networking Links
  3. * Copyright (C) 2005 by David Brownell
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. // #define DEBUG // error path messages, extra info
  20. // #define VERBOSE // more; success messages
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/mii.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/cdc.h>
  30. #include "usbnet.h"
  31. #include "rndis_host.h"
  32. /*
  33. * RNDIS is NDIS remoted over USB. It's a MSFT variant of CDC ACM ... of
  34. * course ACM was intended for modems, not Ethernet links! USB's standard
  35. * for Ethernet links is "CDC Ethernet", which is significantly simpler.
  36. *
  37. * NOTE that Microsoft's "RNDIS 1.0" specification is incomplete. Issues
  38. * include:
  39. * - Power management in particular relies on information that's scattered
  40. * through other documentation, and which is incomplete or incorrect even
  41. * there.
  42. * - There are various undocumented protocol requirements, such as the
  43. * need to send unused garbage in control-OUT messages.
  44. * - In some cases, MS-Windows will emit undocumented requests; this
  45. * matters more to peripheral implementations than host ones.
  46. *
  47. * Moreover there's a no-open-specs variant of RNDIS called "ActiveSync".
  48. *
  49. * For these reasons and others, ** USE OF RNDIS IS STRONGLY DISCOURAGED ** in
  50. * favor of such non-proprietary alternatives as CDC Ethernet or the newer (and
  51. * currently rare) "Ethernet Emulation Model" (EEM).
  52. */
  53. /*
  54. * RNDIS notifications from device: command completion; "reverse"
  55. * keepalives; etc
  56. */
  57. void rndis_status(struct usbnet *dev, struct urb *urb)
  58. {
  59. devdbg(dev, "rndis status urb, len %d stat %d",
  60. urb->actual_length, urb->status);
  61. // FIXME for keepalives, respond immediately (asynchronously)
  62. // if not an RNDIS status, do like cdc_status(dev,urb) does
  63. }
  64. EXPORT_SYMBOL_GPL(rndis_status);
  65. /*
  66. * RPC done RNDIS-style. Caller guarantees:
  67. * - message is properly byteswapped
  68. * - there's no other request pending
  69. * - buf can hold up to 1KB response (required by RNDIS spec)
  70. * On return, the first few entries are already byteswapped.
  71. *
  72. * Call context is likely probe(), before interface name is known,
  73. * which is why we won't try to use it in the diagnostics.
  74. */
  75. int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf)
  76. {
  77. struct cdc_state *info = (void *) &dev->data;
  78. int master_ifnum;
  79. int retval;
  80. unsigned count;
  81. __le32 rsp;
  82. u32 xid = 0, msg_len, request_id;
  83. /* REVISIT when this gets called from contexts other than probe() or
  84. * disconnect(): either serialize, or dispatch responses on xid
  85. */
  86. /* Issue the request; xid is unique, don't bother byteswapping it */
  87. if (likely(buf->msg_type != RNDIS_MSG_HALT
  88. && buf->msg_type != RNDIS_MSG_RESET)) {
  89. xid = dev->xid++;
  90. if (!xid)
  91. xid = dev->xid++;
  92. buf->request_id = (__force __le32) xid;
  93. }
  94. master_ifnum = info->control->cur_altsetting->desc.bInterfaceNumber;
  95. retval = usb_control_msg(dev->udev,
  96. usb_sndctrlpipe(dev->udev, 0),
  97. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  98. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  99. 0, master_ifnum,
  100. buf, le32_to_cpu(buf->msg_len),
  101. RNDIS_CONTROL_TIMEOUT_MS);
  102. if (unlikely(retval < 0 || xid == 0))
  103. return retval;
  104. // FIXME Seems like some devices discard responses when
  105. // we time out and cancel our "get response" requests...
  106. // so, this is fragile. Probably need to poll for status.
  107. /* ignore status endpoint, just poll the control channel;
  108. * the request probably completed immediately
  109. */
  110. rsp = buf->msg_type | RNDIS_MSG_COMPLETION;
  111. for (count = 0; count < 10; count++) {
  112. memset(buf, 0, CONTROL_BUFFER_SIZE);
  113. retval = usb_control_msg(dev->udev,
  114. usb_rcvctrlpipe(dev->udev, 0),
  115. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  116. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  117. 0, master_ifnum,
  118. buf, CONTROL_BUFFER_SIZE,
  119. RNDIS_CONTROL_TIMEOUT_MS);
  120. if (likely(retval >= 8)) {
  121. msg_len = le32_to_cpu(buf->msg_len);
  122. request_id = (__force u32) buf->request_id;
  123. if (likely(buf->msg_type == rsp)) {
  124. if (likely(request_id == xid)) {
  125. if (unlikely(rsp == RNDIS_MSG_RESET_C))
  126. return 0;
  127. if (likely(RNDIS_STATUS_SUCCESS
  128. == buf->status))
  129. return 0;
  130. dev_dbg(&info->control->dev,
  131. "rndis reply status %08x\n",
  132. le32_to_cpu(buf->status));
  133. return -EL3RST;
  134. }
  135. dev_dbg(&info->control->dev,
  136. "rndis reply id %d expected %d\n",
  137. request_id, xid);
  138. /* then likely retry */
  139. } else switch (buf->msg_type) {
  140. case RNDIS_MSG_INDICATE: { /* fault */
  141. // struct rndis_indicate *msg = (void *)buf;
  142. dev_info(&info->control->dev,
  143. "rndis fault indication\n");
  144. }
  145. break;
  146. case RNDIS_MSG_KEEPALIVE: { /* ping */
  147. struct rndis_keepalive_c *msg = (void *)buf;
  148. msg->msg_type = RNDIS_MSG_KEEPALIVE_C;
  149. msg->msg_len = ccpu2(sizeof *msg);
  150. msg->status = RNDIS_STATUS_SUCCESS;
  151. retval = usb_control_msg(dev->udev,
  152. usb_sndctrlpipe(dev->udev, 0),
  153. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  154. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  155. 0, master_ifnum,
  156. msg, sizeof *msg,
  157. RNDIS_CONTROL_TIMEOUT_MS);
  158. if (unlikely(retval < 0))
  159. dev_dbg(&info->control->dev,
  160. "rndis keepalive err %d\n",
  161. retval);
  162. }
  163. break;
  164. default:
  165. dev_dbg(&info->control->dev,
  166. "unexpected rndis msg %08x len %d\n",
  167. le32_to_cpu(buf->msg_type), msg_len);
  168. }
  169. } else {
  170. /* device probably issued a protocol stall; ignore */
  171. dev_dbg(&info->control->dev,
  172. "rndis response error, code %d\n", retval);
  173. }
  174. msleep(2);
  175. }
  176. dev_dbg(&info->control->dev, "rndis response timeout\n");
  177. return -ETIMEDOUT;
  178. }
  179. EXPORT_SYMBOL_GPL(rndis_command);
  180. /*
  181. * rndis_query:
  182. *
  183. * Performs a query for @oid along with 0 or more bytes of payload as
  184. * specified by @in_len. If @reply_len is not set to -1 then the reply
  185. * length is checked against this value, resulting in an error if it
  186. * doesn't match.
  187. *
  188. * NOTE: Adding a payload exactly or greater than the size of the expected
  189. * response payload is an evident requirement MSFT added for ActiveSync.
  190. *
  191. * The only exception is for OIDs that return a variably sized response,
  192. * in which case no payload should be added. This undocumented (and
  193. * nonsensical!) issue was found by sniffing protocol requests from the
  194. * ActiveSync 4.1 Windows driver.
  195. */
  196. static int rndis_query(struct usbnet *dev, struct usb_interface *intf,
  197. void *buf, u32 oid, u32 in_len,
  198. void **reply, int *reply_len)
  199. {
  200. int retval;
  201. union {
  202. void *buf;
  203. struct rndis_msg_hdr *header;
  204. struct rndis_query *get;
  205. struct rndis_query_c *get_c;
  206. } u;
  207. u32 off, len;
  208. u.buf = buf;
  209. memset(u.get, 0, sizeof *u.get + in_len);
  210. u.get->msg_type = RNDIS_MSG_QUERY;
  211. u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len);
  212. u.get->oid = oid;
  213. u.get->len = cpu_to_le32(in_len);
  214. u.get->offset = ccpu2(20);
  215. retval = rndis_command(dev, u.header);
  216. if (unlikely(retval < 0)) {
  217. dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) failed, %d\n",
  218. oid, retval);
  219. return retval;
  220. }
  221. off = le32_to_cpu(u.get_c->offset);
  222. len = le32_to_cpu(u.get_c->len);
  223. if (unlikely((8 + off + len) > CONTROL_BUFFER_SIZE))
  224. goto response_error;
  225. if (*reply_len != -1 && len != *reply_len)
  226. goto response_error;
  227. *reply = (unsigned char *) &u.get_c->request_id + off;
  228. *reply_len = len;
  229. return retval;
  230. response_error:
  231. dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) "
  232. "invalid response - off %d len %d\n",
  233. oid, off, len);
  234. return -EDOM;
  235. }
  236. int generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf)
  237. {
  238. int retval;
  239. struct net_device *net = dev->net;
  240. struct cdc_state *info = (void *) &dev->data;
  241. union {
  242. void *buf;
  243. struct rndis_msg_hdr *header;
  244. struct rndis_init *init;
  245. struct rndis_init_c *init_c;
  246. struct rndis_query *get;
  247. struct rndis_query_c *get_c;
  248. struct rndis_set *set;
  249. struct rndis_set_c *set_c;
  250. struct rndis_halt *halt;
  251. } u;
  252. u32 tmp;
  253. int reply_len;
  254. unsigned char *bp;
  255. /* we can't rely on i/o from stack working, or stack allocation */
  256. u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  257. if (!u.buf)
  258. return -ENOMEM;
  259. retval = usbnet_generic_cdc_bind(dev, intf);
  260. if (retval < 0)
  261. goto fail;
  262. u.init->msg_type = RNDIS_MSG_INIT;
  263. u.init->msg_len = ccpu2(sizeof *u.init);
  264. u.init->major_version = ccpu2(1);
  265. u.init->minor_version = ccpu2(0);
  266. /* max transfer (in spec) is 0x4000 at full speed, but for
  267. * TX we'll stick to one Ethernet packet plus RNDIS framing.
  268. * For RX we handle drivers that zero-pad to end-of-packet.
  269. * Don't let userspace change these settings.
  270. *
  271. * NOTE: there still seems to be wierdness here, as if we need
  272. * to do some more things to make sure WinCE targets accept this.
  273. * They default to jumbograms of 8KB or 16KB, which is absurd
  274. * for such low data rates and which is also more than Linux
  275. * can usually expect to allocate for SKB data...
  276. */
  277. net->hard_header_len += sizeof (struct rndis_data_hdr);
  278. dev->hard_mtu = net->mtu + net->hard_header_len;
  279. dev->rx_urb_size = dev->hard_mtu + (dev->maxpacket + 1);
  280. dev->rx_urb_size &= ~(dev->maxpacket - 1);
  281. u.init->max_transfer_size = cpu_to_le32(dev->rx_urb_size);
  282. net->change_mtu = NULL;
  283. retval = rndis_command(dev, u.header);
  284. if (unlikely(retval < 0)) {
  285. /* it might not even be an RNDIS device!! */
  286. dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
  287. goto fail_and_release;
  288. }
  289. tmp = le32_to_cpu(u.init_c->max_transfer_size);
  290. if (tmp < dev->hard_mtu) {
  291. if (tmp <= net->hard_header_len) {
  292. dev_err(&intf->dev,
  293. "dev can't take %u byte packets (max %u)\n",
  294. dev->hard_mtu, tmp);
  295. retval = -EINVAL;
  296. goto halt_fail_and_release;
  297. }
  298. dev->hard_mtu = tmp;
  299. net->mtu = dev->hard_mtu - net->hard_header_len;
  300. dev_warn(&intf->dev,
  301. "dev can't take %u byte packets (max %u), "
  302. "adjusting MTU to %u\n",
  303. dev->hard_mtu, tmp, net->mtu);
  304. }
  305. /* REVISIT: peripheral "alignment" request is ignored ... */
  306. dev_dbg(&intf->dev,
  307. "hard mtu %u (%u from dev), rx buflen %Zu, align %d\n",
  308. dev->hard_mtu, tmp, dev->rx_urb_size,
  309. 1 << le32_to_cpu(u.init_c->packet_alignment));
  310. /* module has some device initialization code needs to be done right
  311. * after RNDIS_INIT */
  312. if (dev->driver_info->early_init &&
  313. dev->driver_info->early_init(dev) != 0)
  314. goto halt_fail_and_release;
  315. /* Get designated host ethernet address */
  316. reply_len = ETH_ALEN;
  317. retval = rndis_query(dev, intf, u.buf, OID_802_3_PERMANENT_ADDRESS,
  318. 48, (void **) &bp, &reply_len);
  319. if (unlikely(retval< 0)) {
  320. dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
  321. goto halt_fail_and_release;
  322. }
  323. memcpy(net->dev_addr, bp, ETH_ALEN);
  324. /* set a nonzero filter to enable data transfers */
  325. memset(u.set, 0, sizeof *u.set);
  326. u.set->msg_type = RNDIS_MSG_SET;
  327. u.set->msg_len = ccpu2(4 + sizeof *u.set);
  328. u.set->oid = OID_GEN_CURRENT_PACKET_FILTER;
  329. u.set->len = ccpu2(4);
  330. u.set->offset = ccpu2((sizeof *u.set) - 8);
  331. *(__le32 *)(u.buf + sizeof *u.set) = RNDIS_DEFAULT_FILTER;
  332. retval = rndis_command(dev, u.header);
  333. if (unlikely(retval < 0)) {
  334. dev_err(&intf->dev, "rndis set packet filter, %d\n", retval);
  335. goto halt_fail_and_release;
  336. }
  337. retval = 0;
  338. kfree(u.buf);
  339. return retval;
  340. halt_fail_and_release:
  341. memset(u.halt, 0, sizeof *u.halt);
  342. u.halt->msg_type = RNDIS_MSG_HALT;
  343. u.halt->msg_len = ccpu2(sizeof *u.halt);
  344. (void) rndis_command(dev, (void *)u.halt);
  345. fail_and_release:
  346. usb_set_intfdata(info->data, NULL);
  347. usb_driver_release_interface(driver_of(intf), info->data);
  348. info->data = NULL;
  349. fail:
  350. kfree(u.buf);
  351. return retval;
  352. }
  353. EXPORT_SYMBOL_GPL(generic_rndis_bind);
  354. void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
  355. {
  356. struct rndis_halt *halt;
  357. /* try to clear any rndis state/activity (no i/o from stack!) */
  358. halt = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  359. if (halt) {
  360. halt->msg_type = RNDIS_MSG_HALT;
  361. halt->msg_len = ccpu2(sizeof *halt);
  362. (void) rndis_command(dev, (void *)halt);
  363. kfree(halt);
  364. }
  365. usbnet_cdc_unbind(dev, intf);
  366. }
  367. EXPORT_SYMBOL_GPL(rndis_unbind);
  368. /*
  369. * DATA -- host must not write zlps
  370. */
  371. int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  372. {
  373. /* peripheral may have batched packets to us... */
  374. while (likely(skb->len)) {
  375. struct rndis_data_hdr *hdr = (void *)skb->data;
  376. struct sk_buff *skb2;
  377. u32 msg_len, data_offset, data_len;
  378. msg_len = le32_to_cpu(hdr->msg_len);
  379. data_offset = le32_to_cpu(hdr->data_offset);
  380. data_len = le32_to_cpu(hdr->data_len);
  381. /* don't choke if we see oob, per-packet data, etc */
  382. if (unlikely(hdr->msg_type != RNDIS_MSG_PACKET
  383. || skb->len < msg_len
  384. || (data_offset + data_len + 8) > msg_len)) {
  385. dev->stats.rx_frame_errors++;
  386. devdbg(dev, "bad rndis message %d/%d/%d/%d, len %d",
  387. le32_to_cpu(hdr->msg_type),
  388. msg_len, data_offset, data_len, skb->len);
  389. return 0;
  390. }
  391. skb_pull(skb, 8 + data_offset);
  392. /* at most one packet left? */
  393. if (likely((data_len - skb->len) <= sizeof *hdr)) {
  394. skb_trim(skb, data_len);
  395. break;
  396. }
  397. /* try to return all the packets in the batch */
  398. skb2 = skb_clone(skb, GFP_ATOMIC);
  399. if (unlikely(!skb2))
  400. break;
  401. skb_pull(skb, msg_len - sizeof *hdr);
  402. skb_trim(skb2, data_len);
  403. usbnet_skb_return(dev, skb2);
  404. }
  405. /* caller will usbnet_skb_return the remaining packet */
  406. return 1;
  407. }
  408. EXPORT_SYMBOL_GPL(rndis_rx_fixup);
  409. struct sk_buff *
  410. rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  411. {
  412. struct rndis_data_hdr *hdr;
  413. struct sk_buff *skb2;
  414. unsigned len = skb->len;
  415. if (likely(!skb_cloned(skb))) {
  416. int room = skb_headroom(skb);
  417. /* enough head room as-is? */
  418. if (unlikely((sizeof *hdr) <= room))
  419. goto fill;
  420. /* enough room, but needs to be readjusted? */
  421. room += skb_tailroom(skb);
  422. if (likely((sizeof *hdr) <= room)) {
  423. skb->data = memmove(skb->head + sizeof *hdr,
  424. skb->data, len);
  425. skb_set_tail_pointer(skb, len);
  426. goto fill;
  427. }
  428. }
  429. /* create a new skb, with the correct size (and tailpad) */
  430. skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags);
  431. dev_kfree_skb_any(skb);
  432. if (unlikely(!skb2))
  433. return skb2;
  434. skb = skb2;
  435. /* fill out the RNDIS header. we won't bother trying to batch
  436. * packets; Linux minimizes wasted bandwidth through tx queues.
  437. */
  438. fill:
  439. hdr = (void *) __skb_push(skb, sizeof *hdr);
  440. memset(hdr, 0, sizeof *hdr);
  441. hdr->msg_type = RNDIS_MSG_PACKET;
  442. hdr->msg_len = cpu_to_le32(skb->len);
  443. hdr->data_offset = ccpu2(sizeof(*hdr) - 8);
  444. hdr->data_len = cpu_to_le32(len);
  445. /* FIXME make the last packet always be short ... */
  446. return skb;
  447. }
  448. EXPORT_SYMBOL_GPL(rndis_tx_fixup);
  449. static const struct driver_info rndis_info = {
  450. .description = "RNDIS device",
  451. .flags = FLAG_ETHER | FLAG_FRAMING_RN | FLAG_NO_SETINT,
  452. .bind = generic_rndis_bind,
  453. .unbind = rndis_unbind,
  454. .status = rndis_status,
  455. .rx_fixup = rndis_rx_fixup,
  456. .tx_fixup = rndis_tx_fixup,
  457. };
  458. #undef ccpu2
  459. /*-------------------------------------------------------------------------*/
  460. static const struct usb_device_id products [] = {
  461. {
  462. /* RNDIS is MSFT's un-official variant of CDC ACM */
  463. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  464. .driver_info = (unsigned long) &rndis_info,
  465. }, {
  466. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  467. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  468. .driver_info = (unsigned long) &rndis_info,
  469. },
  470. { }, // END
  471. };
  472. MODULE_DEVICE_TABLE(usb, products);
  473. static struct usb_driver rndis_driver = {
  474. .name = "rndis_host",
  475. .id_table = products,
  476. .probe = usbnet_probe,
  477. .disconnect = usbnet_disconnect,
  478. .suspend = usbnet_suspend,
  479. .resume = usbnet_resume,
  480. };
  481. static int __init rndis_init(void)
  482. {
  483. return usb_register(&rndis_driver);
  484. }
  485. module_init(rndis_init);
  486. static void __exit rndis_exit(void)
  487. {
  488. usb_deregister(&rndis_driver);
  489. }
  490. module_exit(rndis_exit);
  491. MODULE_AUTHOR("David Brownell");
  492. MODULE_DESCRIPTION("USB Host side RNDIS driver");
  493. MODULE_LICENSE("GPL");