pcap_user.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL.
  4. */
  5. #include <errno.h>
  6. #include <pcap.h>
  7. #include <string.h>
  8. #include <asm/types.h>
  9. #include <net_user.h>
  10. #include "pcap_user.h"
  11. #include <um_malloc.h>
  12. #define PCAP_FD(p) (*(int *)(p))
  13. static int pcap_user_init(void *data, void *dev)
  14. {
  15. struct pcap_data *pri = data;
  16. pcap_t *p;
  17. char errors[PCAP_ERRBUF_SIZE];
  18. p = pcap_open_live(pri->host_if, ETH_MAX_PACKET + ETH_HEADER_OTHER,
  19. pri->promisc, 0, errors);
  20. if (p == NULL) {
  21. printk(UM_KERN_ERR "pcap_user_init : pcap_open_live failed - "
  22. "'%s'\n", errors);
  23. return -EINVAL;
  24. }
  25. pri->dev = dev;
  26. pri->pcap = p;
  27. return 0;
  28. }
  29. static int pcap_open(void *data)
  30. {
  31. struct pcap_data *pri = data;
  32. __u32 netmask;
  33. int err;
  34. if (pri->pcap == NULL)
  35. return -ENODEV;
  36. if (pri->filter != NULL) {
  37. err = dev_netmask(pri->dev, &netmask);
  38. if (err < 0) {
  39. printk(UM_KERN_ERR "pcap_open : dev_netmask failed\n");
  40. return -EIO;
  41. }
  42. pri->compiled = uml_kmalloc(sizeof(struct bpf_program),
  43. UM_GFP_KERNEL);
  44. if (pri->compiled == NULL) {
  45. printk(UM_KERN_ERR "pcap_open : kmalloc failed\n");
  46. return -ENOMEM;
  47. }
  48. err = pcap_compile(pri->pcap,
  49. (struct bpf_program *) pri->compiled,
  50. pri->filter, pri->optimize, netmask);
  51. if (err < 0) {
  52. printk(UM_KERN_ERR "pcap_open : pcap_compile failed - "
  53. "'%s'\n", pcap_geterr(pri->pcap));
  54. goto out;
  55. }
  56. err = pcap_setfilter(pri->pcap, pri->compiled);
  57. if (err < 0) {
  58. printk(UM_KERN_ERR "pcap_open : pcap_setfilter "
  59. "failed - '%s'\n", pcap_geterr(pri->pcap));
  60. goto out;
  61. }
  62. }
  63. return PCAP_FD(pri->pcap);
  64. out:
  65. kfree(pri->compiled);
  66. return -EIO;
  67. }
  68. static void pcap_remove(void *data)
  69. {
  70. struct pcap_data *pri = data;
  71. if (pri->compiled != NULL)
  72. pcap_freecode(pri->compiled);
  73. if (pri->pcap != NULL)
  74. pcap_close(pri->pcap);
  75. }
  76. struct pcap_handler_data {
  77. char *buffer;
  78. int len;
  79. };
  80. static void handler(u_char *data, const struct pcap_pkthdr *header,
  81. const u_char *packet)
  82. {
  83. int len;
  84. struct pcap_handler_data *hdata = (struct pcap_handler_data *) data;
  85. len = hdata->len < header->caplen ? hdata->len : header->caplen;
  86. memcpy(hdata->buffer, packet, len);
  87. hdata->len = len;
  88. }
  89. int pcap_user_read(int fd, void *buffer, int len, struct pcap_data *pri)
  90. {
  91. struct pcap_handler_data hdata = ((struct pcap_handler_data)
  92. { .buffer = buffer,
  93. .len = len });
  94. int n;
  95. n = pcap_dispatch(pri->pcap, 1, handler, (u_char *) &hdata);
  96. if (n < 0) {
  97. printk(UM_KERN_ERR "pcap_dispatch failed - %s\n",
  98. pcap_geterr(pri->pcap));
  99. return -EIO;
  100. }
  101. else if (n == 0)
  102. return 0;
  103. return hdata.len;
  104. }
  105. const struct net_user_info pcap_user_info = {
  106. .init = pcap_user_init,
  107. .open = pcap_open,
  108. .close = NULL,
  109. .remove = pcap_remove,
  110. .add_address = NULL,
  111. .delete_address = NULL,
  112. .mtu = ETH_MAX_PACKET,
  113. .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
  114. };