JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Bert Vermeulen <bert@biot.com> |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that: (1) source code distributions |
| 6 | * retain the above copyright notice and this paragraph in its entirety, (2) |
| 7 | * distributions including binary code include the above copyright notice and |
| 8 | * this paragraph in its entirety in the documentation or other materials |
| 9 | * provided with the distribution, and (3) all advertising materials mentioning |
| 10 | * features or use of this software display the following acknowledgement: |
| 11 | * ``This product includes software developed by Paolo Abeni.'' |
| 12 | * The name of author may not be used to endorse or promote products derived |
| 13 | * from this software without specific prior written permission. |
| 14 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
| 15 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 16 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | * |
| 18 | * Support for USB packets |
| 19 | * |
| 20 | */ |
| 21 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 22 | /* \summary: USB printer */ |
| 23 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include "config.h" |
| 26 | #endif |
| 27 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 28 | #include <netdissect-stdinc.h> |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 30 | #include "netdissect.h" |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 31 | |
| 32 | |
| 33 | #if defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX) |
| 34 | #include <pcap/usb.h> |
| 35 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 36 | static const char tstr[] = "[|usb]"; |
| 37 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 38 | /* returns direction: 1=inbound 2=outbound -1=invalid */ |
| 39 | static int |
| 40 | get_direction(int transfer_type, int event_type) |
| 41 | { |
| 42 | int direction; |
| 43 | |
| 44 | direction = -1; |
| 45 | switch(transfer_type){ |
| 46 | case URB_BULK: |
| 47 | case URB_CONTROL: |
| 48 | case URB_ISOCHRONOUS: |
| 49 | switch(event_type) |
| 50 | { |
| 51 | case URB_SUBMIT: |
| 52 | direction = 2; |
| 53 | break; |
| 54 | case URB_COMPLETE: |
| 55 | case URB_ERROR: |
| 56 | direction = 1; |
| 57 | break; |
| 58 | default: |
| 59 | direction = -1; |
| 60 | } |
| 61 | break; |
| 62 | case URB_INTERRUPT: |
| 63 | switch(event_type) |
| 64 | { |
| 65 | case URB_SUBMIT: |
| 66 | direction = 1; |
| 67 | break; |
| 68 | case URB_COMPLETE: |
| 69 | case URB_ERROR: |
| 70 | direction = 2; |
| 71 | break; |
| 72 | default: |
| 73 | direction = -1; |
| 74 | } |
| 75 | break; |
| 76 | default: |
| 77 | direction = -1; |
| 78 | } |
| 79 | |
| 80 | return direction; |
| 81 | } |
| 82 | |
| 83 | static void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 84 | usb_header_print(netdissect_options *ndo, const pcap_usb_header *uh) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 85 | { |
| 86 | int direction; |
| 87 | |
| 88 | switch(uh->transfer_type) |
| 89 | { |
| 90 | case URB_ISOCHRONOUS: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 91 | ND_PRINT((ndo, "ISOCHRONOUS")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 92 | break; |
| 93 | case URB_INTERRUPT: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 94 | ND_PRINT((ndo, "INTERRUPT")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 95 | break; |
| 96 | case URB_CONTROL: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 97 | ND_PRINT((ndo, "CONTROL")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 98 | break; |
| 99 | case URB_BULK: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 100 | ND_PRINT((ndo, "BULK")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 101 | break; |
| 102 | default: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 103 | ND_PRINT((ndo, " ?")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | switch(uh->event_type) |
| 107 | { |
| 108 | case URB_SUBMIT: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 109 | ND_PRINT((ndo, " SUBMIT")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 110 | break; |
| 111 | case URB_COMPLETE: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 112 | ND_PRINT((ndo, " COMPLETE")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 113 | break; |
| 114 | case URB_ERROR: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 115 | ND_PRINT((ndo, " ERROR")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 116 | break; |
| 117 | default: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 118 | ND_PRINT((ndo, " ?")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | direction = get_direction(uh->transfer_type, uh->event_type); |
| 122 | if(direction == 1) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 123 | ND_PRINT((ndo, " from")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 124 | else if(direction == 2) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 125 | ND_PRINT((ndo, " to")); |
| 126 | ND_PRINT((ndo, " %d:%d:%d", uh->bus_id, uh->device_address, uh->endpoint_number & 0x7f)); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * This is the top level routine of the printer for captures with a |
| 131 | * 48-byte header. |
| 132 | * |
| 133 | * 'p' points to the header of the packet, 'h->ts' is the timestamp, |
| 134 | * 'h->len' is the length of the packet off the wire, and 'h->caplen' |
| 135 | * is the number of bytes actually captured. |
| 136 | */ |
| 137 | u_int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 138 | usb_linux_48_byte_print(netdissect_options *ndo, const struct pcap_pkthdr *h, |
| 139 | register const u_char *p) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 140 | { |
| 141 | if (h->caplen < sizeof(pcap_usb_header)) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 142 | ND_PRINT((ndo, "%s", tstr)); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 143 | return(sizeof(pcap_usb_header)); |
| 144 | } |
| 145 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 146 | usb_header_print(ndo, (const pcap_usb_header *) p); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 147 | |
| 148 | return(sizeof(pcap_usb_header)); |
| 149 | } |
| 150 | |
| 151 | #ifdef DLT_USB_LINUX_MMAPPED |
| 152 | /* |
| 153 | * This is the top level routine of the printer for captures with a |
| 154 | * 64-byte header. |
| 155 | * |
| 156 | * 'p' points to the header of the packet, 'h->ts' is the timestamp, |
| 157 | * 'h->len' is the length of the packet off the wire, and 'h->caplen' |
| 158 | * is the number of bytes actually captured. |
| 159 | */ |
| 160 | u_int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 161 | usb_linux_64_byte_print(netdissect_options *ndo, const struct pcap_pkthdr *h, |
| 162 | register const u_char *p) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 163 | { |
| 164 | if (h->caplen < sizeof(pcap_usb_header_mmapped)) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 165 | ND_PRINT((ndo, "%s", tstr)); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 166 | return(sizeof(pcap_usb_header_mmapped)); |
| 167 | } |
| 168 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 169 | usb_header_print(ndo, (const pcap_usb_header *) p); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 170 | |
| 171 | return(sizeof(pcap_usb_header_mmapped)); |
| 172 | } |
| 173 | #endif /* DLT_USB_LINUX_MMAPPED */ |
| 174 | |
| 175 | #endif /* defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX) */ |
| 176 | |