1 // SPDX-License-Identifier: GPL-2.0
3 * The USB Monitor, inspired by Dave Harding's USBMon.
5 * This is a text format reader.
8 #include <linux/kernel.h>
9 #include <linux/list.h>
10 #include <linux/usb.h>
11 #include <linux/slab.h>
12 #include <linux/sched/signal.h>
13 #include <linux/time.h>
14 #include <linux/ktime.h>
15 #include <linux/export.h>
16 #include <linux/mutex.h>
17 #include <linux/debugfs.h>
18 #include <linux/scatterlist.h>
19 #include <linux/uaccess.h>
24 * No, we do not want arbitrarily long data strings.
25 * Use the binary interface if you want to capture bulk data!
30 * Defined by USB 2.0 clause 9.3, table 9.2.
35 * This limit exists to prevent OOMs when the user process stops reading.
36 * If usbmon were available to unprivileged processes, it might be open
37 * to a local DoS. But we have to keep to root in order to prevent
38 * password sniffing from HID devices.
40 #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
43 * Potentially unlimited number; we limit it for similar allocations.
44 * The usbfs limits this to 128, but we're not quite as generous.
48 #define PRINTF_DFL 250 /* with 5 ISOs segs */
53 unsigned int length; /* Unsigned here, signed in URB. Historic. */
56 struct mon_event_text {
57 struct list_head e_link;
58 int type; /* submit, complete, etc. */
59 unsigned long id; /* From pointer, most of the time */
66 int length; /* Depends on type: xfer length or act length */
73 int numdesc; /* Full number */
74 struct mon_iso_desc isodesc[ISODESC_MAX];
75 unsigned char setup[SETUP_MAX];
76 unsigned char data[DATA_MAX];
79 #define SLAB_NAME_SZ 30
80 struct mon_reader_text {
81 struct kmem_cache *e_slab;
83 struct list_head e_list;
84 struct mon_reader r; /* In C, parent class can be placed anywhere */
86 wait_queue_head_t wait;
91 struct mutex printf_lock;
93 char slab_name[SLAB_NAME_SZ];
96 static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
98 static void mon_text_ctor(void *);
100 struct mon_text_ptr {
105 static struct mon_event_text *
106 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
107 static void mon_text_read_head_t(struct mon_reader_text *rp,
108 struct mon_text_ptr *p, const struct mon_event_text *ep);
109 static void mon_text_read_head_u(struct mon_reader_text *rp,
110 struct mon_text_ptr *p, const struct mon_event_text *ep);
111 static void mon_text_read_statset(struct mon_reader_text *rp,
112 struct mon_text_ptr *p, const struct mon_event_text *ep);
113 static void mon_text_read_intstat(struct mon_reader_text *rp,
114 struct mon_text_ptr *p, const struct mon_event_text *ep);
115 static void mon_text_read_isostat(struct mon_reader_text *rp,
116 struct mon_text_ptr *p, const struct mon_event_text *ep);
117 static void mon_text_read_isodesc(struct mon_reader_text *rp,
118 struct mon_text_ptr *p, const struct mon_event_text *ep);
119 static void mon_text_read_data(struct mon_reader_text *rp,
120 struct mon_text_ptr *p, const struct mon_event_text *ep);
126 * May be called from an interrupt.
128 * This is called with the whole mon_bus locked, so no additional lock.
131 static inline char mon_text_get_setup(struct mon_event_text *ep,
132 struct urb *urb, char ev_type, struct mon_bus *mbus)
135 if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
138 if (urb->setup_packet == NULL)
139 return 'Z'; /* '0' would be not as pretty. */
141 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
145 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
146 int len, char ev_type, struct mon_bus *mbus)
163 if (urb->num_sgs == 0) {
164 src = urb->transfer_buffer;
166 return 'Z'; /* '0' would be not as pretty. */
168 struct scatterlist *sg = urb->sg;
170 if (PageHighMem(sg_page(sg)))
173 /* For the text interface we copy only the first sg buffer */
174 len = min_t(int, sg->length, len);
178 memcpy(ep->data, src, len);
182 static inline unsigned int mon_get_timestamp(void)
184 struct timespec64 now;
187 ktime_get_ts64(&now);
188 stamp = now.tv_sec & 0xFFF; /* 2^32 = 4294967296. Limit to 4096s. */
189 stamp = stamp * USEC_PER_SEC + now.tv_nsec / NSEC_PER_USEC;
193 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
194 char ev_type, int status)
196 struct mon_event_text *ep;
198 struct usb_iso_packet_descriptor *fp;
199 struct mon_iso_desc *dp;
202 stamp = mon_get_timestamp();
204 if (rp->nevents >= EVENT_MAX ||
205 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
206 rp->r.m_bus->cnt_text_lost++;
211 ep->id = (unsigned long) urb;
212 ep->busnum = urb->dev->bus->busnum;
213 ep->devnum = urb->dev->devnum;
214 ep->epnum = usb_endpoint_num(&urb->ep->desc);
215 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
216 ep->is_in = usb_urb_dir_in(urb);
218 ep->length = (ev_type == 'S') ?
219 urb->transfer_buffer_length : urb->actual_length;
220 /* Collecting status makes debugging sense for submits, too */
223 if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
224 ep->interval = urb->interval;
225 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
226 ep->interval = urb->interval;
227 ep->start_frame = urb->start_frame;
228 ep->error_count = urb->error_count;
230 ep->numdesc = urb->number_of_packets;
231 if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
232 urb->number_of_packets > 0) {
233 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
235 fp = urb->iso_frame_desc;
237 for (i = 0; i < ndesc; i++) {
238 dp->status = fp->status;
239 dp->offset = fp->offset;
240 dp->length = (ev_type == 'S') ?
241 fp->length : fp->actual_length;
245 /* Wasteful, but simple to understand: ISO 'C' is sparse. */
247 ep->length = urb->transfer_buffer_length;
250 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
251 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
255 list_add_tail(&ep->e_link, &rp->e_list);
259 static void mon_text_submit(void *data, struct urb *urb)
261 struct mon_reader_text *rp = data;
262 mon_text_event(rp, urb, 'S', -EINPROGRESS);
265 static void mon_text_complete(void *data, struct urb *urb, int status)
267 struct mon_reader_text *rp = data;
268 mon_text_event(rp, urb, 'C', status);
271 static void mon_text_error(void *data, struct urb *urb, int error)
273 struct mon_reader_text *rp = data;
274 struct mon_event_text *ep;
276 if (rp->nevents >= EVENT_MAX ||
277 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
278 rp->r.m_bus->cnt_text_lost++;
283 ep->id = (unsigned long) urb;
284 ep->busnum = urb->dev->bus->busnum;
285 ep->devnum = urb->dev->devnum;
286 ep->epnum = usb_endpoint_num(&urb->ep->desc);
287 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
288 ep->is_in = usb_urb_dir_in(urb);
289 ep->tstamp = mon_get_timestamp();
293 ep->setup_flag = '-';
297 list_add_tail(&ep->e_link, &rp->e_list);
302 * Fetch next event from the circular buffer.
304 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
305 struct mon_bus *mbus)
310 spin_lock_irqsave(&mbus->lock, flags);
311 if (list_empty(&rp->e_list)) {
312 spin_unlock_irqrestore(&mbus->lock, flags);
318 spin_unlock_irqrestore(&mbus->lock, flags);
319 return list_entry(p, struct mon_event_text, e_link);
324 static int mon_text_open(struct inode *inode, struct file *file)
326 struct mon_bus *mbus;
327 struct mon_reader_text *rp;
330 mutex_lock(&mon_lock);
331 mbus = inode->i_private;
333 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
338 INIT_LIST_HEAD(&rp->e_list);
339 init_waitqueue_head(&rp->wait);
340 mutex_init(&rp->printf_lock);
342 rp->printf_size = PRINTF_DFL;
343 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
344 if (rp->printf_buf == NULL) {
351 rp->r.rnf_submit = mon_text_submit;
352 rp->r.rnf_error = mon_text_error;
353 rp->r.rnf_complete = mon_text_complete;
355 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
356 rp->e_slab = kmem_cache_create(rp->slab_name,
357 sizeof(struct mon_event_text), sizeof(long), 0,
359 if (rp->e_slab == NULL) {
364 mon_reader_add(mbus, &rp->r);
366 file->private_data = rp;
367 mutex_unlock(&mon_lock);
371 // kmem_cache_destroy(rp->e_slab);
373 kfree(rp->printf_buf);
377 mutex_unlock(&mon_lock);
381 static ssize_t mon_text_copy_to_user(struct mon_reader_text *rp,
382 char __user * const buf, const size_t nbytes)
384 const size_t togo = min(nbytes, rp->printf_togo);
386 if (copy_to_user(buf, &rp->printf_buf[rp->printf_offset], togo))
388 rp->printf_togo -= togo;
389 rp->printf_offset += togo;
393 /* ppos is not advanced since the llseek operation is not permitted. */
394 static ssize_t mon_text_read_t(struct file *file, char __user *buf,
395 size_t nbytes, loff_t *ppos)
397 struct mon_reader_text *rp = file->private_data;
398 struct mon_event_text *ep;
399 struct mon_text_ptr ptr;
402 mutex_lock(&rp->printf_lock);
404 if (rp->printf_togo == 0) {
406 ep = mon_text_read_wait(rp, file);
408 mutex_unlock(&rp->printf_lock);
412 ptr.pbuf = rp->printf_buf;
413 ptr.limit = rp->printf_size;
415 mon_text_read_head_t(rp, &ptr, ep);
416 mon_text_read_statset(rp, &ptr, ep);
417 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
419 mon_text_read_data(rp, &ptr, ep);
421 rp->printf_togo = ptr.cnt;
422 rp->printf_offset = 0;
424 kmem_cache_free(rp->e_slab, ep);
427 ret = mon_text_copy_to_user(rp, buf, nbytes);
428 mutex_unlock(&rp->printf_lock);
432 /* ppos is not advanced since the llseek operation is not permitted. */
433 static ssize_t mon_text_read_u(struct file *file, char __user *buf,
434 size_t nbytes, loff_t *ppos)
436 struct mon_reader_text *rp = file->private_data;
437 struct mon_event_text *ep;
438 struct mon_text_ptr ptr;
441 mutex_lock(&rp->printf_lock);
443 if (rp->printf_togo == 0) {
445 ep = mon_text_read_wait(rp, file);
447 mutex_unlock(&rp->printf_lock);
451 ptr.pbuf = rp->printf_buf;
452 ptr.limit = rp->printf_size;
454 mon_text_read_head_u(rp, &ptr, ep);
455 if (ep->type == 'E') {
456 mon_text_read_statset(rp, &ptr, ep);
457 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
458 mon_text_read_isostat(rp, &ptr, ep);
459 mon_text_read_isodesc(rp, &ptr, ep);
460 } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
461 mon_text_read_intstat(rp, &ptr, ep);
463 mon_text_read_statset(rp, &ptr, ep);
465 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
467 mon_text_read_data(rp, &ptr, ep);
469 rp->printf_togo = ptr.cnt;
470 rp->printf_offset = 0;
472 kmem_cache_free(rp->e_slab, ep);
475 ret = mon_text_copy_to_user(rp, buf, nbytes);
476 mutex_unlock(&rp->printf_lock);
480 static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
483 struct mon_bus *mbus = rp->r.m_bus;
484 DECLARE_WAITQUEUE(waita, current);
485 struct mon_event_text *ep;
487 add_wait_queue(&rp->wait, &waita);
488 set_current_state(TASK_INTERRUPTIBLE);
489 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
490 if (file->f_flags & O_NONBLOCK) {
491 set_current_state(TASK_RUNNING);
492 remove_wait_queue(&rp->wait, &waita);
493 return ERR_PTR(-EWOULDBLOCK);
496 * We do not count nwaiters, because ->release is supposed
497 * to be called when all openers are gone only.
500 if (signal_pending(current)) {
501 remove_wait_queue(&rp->wait, &waita);
502 return ERR_PTR(-EINTR);
504 set_current_state(TASK_INTERRUPTIBLE);
506 set_current_state(TASK_RUNNING);
507 remove_wait_queue(&rp->wait, &waita);
511 static void mon_text_read_head_t(struct mon_reader_text *rp,
512 struct mon_text_ptr *p, const struct mon_event_text *ep)
516 udir = (ep->is_in ? 'i' : 'o');
517 switch (ep->xfertype) {
518 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
519 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
520 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
521 default: /* PIPE_BULK */ utype = 'B';
523 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
524 "%lx %u %c %c%c:%03u:%02u",
525 ep->id, ep->tstamp, ep->type,
526 utype, udir, ep->devnum, ep->epnum);
529 static void mon_text_read_head_u(struct mon_reader_text *rp,
530 struct mon_text_ptr *p, const struct mon_event_text *ep)
534 udir = (ep->is_in ? 'i' : 'o');
535 switch (ep->xfertype) {
536 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
537 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
538 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
539 default: /* PIPE_BULK */ utype = 'B';
541 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
542 "%lx %u %c %c%c:%d:%03u:%u",
543 ep->id, ep->tstamp, ep->type,
544 utype, udir, ep->busnum, ep->devnum, ep->epnum);
547 static void mon_text_read_statset(struct mon_reader_text *rp,
548 struct mon_text_ptr *p, const struct mon_event_text *ep)
551 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
552 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
553 " s %02x %02x %04x %04x %04x",
556 (ep->setup[3] << 8) | ep->setup[2],
557 (ep->setup[5] << 8) | ep->setup[4],
558 (ep->setup[7] << 8) | ep->setup[6]);
559 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
560 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
561 " %c __ __ ____ ____ ____", ep->setup_flag);
562 } else { /* No setup for this kind of URB */
563 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
568 static void mon_text_read_intstat(struct mon_reader_text *rp,
569 struct mon_text_ptr *p, const struct mon_event_text *ep)
571 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
572 " %d:%d", ep->status, ep->interval);
575 static void mon_text_read_isostat(struct mon_reader_text *rp,
576 struct mon_text_ptr *p, const struct mon_event_text *ep)
578 if (ep->type == 'S') {
579 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
580 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
582 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
584 ep->status, ep->interval, ep->start_frame, ep->error_count);
588 static void mon_text_read_isodesc(struct mon_reader_text *rp,
589 struct mon_text_ptr *p, const struct mon_event_text *ep)
591 int ndesc; /* Display this many */
593 const struct mon_iso_desc *dp;
595 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
598 if (ndesc > ISODESC_MAX)
603 for (i = 0; i < ndesc; i++) {
604 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
605 " %d:%u:%u", dp->status, dp->offset, dp->length);
610 static void mon_text_read_data(struct mon_reader_text *rp,
611 struct mon_text_ptr *p, const struct mon_event_text *ep)
615 if ((data_len = ep->length) > 0) {
616 if (ep->data_flag == 0) {
617 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
619 if (data_len >= DATA_MAX)
621 for (i = 0; i < data_len; i++) {
623 p->cnt += snprintf(p->pbuf + p->cnt,
627 p->cnt += snprintf(p->pbuf + p->cnt,
629 "%02x", ep->data[i]);
631 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
634 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
635 " %c\n", ep->data_flag);
638 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
642 static int mon_text_release(struct inode *inode, struct file *file)
644 struct mon_reader_text *rp = file->private_data;
645 struct mon_bus *mbus;
646 /* unsigned long flags; */
648 struct mon_event_text *ep;
650 mutex_lock(&mon_lock);
651 mbus = inode->i_private;
653 if (mbus->nreaders <= 0) {
654 printk(KERN_ERR TAG ": consistency error on close\n");
655 mutex_unlock(&mon_lock);
658 mon_reader_del(mbus, &rp->r);
661 * In theory, e_list is protected by mbus->lock. However,
662 * after mon_reader_del has finished, the following is the case:
663 * - we are not on reader list anymore, so new events won't be added;
664 * - whole mbus may be dropped if it was orphaned.
665 * So, we better not touch mbus.
667 /* spin_lock_irqsave(&mbus->lock, flags); */
668 while (!list_empty(&rp->e_list)) {
670 ep = list_entry(p, struct mon_event_text, e_link);
673 kmem_cache_free(rp->e_slab, ep);
675 /* spin_unlock_irqrestore(&mbus->lock, flags); */
677 kmem_cache_destroy(rp->e_slab);
678 kfree(rp->printf_buf);
681 mutex_unlock(&mon_lock);
685 static const struct file_operations mon_fops_text_t = {
686 .owner = THIS_MODULE,
687 .open = mon_text_open,
689 .read = mon_text_read_t,
690 .release = mon_text_release,
693 static const struct file_operations mon_fops_text_u = {
694 .owner = THIS_MODULE,
695 .open = mon_text_open,
697 .read = mon_text_read_u,
698 .release = mon_text_release,
701 int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
703 enum { NAMESZ = 10 };
705 int busnum = ubus? ubus->busnum: 0;
712 rc = snprintf(name, NAMESZ, "%dt", busnum);
713 if (rc <= 0 || rc >= NAMESZ)
715 mbus->dent_t = debugfs_create_file(name, 0600, mon_dir, mbus,
719 rc = snprintf(name, NAMESZ, "%du", busnum);
720 if (rc <= 0 || rc >= NAMESZ)
722 mbus->dent_u = debugfs_create_file(name, 0600, mon_dir, mbus,
725 rc = snprintf(name, NAMESZ, "%ds", busnum);
726 if (rc <= 0 || rc >= NAMESZ)
728 mbus->dent_s = debugfs_create_file(name, 0600, mon_dir, mbus,
734 debugfs_remove(mbus->dent_u);
738 debugfs_remove(mbus->dent_t);
745 void mon_text_del(struct mon_bus *mbus)
747 debugfs_remove(mbus->dent_u);
748 debugfs_remove(mbus->dent_t);
749 debugfs_remove(mbus->dent_s);
753 * Slab interface: constructor.
755 static void mon_text_ctor(void *mem)
758 * Nothing to initialize. No, really!
759 * So, we fill it with garbage to emulate a reused object.
761 memset(mem, 0xe5, sizeof(struct mon_event_text));
764 int __init mon_text_init(void)
766 mon_dir = debugfs_create_dir("usbmon", usb_debug_root);
770 void mon_text_exit(void)
772 debugfs_remove(mon_dir);