rndis_host.c 16 KB

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