mon_text.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * The USB Monitor, inspired by Dave Harding's USBMon.
  3. *
  4. * This is a text format reader.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/list.h>
  8. #include <linux/usb.h>
  9. #include <linux/time.h>
  10. #include <linux/mutex.h>
  11. #include <asm/uaccess.h>
  12. #include "usb_mon.h"
  13. /*
  14. * No, we do not want arbitrarily long data strings.
  15. * Use the binary interface if you want to capture bulk data!
  16. */
  17. #define DATA_MAX 32
  18. /*
  19. * Defined by USB 2.0 clause 9.3, table 9.2.
  20. */
  21. #define SETUP_MAX 8
  22. /*
  23. * This limit exists to prevent OOMs when the user process stops reading.
  24. * If usbmon were available to unprivileged processes, it might be open
  25. * to a local DoS. But we have to keep to root in order to prevent
  26. * password sniffing from HID devices.
  27. */
  28. #define EVENT_MAX (2*PAGE_SIZE / sizeof(struct mon_event_text))
  29. #define PRINTF_DFL 160
  30. struct mon_event_text {
  31. struct list_head e_link;
  32. int type; /* submit, complete, etc. */
  33. unsigned int pipe; /* Pipe */
  34. unsigned long id; /* From pointer, most of the time */
  35. unsigned int tstamp;
  36. int length; /* Depends on type: xfer length or act length */
  37. int status;
  38. char setup_flag;
  39. char data_flag;
  40. unsigned char setup[SETUP_MAX];
  41. unsigned char data[DATA_MAX];
  42. };
  43. #define SLAB_NAME_SZ 30
  44. struct mon_reader_text {
  45. kmem_cache_t *e_slab;
  46. int nevents;
  47. struct list_head e_list;
  48. struct mon_reader r; /* In C, parent class can be placed anywhere */
  49. wait_queue_head_t wait;
  50. int printf_size;
  51. char *printf_buf;
  52. struct mutex printf_lock;
  53. char slab_name[SLAB_NAME_SZ];
  54. };
  55. static void mon_text_ctor(void *, kmem_cache_t *, unsigned long);
  56. /*
  57. * mon_text_submit
  58. * mon_text_complete
  59. *
  60. * May be called from an interrupt.
  61. *
  62. * This is called with the whole mon_bus locked, so no additional lock.
  63. */
  64. static inline char mon_text_get_setup(struct mon_event_text *ep,
  65. struct urb *urb, char ev_type, struct mon_bus *mbus)
  66. {
  67. if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
  68. return '-';
  69. if (mbus->uses_dma && (urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  70. return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
  71. if (urb->setup_packet == NULL)
  72. return 'Z'; /* '0' would be not as pretty. */
  73. memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
  74. return 0;
  75. }
  76. static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
  77. int len, char ev_type, struct mon_bus *mbus)
  78. {
  79. int pipe = urb->pipe;
  80. if (len <= 0)
  81. return 'L';
  82. if (len >= DATA_MAX)
  83. len = DATA_MAX;
  84. if (usb_pipein(pipe)) {
  85. if (ev_type == 'S')
  86. return '<';
  87. } else {
  88. if (ev_type == 'C')
  89. return '>';
  90. }
  91. /*
  92. * The check to see if it's safe to poke at data has an enormous
  93. * number of corner cases, but it seems that the following is
  94. * more or less safe.
  95. *
  96. * We do not even try to look at transfer_buffer, because it can
  97. * contain non-NULL garbage in case the upper level promised to
  98. * set DMA for the HCD.
  99. */
  100. if (mbus->uses_dma && (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  101. return mon_dmapeek(ep->data, urb->transfer_dma, len);
  102. if (urb->transfer_buffer == NULL)
  103. return 'Z'; /* '0' would be not as pretty. */
  104. memcpy(ep->data, urb->transfer_buffer, len);
  105. return 0;
  106. }
  107. static inline unsigned int mon_get_timestamp(void)
  108. {
  109. struct timeval tval;
  110. unsigned int stamp;
  111. do_gettimeofday(&tval);
  112. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
  113. stamp = stamp * 1000000 + tval.tv_usec;
  114. return stamp;
  115. }
  116. static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
  117. char ev_type)
  118. {
  119. struct mon_event_text *ep;
  120. unsigned int stamp;
  121. stamp = mon_get_timestamp();
  122. if (rp->nevents >= EVENT_MAX ||
  123. (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) {
  124. rp->r.m_bus->cnt_text_lost++;
  125. return;
  126. }
  127. ep->type = ev_type;
  128. ep->pipe = urb->pipe;
  129. ep->id = (unsigned long) urb;
  130. ep->tstamp = stamp;
  131. ep->length = (ev_type == 'S') ?
  132. urb->transfer_buffer_length : urb->actual_length;
  133. /* Collecting status makes debugging sense for submits, too */
  134. ep->status = urb->status;
  135. ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
  136. ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
  137. rp->r.m_bus);
  138. rp->nevents++;
  139. list_add_tail(&ep->e_link, &rp->e_list);
  140. wake_up(&rp->wait);
  141. }
  142. static void mon_text_submit(void *data, struct urb *urb)
  143. {
  144. struct mon_reader_text *rp = data;
  145. mon_text_event(rp, urb, 'S');
  146. }
  147. static void mon_text_complete(void *data, struct urb *urb)
  148. {
  149. struct mon_reader_text *rp = data;
  150. mon_text_event(rp, urb, 'C');
  151. }
  152. static void mon_text_error(void *data, struct urb *urb, int error)
  153. {
  154. struct mon_reader_text *rp = data;
  155. struct mon_event_text *ep;
  156. if (rp->nevents >= EVENT_MAX ||
  157. (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) {
  158. rp->r.m_bus->cnt_text_lost++;
  159. return;
  160. }
  161. ep->type = 'E';
  162. ep->pipe = urb->pipe;
  163. ep->id = (unsigned long) urb;
  164. ep->tstamp = 0;
  165. ep->length = 0;
  166. ep->status = error;
  167. ep->setup_flag = '-';
  168. ep->data_flag = 'E';
  169. rp->nevents++;
  170. list_add_tail(&ep->e_link, &rp->e_list);
  171. wake_up(&rp->wait);
  172. }
  173. /*
  174. * Fetch next event from the circular buffer.
  175. */
  176. static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
  177. struct mon_bus *mbus)
  178. {
  179. struct list_head *p;
  180. unsigned long flags;
  181. spin_lock_irqsave(&mbus->lock, flags);
  182. if (list_empty(&rp->e_list)) {
  183. spin_unlock_irqrestore(&mbus->lock, flags);
  184. return NULL;
  185. }
  186. p = rp->e_list.next;
  187. list_del(p);
  188. --rp->nevents;
  189. spin_unlock_irqrestore(&mbus->lock, flags);
  190. return list_entry(p, struct mon_event_text, e_link);
  191. }
  192. /*
  193. */
  194. static int mon_text_open(struct inode *inode, struct file *file)
  195. {
  196. struct mon_bus *mbus;
  197. struct usb_bus *ubus;
  198. struct mon_reader_text *rp;
  199. int rc;
  200. mutex_lock(&mon_lock);
  201. mbus = inode->i_private;
  202. ubus = mbus->u_bus;
  203. rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
  204. if (rp == NULL) {
  205. rc = -ENOMEM;
  206. goto err_alloc;
  207. }
  208. INIT_LIST_HEAD(&rp->e_list);
  209. init_waitqueue_head(&rp->wait);
  210. mutex_init(&rp->printf_lock);
  211. rp->printf_size = PRINTF_DFL;
  212. rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
  213. if (rp->printf_buf == NULL) {
  214. rc = -ENOMEM;
  215. goto err_alloc_pr;
  216. }
  217. rp->r.m_bus = mbus;
  218. rp->r.r_data = rp;
  219. rp->r.rnf_submit = mon_text_submit;
  220. rp->r.rnf_error = mon_text_error;
  221. rp->r.rnf_complete = mon_text_complete;
  222. snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
  223. (long)rp);
  224. rp->e_slab = kmem_cache_create(rp->slab_name,
  225. sizeof(struct mon_event_text), sizeof(long), 0,
  226. mon_text_ctor, NULL);
  227. if (rp->e_slab == NULL) {
  228. rc = -ENOMEM;
  229. goto err_slab;
  230. }
  231. mon_reader_add(mbus, &rp->r);
  232. file->private_data = rp;
  233. mutex_unlock(&mon_lock);
  234. return 0;
  235. // err_busy:
  236. // kmem_cache_destroy(rp->e_slab);
  237. err_slab:
  238. kfree(rp->printf_buf);
  239. err_alloc_pr:
  240. kfree(rp);
  241. err_alloc:
  242. mutex_unlock(&mon_lock);
  243. return rc;
  244. }
  245. /*
  246. * For simplicity, we read one record in one system call and throw out
  247. * what does not fit. This means that the following does not work:
  248. * dd if=/dbg/usbmon/0t bs=10
  249. * Also, we do not allow seeks and do not bother advancing the offset.
  250. */
  251. static ssize_t mon_text_read(struct file *file, char __user *buf,
  252. size_t nbytes, loff_t *ppos)
  253. {
  254. struct mon_reader_text *rp = file->private_data;
  255. struct mon_bus *mbus = rp->r.m_bus;
  256. DECLARE_WAITQUEUE(waita, current);
  257. struct mon_event_text *ep;
  258. int cnt, limit;
  259. char *pbuf;
  260. char udir, utype;
  261. int data_len, i;
  262. add_wait_queue(&rp->wait, &waita);
  263. set_current_state(TASK_INTERRUPTIBLE);
  264. while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
  265. if (file->f_flags & O_NONBLOCK) {
  266. set_current_state(TASK_RUNNING);
  267. remove_wait_queue(&rp->wait, &waita);
  268. return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
  269. }
  270. /*
  271. * We do not count nwaiters, because ->release is supposed
  272. * to be called when all openers are gone only.
  273. */
  274. schedule();
  275. if (signal_pending(current)) {
  276. remove_wait_queue(&rp->wait, &waita);
  277. return -EINTR;
  278. }
  279. set_current_state(TASK_INTERRUPTIBLE);
  280. }
  281. set_current_state(TASK_RUNNING);
  282. remove_wait_queue(&rp->wait, &waita);
  283. mutex_lock(&rp->printf_lock);
  284. cnt = 0;
  285. pbuf = rp->printf_buf;
  286. limit = rp->printf_size;
  287. udir = usb_pipein(ep->pipe) ? 'i' : 'o';
  288. switch (usb_pipetype(ep->pipe)) {
  289. case PIPE_ISOCHRONOUS: utype = 'Z'; break;
  290. case PIPE_INTERRUPT: utype = 'I'; break;
  291. case PIPE_CONTROL: utype = 'C'; break;
  292. default: /* PIPE_BULK */ utype = 'B';
  293. }
  294. cnt += snprintf(pbuf + cnt, limit - cnt,
  295. "%lx %u %c %c%c:%03u:%02u",
  296. ep->id, ep->tstamp, ep->type,
  297. utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
  298. if (ep->setup_flag == 0) { /* Setup packet is present and captured */
  299. cnt += snprintf(pbuf + cnt, limit - cnt,
  300. " s %02x %02x %04x %04x %04x",
  301. ep->setup[0],
  302. ep->setup[1],
  303. (ep->setup[3] << 8) | ep->setup[2],
  304. (ep->setup[5] << 8) | ep->setup[4],
  305. (ep->setup[7] << 8) | ep->setup[6]);
  306. } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
  307. cnt += snprintf(pbuf + cnt, limit - cnt,
  308. " %c __ __ ____ ____ ____", ep->setup_flag);
  309. } else { /* No setup for this kind of URB */
  310. cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->status);
  311. }
  312. cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->length);
  313. if ((data_len = ep->length) > 0) {
  314. if (ep->data_flag == 0) {
  315. cnt += snprintf(pbuf + cnt, limit - cnt, " =");
  316. if (data_len >= DATA_MAX)
  317. data_len = DATA_MAX;
  318. for (i = 0; i < data_len; i++) {
  319. if (i % 4 == 0) {
  320. cnt += snprintf(pbuf + cnt, limit - cnt,
  321. " ");
  322. }
  323. cnt += snprintf(pbuf + cnt, limit - cnt,
  324. "%02x", ep->data[i]);
  325. }
  326. cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
  327. } else {
  328. cnt += snprintf(pbuf + cnt, limit - cnt,
  329. " %c\n", ep->data_flag);
  330. }
  331. } else {
  332. cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
  333. }
  334. if (copy_to_user(buf, rp->printf_buf, cnt))
  335. cnt = -EFAULT;
  336. mutex_unlock(&rp->printf_lock);
  337. kmem_cache_free(rp->e_slab, ep);
  338. return cnt;
  339. }
  340. static int mon_text_release(struct inode *inode, struct file *file)
  341. {
  342. struct mon_reader_text *rp = file->private_data;
  343. struct mon_bus *mbus;
  344. /* unsigned long flags; */
  345. struct list_head *p;
  346. struct mon_event_text *ep;
  347. mutex_lock(&mon_lock);
  348. mbus = inode->i_private;
  349. if (mbus->nreaders <= 0) {
  350. printk(KERN_ERR TAG ": consistency error on close\n");
  351. mutex_unlock(&mon_lock);
  352. return 0;
  353. }
  354. mon_reader_del(mbus, &rp->r);
  355. /*
  356. * In theory, e_list is protected by mbus->lock. However,
  357. * after mon_reader_del has finished, the following is the case:
  358. * - we are not on reader list anymore, so new events won't be added;
  359. * - whole mbus may be dropped if it was orphaned.
  360. * So, we better not touch mbus.
  361. */
  362. /* spin_lock_irqsave(&mbus->lock, flags); */
  363. while (!list_empty(&rp->e_list)) {
  364. p = rp->e_list.next;
  365. ep = list_entry(p, struct mon_event_text, e_link);
  366. list_del(p);
  367. --rp->nevents;
  368. kmem_cache_free(rp->e_slab, ep);
  369. }
  370. /* spin_unlock_irqrestore(&mbus->lock, flags); */
  371. kmem_cache_destroy(rp->e_slab);
  372. kfree(rp->printf_buf);
  373. kfree(rp);
  374. mutex_unlock(&mon_lock);
  375. return 0;
  376. }
  377. const struct file_operations mon_fops_text = {
  378. .owner = THIS_MODULE,
  379. .open = mon_text_open,
  380. .llseek = no_llseek,
  381. .read = mon_text_read,
  382. /* .write = mon_text_write, */
  383. /* .poll = mon_text_poll, */
  384. /* .ioctl = mon_text_ioctl, */
  385. .release = mon_text_release,
  386. };
  387. /*
  388. * Slab interface: constructor.
  389. */
  390. static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags)
  391. {
  392. /*
  393. * Nothing to initialize. No, really!
  394. * So, we fill it with garbage to emulate a reused object.
  395. */
  396. memset(mem, 0xe5, sizeof(struct mon_event_text));
  397. }