stub_rx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4. */
  5. #include <asm/byteorder.h>
  6. #include <linux/kthread.h>
  7. #include <linux/usb.h>
  8. #include <linux/usb/hcd.h>
  9. #include "usbip_common.h"
  10. #include "stub.h"
  11. static int is_clear_halt_cmd(struct urb *urb)
  12. {
  13. struct usb_ctrlrequest *req;
  14. req = (struct usb_ctrlrequest *) urb->setup_packet;
  15. return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
  16. (req->bRequestType == USB_RECIP_ENDPOINT) &&
  17. (req->wValue == USB_ENDPOINT_HALT);
  18. }
  19. static int is_set_interface_cmd(struct urb *urb)
  20. {
  21. struct usb_ctrlrequest *req;
  22. req = (struct usb_ctrlrequest *) urb->setup_packet;
  23. return (req->bRequest == USB_REQ_SET_INTERFACE) &&
  24. (req->bRequestType == USB_RECIP_INTERFACE);
  25. }
  26. static int is_set_configuration_cmd(struct urb *urb)
  27. {
  28. struct usb_ctrlrequest *req;
  29. req = (struct usb_ctrlrequest *) urb->setup_packet;
  30. return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
  31. (req->bRequestType == USB_RECIP_DEVICE);
  32. }
  33. static int is_reset_device_cmd(struct urb *urb)
  34. {
  35. struct usb_ctrlrequest *req;
  36. __u16 value;
  37. __u16 index;
  38. req = (struct usb_ctrlrequest *) urb->setup_packet;
  39. value = le16_to_cpu(req->wValue);
  40. index = le16_to_cpu(req->wIndex);
  41. if ((req->bRequest == USB_REQ_SET_FEATURE) &&
  42. (req->bRequestType == USB_RT_PORT) &&
  43. (value == USB_PORT_FEAT_RESET)) {
  44. usbip_dbg_stub_rx("reset_device_cmd, port %u\n", index);
  45. return 1;
  46. } else
  47. return 0;
  48. }
  49. static int tweak_clear_halt_cmd(struct urb *urb)
  50. {
  51. struct usb_ctrlrequest *req;
  52. int target_endp;
  53. int target_dir;
  54. int target_pipe;
  55. int ret;
  56. req = (struct usb_ctrlrequest *) urb->setup_packet;
  57. /*
  58. * The stalled endpoint is specified in the wIndex value. The endpoint
  59. * of the urb is the target of this clear_halt request (i.e., control
  60. * endpoint).
  61. */
  62. target_endp = le16_to_cpu(req->wIndex) & 0x000f;
  63. /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80. */
  64. target_dir = le16_to_cpu(req->wIndex) & 0x0080;
  65. if (target_dir)
  66. target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
  67. else
  68. target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
  69. ret = usb_clear_halt(urb->dev, target_pipe);
  70. if (ret < 0)
  71. dev_err(&urb->dev->dev,
  72. "usb_clear_halt error: devnum %d endp %d ret %d\n",
  73. urb->dev->devnum, target_endp, ret);
  74. else
  75. dev_info(&urb->dev->dev,
  76. "usb_clear_halt done: devnum %d endp %d\n",
  77. urb->dev->devnum, target_endp);
  78. return ret;
  79. }
  80. static int tweak_set_interface_cmd(struct urb *urb)
  81. {
  82. struct usb_ctrlrequest *req;
  83. __u16 alternate;
  84. __u16 interface;
  85. int ret;
  86. req = (struct usb_ctrlrequest *) urb->setup_packet;
  87. alternate = le16_to_cpu(req->wValue);
  88. interface = le16_to_cpu(req->wIndex);
  89. usbip_dbg_stub_rx("set_interface: inf %u alt %u\n",
  90. interface, alternate);
  91. ret = usb_set_interface(urb->dev, interface, alternate);
  92. if (ret < 0)
  93. dev_err(&urb->dev->dev,
  94. "usb_set_interface error: inf %u alt %u ret %d\n",
  95. interface, alternate, ret);
  96. else
  97. dev_info(&urb->dev->dev,
  98. "usb_set_interface done: inf %u alt %u\n",
  99. interface, alternate);
  100. return ret;
  101. }
  102. static int tweak_set_configuration_cmd(struct urb *urb)
  103. {
  104. struct stub_priv *priv = (struct stub_priv *) urb->context;
  105. struct stub_device *sdev = priv->sdev;
  106. struct usb_ctrlrequest *req;
  107. __u16 config;
  108. int err;
  109. req = (struct usb_ctrlrequest *) urb->setup_packet;
  110. config = le16_to_cpu(req->wValue);
  111. err = usb_set_configuration(sdev->udev, config);
  112. if (err && err != -ENODEV)
  113. dev_err(&sdev->udev->dev, "can't set config #%d, error %d\n",
  114. config, err);
  115. return 0;
  116. }
  117. static int tweak_reset_device_cmd(struct urb *urb)
  118. {
  119. struct stub_priv *priv = (struct stub_priv *) urb->context;
  120. struct stub_device *sdev = priv->sdev;
  121. dev_info(&urb->dev->dev, "usb_queue_reset_device\n");
  122. if (usb_lock_device_for_reset(sdev->udev, NULL) < 0) {
  123. dev_err(&urb->dev->dev, "could not obtain lock to reset device\n");
  124. return 0;
  125. }
  126. usb_reset_device(sdev->udev);
  127. usb_unlock_device(sdev->udev);
  128. return 0;
  129. }
  130. /*
  131. * clear_halt, set_interface, and set_configuration require special tricks.
  132. */
  133. static void tweak_special_requests(struct urb *urb)
  134. {
  135. if (!urb || !urb->setup_packet)
  136. return;
  137. if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
  138. return;
  139. if (is_clear_halt_cmd(urb))
  140. /* tweak clear_halt */
  141. tweak_clear_halt_cmd(urb);
  142. else if (is_set_interface_cmd(urb))
  143. /* tweak set_interface */
  144. tweak_set_interface_cmd(urb);
  145. else if (is_set_configuration_cmd(urb))
  146. /* tweak set_configuration */
  147. tweak_set_configuration_cmd(urb);
  148. else if (is_reset_device_cmd(urb))
  149. tweak_reset_device_cmd(urb);
  150. else
  151. usbip_dbg_stub_rx("no need to tweak\n");
  152. }
  153. /*
  154. * stub_recv_unlink() unlinks the URB by a call to usb_unlink_urb().
  155. * By unlinking the urb asynchronously, stub_rx can continuously
  156. * process coming urbs. Even if the urb is unlinked, its completion
  157. * handler will be called and stub_tx will send a return pdu.
  158. *
  159. * See also comments about unlinking strategy in vhci_hcd.c.
  160. */
  161. static int stub_recv_cmd_unlink(struct stub_device *sdev,
  162. struct usbip_header *pdu)
  163. {
  164. int ret;
  165. unsigned long flags;
  166. struct stub_priv *priv;
  167. spin_lock_irqsave(&sdev->priv_lock, flags);
  168. list_for_each_entry(priv, &sdev->priv_init, list) {
  169. if (priv->seqnum != pdu->u.cmd_unlink.seqnum)
  170. continue;
  171. dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
  172. priv->urb);
  173. /*
  174. * This matched urb is not completed yet (i.e., be in
  175. * flight in usb hcd hardware/driver). Now we are
  176. * cancelling it. The unlinking flag means that we are
  177. * now not going to return the normal result pdu of a
  178. * submission request, but going to return a result pdu
  179. * of the unlink request.
  180. */
  181. priv->unlinking = 1;
  182. /*
  183. * In the case that unlinking flag is on, prev->seqnum
  184. * is changed from the seqnum of the cancelling urb to
  185. * the seqnum of the unlink request. This will be used
  186. * to make the result pdu of the unlink request.
  187. */
  188. priv->seqnum = pdu->base.seqnum;
  189. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  190. /*
  191. * usb_unlink_urb() is now out of spinlocking to avoid
  192. * spinlock recursion since stub_complete() is
  193. * sometimes called in this context but not in the
  194. * interrupt context. If stub_complete() is executed
  195. * before we call usb_unlink_urb(), usb_unlink_urb()
  196. * will return an error value. In this case, stub_tx
  197. * will return the result pdu of this unlink request
  198. * though submission is completed and actual unlinking
  199. * is not executed. OK?
  200. */
  201. /* In the above case, urb->status is not -ECONNRESET,
  202. * so a driver in a client host will know the failure
  203. * of the unlink request ?
  204. */
  205. ret = usb_unlink_urb(priv->urb);
  206. if (ret != -EINPROGRESS)
  207. dev_err(&priv->urb->dev->dev,
  208. "failed to unlink a urb %p, ret %d\n",
  209. priv->urb, ret);
  210. return 0;
  211. }
  212. usbip_dbg_stub_rx("seqnum %d is not pending\n",
  213. pdu->u.cmd_unlink.seqnum);
  214. /*
  215. * The urb of the unlink target is not found in priv_init queue. It was
  216. * already completed and its results is/was going to be sent by a
  217. * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
  218. * return the completeness of this unlink request to vhci_hcd.
  219. */
  220. stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
  221. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  222. return 0;
  223. }
  224. static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
  225. {
  226. struct usbip_device *ud = &sdev->ud;
  227. int valid = 0;
  228. if (pdu->base.devid == sdev->devid) {
  229. spin_lock_irq(&ud->lock);
  230. if (ud->status == SDEV_ST_USED) {
  231. /* A request is valid. */
  232. valid = 1;
  233. }
  234. spin_unlock_irq(&ud->lock);
  235. }
  236. return valid;
  237. }
  238. static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
  239. struct usbip_header *pdu)
  240. {
  241. struct stub_priv *priv;
  242. struct usbip_device *ud = &sdev->ud;
  243. unsigned long flags;
  244. spin_lock_irqsave(&sdev->priv_lock, flags);
  245. priv = kmem_cache_zalloc(stub_priv_cache, GFP_ATOMIC);
  246. if (!priv) {
  247. dev_err(&sdev->udev->dev, "alloc stub_priv\n");
  248. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  249. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  250. return NULL;
  251. }
  252. priv->seqnum = pdu->base.seqnum;
  253. priv->sdev = sdev;
  254. /*
  255. * After a stub_priv is linked to a list_head,
  256. * our error handler can free allocated data.
  257. */
  258. list_add_tail(&priv->list, &sdev->priv_init);
  259. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  260. return priv;
  261. }
  262. static int get_pipe(struct stub_device *sdev, int epnum, int dir)
  263. {
  264. struct usb_device *udev = sdev->udev;
  265. struct usb_host_endpoint *ep;
  266. struct usb_endpoint_descriptor *epd = NULL;
  267. if (dir == USBIP_DIR_IN)
  268. ep = udev->ep_in[epnum & 0x7f];
  269. else
  270. ep = udev->ep_out[epnum & 0x7f];
  271. if (!ep) {
  272. dev_err(&sdev->udev->dev, "no such endpoint?, %d\n",
  273. epnum);
  274. BUG();
  275. }
  276. epd = &ep->desc;
  277. if (usb_endpoint_xfer_control(epd)) {
  278. if (dir == USBIP_DIR_OUT)
  279. return usb_sndctrlpipe(udev, epnum);
  280. else
  281. return usb_rcvctrlpipe(udev, epnum);
  282. }
  283. if (usb_endpoint_xfer_bulk(epd)) {
  284. if (dir == USBIP_DIR_OUT)
  285. return usb_sndbulkpipe(udev, epnum);
  286. else
  287. return usb_rcvbulkpipe(udev, epnum);
  288. }
  289. if (usb_endpoint_xfer_int(epd)) {
  290. if (dir == USBIP_DIR_OUT)
  291. return usb_sndintpipe(udev, epnum);
  292. else
  293. return usb_rcvintpipe(udev, epnum);
  294. }
  295. if (usb_endpoint_xfer_isoc(epd)) {
  296. if (dir == USBIP_DIR_OUT)
  297. return usb_sndisocpipe(udev, epnum);
  298. else
  299. return usb_rcvisocpipe(udev, epnum);
  300. }
  301. /* NOT REACHED */
  302. dev_err(&sdev->udev->dev, "get pipe, epnum %d\n", epnum);
  303. return 0;
  304. }
  305. static void masking_bogus_flags(struct urb *urb)
  306. {
  307. int xfertype;
  308. struct usb_device *dev;
  309. struct usb_host_endpoint *ep;
  310. int is_out;
  311. unsigned int allowed;
  312. if (!urb || urb->hcpriv || !urb->complete)
  313. return;
  314. dev = urb->dev;
  315. if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED))
  316. return;
  317. ep = (usb_pipein(urb->pipe) ? dev->ep_in : dev->ep_out)
  318. [usb_pipeendpoint(urb->pipe)];
  319. if (!ep)
  320. return;
  321. xfertype = usb_endpoint_type(&ep->desc);
  322. if (xfertype == USB_ENDPOINT_XFER_CONTROL) {
  323. struct usb_ctrlrequest *setup =
  324. (struct usb_ctrlrequest *) urb->setup_packet;
  325. if (!setup)
  326. return;
  327. is_out = !(setup->bRequestType & USB_DIR_IN) ||
  328. !setup->wLength;
  329. } else {
  330. is_out = usb_endpoint_dir_out(&ep->desc);
  331. }
  332. /* enforce simple/standard policy */
  333. allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT |
  334. URB_DIR_MASK | URB_FREE_BUFFER);
  335. switch (xfertype) {
  336. case USB_ENDPOINT_XFER_BULK:
  337. if (is_out)
  338. allowed |= URB_ZERO_PACKET;
  339. /* FALLTHROUGH */
  340. case USB_ENDPOINT_XFER_CONTROL:
  341. allowed |= URB_NO_FSBR; /* only affects UHCI */
  342. /* FALLTHROUGH */
  343. default: /* all non-iso endpoints */
  344. if (!is_out)
  345. allowed |= URB_SHORT_NOT_OK;
  346. break;
  347. case USB_ENDPOINT_XFER_ISOC:
  348. allowed |= URB_ISO_ASAP;
  349. break;
  350. }
  351. urb->transfer_flags &= allowed;
  352. }
  353. static void stub_recv_cmd_submit(struct stub_device *sdev,
  354. struct usbip_header *pdu)
  355. {
  356. int ret;
  357. struct stub_priv *priv;
  358. struct usbip_device *ud = &sdev->ud;
  359. struct usb_device *udev = sdev->udev;
  360. int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction);
  361. priv = stub_priv_alloc(sdev, pdu);
  362. if (!priv)
  363. return;
  364. /* setup a urb */
  365. if (usb_pipeisoc(pipe))
  366. priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
  367. GFP_KERNEL);
  368. else
  369. priv->urb = usb_alloc_urb(0, GFP_KERNEL);
  370. if (!priv->urb) {
  371. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  372. return;
  373. }
  374. /* allocate urb transfer buffer, if needed */
  375. if (pdu->u.cmd_submit.transfer_buffer_length > 0) {
  376. priv->urb->transfer_buffer =
  377. kzalloc(pdu->u.cmd_submit.transfer_buffer_length,
  378. GFP_KERNEL);
  379. if (!priv->urb->transfer_buffer) {
  380. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  381. return;
  382. }
  383. }
  384. /* copy urb setup packet */
  385. priv->urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8,
  386. GFP_KERNEL);
  387. if (!priv->urb->setup_packet) {
  388. dev_err(&udev->dev, "allocate setup_packet\n");
  389. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  390. return;
  391. }
  392. /* set other members from the base header of pdu */
  393. priv->urb->context = (void *) priv;
  394. priv->urb->dev = udev;
  395. priv->urb->pipe = pipe;
  396. priv->urb->complete = stub_complete;
  397. usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
  398. if (usbip_recv_xbuff(ud, priv->urb) < 0)
  399. return;
  400. if (usbip_recv_iso(ud, priv->urb) < 0)
  401. return;
  402. /* no need to submit an intercepted request, but harmless? */
  403. tweak_special_requests(priv->urb);
  404. masking_bogus_flags(priv->urb);
  405. /* urb is now ready to submit */
  406. ret = usb_submit_urb(priv->urb, GFP_KERNEL);
  407. if (ret == 0)
  408. usbip_dbg_stub_rx("submit urb ok, seqnum %u\n",
  409. pdu->base.seqnum);
  410. else {
  411. dev_err(&udev->dev, "submit_urb error, %d\n", ret);
  412. usbip_dump_header(pdu);
  413. usbip_dump_urb(priv->urb);
  414. /*
  415. * Pessimistic.
  416. * This connection will be discarded.
  417. */
  418. usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
  419. }
  420. usbip_dbg_stub_rx("Leave\n");
  421. }
  422. /* recv a pdu */
  423. static void stub_rx_pdu(struct usbip_device *ud)
  424. {
  425. int ret;
  426. struct usbip_header pdu;
  427. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  428. struct device *dev = &sdev->udev->dev;
  429. usbip_dbg_stub_rx("Enter\n");
  430. memset(&pdu, 0, sizeof(pdu));
  431. /* receive a pdu header */
  432. ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
  433. if (ret != sizeof(pdu)) {
  434. dev_err(dev, "recv a header, %d\n", ret);
  435. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  436. return;
  437. }
  438. usbip_header_correct_endian(&pdu, 0);
  439. if (usbip_dbg_flag_stub_rx)
  440. usbip_dump_header(&pdu);
  441. if (!valid_request(sdev, &pdu)) {
  442. dev_err(dev, "recv invalid request\n");
  443. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  444. return;
  445. }
  446. switch (pdu.base.command) {
  447. case USBIP_CMD_UNLINK:
  448. stub_recv_cmd_unlink(sdev, &pdu);
  449. break;
  450. case USBIP_CMD_SUBMIT:
  451. stub_recv_cmd_submit(sdev, &pdu);
  452. break;
  453. default:
  454. /* NOTREACHED */
  455. dev_err(dev, "unknown pdu\n");
  456. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  457. break;
  458. }
  459. }
  460. int stub_rx_loop(void *data)
  461. {
  462. struct usbip_device *ud = data;
  463. while (!kthread_should_stop()) {
  464. if (usbip_event_happened(ud))
  465. break;
  466. stub_rx_pdu(ud);
  467. }
  468. return 0;
  469. }