PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/libpcap/pcap-canusb-linux.c

https://gitlab.com/g10h4ck/nmap-gsoc2015
C | 474 lines | 313 code | 97 blank | 64 comment | 56 complexity | 8da86ad307a29e231a695c9c484e1ce1 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
  1. /*
  2. * Copyright (c) 2009 Felix Obenhuber
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote
  15. * products derived from this software without specific prior written
  16. * permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * Sockettrace sniffing API implementation for Linux platform
  31. * By Felix Obenhuber <felix@obenhuber.de>
  32. *
  33. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. #include <libusb-1.0/libusb.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <fcntl.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include <pthread.h>
  44. #include "pcap-int.h"
  45. #include "pcap-canusb-linux.h"
  46. #define CANUSB_IFACE "canusb"
  47. #define CANUSB_VID 0x0403
  48. #define CANUSB_PID 0x8990
  49. #define USE_THREAD 1
  50. #if USE_THREAD == 0
  51. #include <signal.h>
  52. #endif
  53. /* forward declaration */
  54. static int canusb_activate(pcap_t *);
  55. static int canusb_read_linux(pcap_t *, int , pcap_handler , u_char *);
  56. static int canusb_inject_linux(pcap_t *, const void *, size_t);
  57. static int canusb_setfilter_linux(pcap_t *, struct bpf_program *);
  58. static int canusb_setdirection_linux(pcap_t *, pcap_direction_t);
  59. static int canusb_stats_linux(pcap_t *, struct pcap_stat *);
  60. struct CAN_Msg
  61. {
  62. uint32_t timestamp;
  63. uint32_t id;
  64. uint32_t length;
  65. uint8_t data[8];
  66. };
  67. /*
  68. * Private data for capturing on Linux CANbus USB devices.
  69. */
  70. struct pcap_canusb {
  71. libusb_context *ctx;
  72. libusb_device_handle *dev;
  73. pthread_t worker;
  74. int rdpipe, wrpipe;
  75. volatile int loop;
  76. };
  77. int canusb_findalldevs(pcap_if_t **alldevsp, char *err_str)
  78. {
  79. libusb_context *fdctx;
  80. libusb_device** devs;
  81. unsigned char sernum[65];
  82. int cnt, i;
  83. if (libusb_init(&fdctx) != 0) {
  84. /*
  85. * XXX - if this doesn't just mean "no USB file system mounted",
  86. * perhaps we should report a real error rather than just
  87. * saying "no CANUSB devices".
  88. */
  89. return 0;
  90. }
  91. cnt = libusb_get_device_list(fdctx,&devs);
  92. for(i=0;i<cnt;i++)
  93. {
  94. int ret;
  95. // Check if this device is interesting.
  96. struct libusb_device_descriptor desc;
  97. libusb_get_device_descriptor(devs[i],&desc);
  98. if ((desc.idVendor != CANUSB_VID) || (desc.idProduct != CANUSB_PID))
  99. continue; //It is not, check next device
  100. //It is!
  101. libusb_device_handle *dh = NULL;
  102. if ((ret = libusb_open(devs[i],&dh)) == 0)
  103. {
  104. char dev_name[30];
  105. char dev_descr[50];
  106. int n = libusb_get_string_descriptor_ascii(dh,desc.iSerialNumber,sernum,64);
  107. sernum[n] = 0;
  108. snprintf(dev_name, 30, CANUSB_IFACE"%s", sernum);
  109. snprintf(dev_descr, 50, "CanUSB [%s]", sernum);
  110. libusb_close(dh);
  111. if (pcap_add_if(alldevsp, dev_name, 0, dev_descr, err_str) < 0)
  112. {
  113. libusb_free_device_list(devs,1);
  114. libusb_exit(fdctx);
  115. return -1;
  116. }
  117. }
  118. }
  119. libusb_free_device_list(devs,1);
  120. libusb_exit(fdctx);
  121. return 0;
  122. }
  123. static libusb_device_handle* canusb_opendevice(struct libusb_context *ctx, char* devserial)
  124. {
  125. libusb_device** devs;
  126. unsigned char serial[65];
  127. int cnt,i,n;
  128. cnt = libusb_get_device_list(ctx,&devs);
  129. for(i=0;i<cnt;i++)
  130. {
  131. // Check if this device is interesting.
  132. struct libusb_device_descriptor desc;
  133. libusb_get_device_descriptor(devs[i],&desc);
  134. if ((desc.idVendor != CANUSB_VID) || (desc.idProduct != CANUSB_PID))
  135. continue;
  136. //Found one!
  137. libusb_device_handle *dh = NULL;
  138. if (libusb_open(devs[i],&dh) != 0) continue;
  139. n = libusb_get_string_descriptor_ascii(dh,desc.iSerialNumber,serial,64);
  140. serial[n] = 0;
  141. if ((devserial) && (strcmp((char *)serial,devserial) != 0))
  142. {
  143. libusb_close(dh);
  144. continue;
  145. }
  146. if ((libusb_kernel_driver_active(dh,0)) && (libusb_detach_kernel_driver(dh,0) != 0))
  147. {
  148. libusb_close(dh);
  149. continue;
  150. }
  151. if (libusb_set_configuration(dh,1) != 0)
  152. {
  153. libusb_close(dh);
  154. continue;
  155. }
  156. if (libusb_claim_interface(dh,0) != 0)
  157. {
  158. libusb_close(dh);
  159. continue;
  160. }
  161. //Fount it!
  162. libusb_free_device_list(devs,1);
  163. return dh;
  164. }
  165. libusb_free_device_list(devs,1);
  166. return NULL;
  167. }
  168. pcap_t *
  169. canusb_create(const char *device, char *ebuf, int *is_ours)
  170. {
  171. const char *cp;
  172. char *cpend;
  173. long devnum;
  174. pcap_t* p;
  175. struct pcap_canusb *canusb;
  176. /* Does this look like a DAG device? */
  177. cp = strrchr(device, '/');
  178. if (cp == NULL)
  179. cp = device;
  180. /* Does it begin with "canusb"? */
  181. if (strncmp(cp, "canusb", 6) != 0) {
  182. /* Nope, doesn't begin with "canusb" */
  183. *is_ours = 0;
  184. return NULL;
  185. }
  186. /* Yes - is "canusb" followed by a number? */
  187. cp += 6;
  188. devnum = strtol(cp, &cpend, 10);
  189. if (cpend == cp || *cpend != '\0') {
  190. /* Not followed by a number. */
  191. *is_ours = 0;
  192. return NULL;
  193. }
  194. if (devnum < 0) {
  195. /* Followed by a non-valid number. */
  196. *is_ours = 0;
  197. return NULL;
  198. }
  199. /* OK, it's probably ours. */
  200. *is_ours = 1;
  201. p = pcap_create_common(device, ebuf, sizeof (struct pcap_canusb));
  202. if (p == NULL)
  203. return (NULL);
  204. canusb = p->priv;
  205. canusb->ctx = NULL;
  206. canusb->dev = NULL;
  207. canusb->rdpipe = -1;
  208. canusb->wrpipe = -1;
  209. p->activate_op = canusb_activate;
  210. return (p);
  211. }
  212. static void* canusb_capture_thread(void *arg)
  213. {
  214. struct pcap_canusb *canusb = arg;
  215. int i;
  216. struct
  217. {
  218. uint8_t rxsz, txsz;
  219. } status;
  220. fcntl(canusb->wrpipe, F_SETFL, O_NONBLOCK);
  221. while(canusb->loop)
  222. {
  223. int sz;
  224. struct CAN_Msg msg;
  225. libusb_interrupt_transfer(canusb->dev, 0x81, (unsigned char*)&status, sizeof(status), &sz, 100);
  226. //HACK!!!!! -> drop buffered data, read new one by reading twice.
  227. libusb_interrupt_transfer(canusb->dev, 0x81, (unsigned char*)&status, sizeof(status), &sz, 100);
  228. for(i = 0; i<status.rxsz; i++)
  229. {
  230. libusb_bulk_transfer(canusb->dev, 0x85, (unsigned char*)&msg, sizeof(msg), &sz, 100);
  231. if(write(canusb->wrpipe, &msg, sizeof(msg)) < 0)
  232. fprintf(stderr,"write() error: %s\n", strerror(errno));
  233. }
  234. }
  235. return NULL;
  236. }
  237. static int canusb_startcapture(struct pcap_canusb* this)
  238. {
  239. int pipefd[2];
  240. if (pipe(pipefd) == -1)
  241. return -1;
  242. this->rdpipe = pipefd[0];
  243. this->wrpipe = pipefd[1];
  244. this->loop = 1;
  245. pthread_create(&this->worker, NULL, canusb_capture_thread, this);
  246. return this->rdpipe;
  247. }
  248. static void canusb_clearbufs(struct pcap_canusb* this)
  249. {
  250. unsigned char cmd[16];
  251. int al;
  252. cmd[0] = 1; //Empty incoming buffer
  253. cmd[1] = 1; //Empty outgoing buffer
  254. cmd[3] = 0; //Not a write to serial number
  255. memset(&cmd[4],0,16-4);
  256. libusb_interrupt_transfer(this->dev, 0x1,cmd,16,&al,100);
  257. }
  258. static void canusb_close(pcap_t* handle)
  259. {
  260. struct pcap_canusb *canusb = handle->priv;
  261. canusb->loop = 0;
  262. pthread_join(canusb->worker, NULL);
  263. if (canusb->dev)
  264. {
  265. libusb_close(canusb->dev);
  266. canusb->dev = NULL;
  267. }
  268. if (canusb->ctx)
  269. {
  270. libusb_exit(canusb->ctx);
  271. canusb->ctx = NULL;
  272. }
  273. }
  274. static int canusb_activate(pcap_t* handle)
  275. {
  276. struct pcap_canusb *canusb = handle->priv;
  277. char *serial;
  278. if (libusb_init(&canusb->ctx) != 0) {
  279. /*
  280. * XXX - what causes this to fail?
  281. */
  282. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "libusb_init() failed");
  283. return PCAP_ERROR;
  284. }
  285. handle->read_op = canusb_read_linux;
  286. handle->inject_op = canusb_inject_linux;
  287. handle->setfilter_op = canusb_setfilter_linux;
  288. handle->setdirection_op = canusb_setdirection_linux;
  289. handle->getnonblock_op = pcap_getnonblock_fd;
  290. handle->setnonblock_op = pcap_setnonblock_fd;
  291. handle->stats_op = canusb_stats_linux;
  292. handle->cleanup_op = canusb_close;
  293. /* Initialize some components of the pcap structure. */
  294. handle->bufsize = 32;
  295. handle->offset = 8;
  296. handle->linktype = DLT_CAN_SOCKETCAN;
  297. handle->set_datalink_op = NULL;
  298. serial = handle->opt.source + strlen(CANUSB_IFACE);
  299. canusb->dev = canusb_opendevice(canusb->ctx, serial);
  300. if (!canusb->dev)
  301. {
  302. libusb_exit(canusb->ctx);
  303. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't open USB Device");
  304. return PCAP_ERROR;
  305. }
  306. canusb_clearbufs(canusb);
  307. handle->fd = canusb_startcapture(canusb);
  308. handle->selectable_fd = handle->fd;
  309. return 0;
  310. }
  311. static int
  312. canusb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
  313. {
  314. static struct timeval firstpacket = { -1, -1};
  315. int i = 0;
  316. struct CAN_Msg msg;
  317. struct pcap_pkthdr pkth;
  318. while(i < max_packets)
  319. {
  320. int n;
  321. usleep(10 * 1000);
  322. n = read(handle->fd, &msg, sizeof(msg));
  323. if (n <= 0)
  324. break;
  325. pkth.caplen = pkth.len = n;
  326. pkth.caplen -= 4;
  327. pkth.caplen -= 8 - msg.length;
  328. if ((firstpacket.tv_sec == -1) && (firstpacket.tv_usec == -1))
  329. gettimeofday(&firstpacket, NULL);
  330. pkth.ts.tv_usec = firstpacket.tv_usec + (msg.timestamp % 100) * 10000;
  331. pkth.ts.tv_sec = firstpacket.tv_usec + (msg.timestamp / 100);
  332. if (pkth.ts.tv_usec > 1000000)
  333. {
  334. pkth.ts.tv_usec -= 1000000;
  335. pkth.ts.tv_sec++;
  336. }
  337. callback(user, &pkth, (void*)&msg.id);
  338. i++;
  339. }
  340. return i;
  341. }
  342. static int
  343. canusb_inject_linux(pcap_t *handle, const void *buf, size_t size)
  344. {
  345. /* not yet implemented */
  346. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on canusb devices");
  347. return (-1);
  348. }
  349. static int
  350. canusb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
  351. {
  352. /* not yet implemented */
  353. stats->ps_recv = 0; /* number of packets received */
  354. stats->ps_drop = 0; /* number of packets dropped */
  355. stats->ps_ifdrop = 0; /* drops by interface -- only supported on some platforms */
  356. return 0;
  357. }
  358. static int
  359. canusb_setfilter_linux(pcap_t *p, struct bpf_program *fp)
  360. {
  361. /* not yet implemented */
  362. return 0;
  363. }
  364. static int
  365. canusb_setdirection_linux(pcap_t *p, pcap_direction_t d)
  366. {
  367. /* no support for PCAP_D_OUT */
  368. if (d == PCAP_D_OUT)
  369. {
  370. snprintf(p->errbuf, sizeof(p->errbuf),
  371. "Setting direction to PCAP_D_OUT is not supported on this interface");
  372. return -1;
  373. }
  374. p->direction = d;
  375. return 0;
  376. }
  377. /* eof */