mon_text.c 11 KB

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