mon_text.c 13 KB

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