202-protocol_api.patch 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. --- a/pcap-int.h
  2. +++ b/pcap-int.h
  3. @@ -209,6 +209,7 @@ struct pcap_opt {
  4. char *source;
  5. int promisc;
  6. int rfmon;
  7. + int proto; /* protocol for packet socket (linux) */
  8. };
  9. /*
  10. --- a/pcap-linux.c
  11. +++ b/pcap-linux.c
  12. @@ -335,7 +335,7 @@ static int iface_get_id(int fd, const ch
  13. static int iface_get_mtu(int fd, const char *device, char *ebuf);
  14. static int iface_get_arptype(int fd, const char *device, char *ebuf);
  15. #ifdef HAVE_PF_PACKET_SOCKETS
  16. -static int iface_bind(int fd, int ifindex, char *ebuf);
  17. +static int iface_bind(int fd, int ifindex, char *ebuf, unsigned short proto);
  18. #ifdef IW_MODE_MONITOR
  19. static int has_wext(int sock_fd, const char *device, char *ebuf);
  20. #endif /* IW_MODE_MONITOR */
  21. @@ -881,7 +881,7 @@ pcap_can_set_rfmon_linux(pcap_t *handle)
  22. * (We assume that if we have Wireless Extensions support
  23. * we also have PF_PACKET support.)
  24. */
  25. - sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  26. + sock_fd = socket(PF_PACKET, SOCK_RAW, p->opt.proto);
  27. if (sock_fd == -1) {
  28. (void)snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  29. "socket: %s", pcap_strerror(errno));
  30. @@ -1128,6 +1128,9 @@ pcap_activate_linux(pcap_t *handle)
  31. handle->read_op = pcap_read_linux;
  32. handle->stats_op = pcap_stats_linux;
  33. + if (handle->opt.proto < 0)
  34. + handle->opt.proto = (int) htons(ETH_P_ALL);
  35. +
  36. /*
  37. * The "any" device is a special device which causes us not
  38. * to bind to a particular device and thus to look at all
  39. @@ -2684,8 +2687,8 @@ activate_new(pcap_t *handle)
  40. * try a SOCK_RAW socket for the raw interface.
  41. */
  42. sock_fd = is_any_device ?
  43. - socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) :
  44. - socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  45. + socket(PF_PACKET, SOCK_DGRAM, handle->opt.proto) :
  46. + socket(PF_PACKET, SOCK_RAW, handle->opt.proto);
  47. if (sock_fd == -1) {
  48. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
  49. @@ -2783,7 +2786,7 @@ activate_new(pcap_t *handle)
  50. return PCAP_ERROR;
  51. }
  52. sock_fd = socket(PF_PACKET, SOCK_DGRAM,
  53. - htons(ETH_P_ALL));
  54. + handle->opt.proto);
  55. if (sock_fd == -1) {
  56. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  57. "socket: %s", pcap_strerror(errno));
  58. @@ -2835,7 +2838,7 @@ activate_new(pcap_t *handle)
  59. }
  60. if ((err = iface_bind(sock_fd, handle->md.ifindex,
  61. - handle->errbuf)) != 1) {
  62. + handle->errbuf, handle->opt.proto)) != 1) {
  63. close(sock_fd);
  64. if (err < 0)
  65. return err;
  66. @@ -3640,7 +3643,7 @@ iface_get_id(int fd, const char *device,
  67. * or a PCAP_ERROR_ value on a hard error.
  68. */
  69. static int
  70. -iface_bind(int fd, int ifindex, char *ebuf)
  71. +iface_bind(int fd, int ifindex, char *ebuf, unsigned short proto)
  72. {
  73. struct sockaddr_ll sll;
  74. int err;
  75. @@ -3649,7 +3652,7 @@ iface_bind(int fd, int ifindex, char *eb
  76. memset(&sll, 0, sizeof(sll));
  77. sll.sll_family = AF_PACKET;
  78. sll.sll_ifindex = ifindex;
  79. - sll.sll_protocol = htons(ETH_P_ALL);
  80. + sll.sll_protocol = proto;
  81. if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
  82. if (errno == ENETDOWN) {
  83. @@ -4359,7 +4362,7 @@ activate_old(pcap_t *handle)
  84. /* Open the socket */
  85. - handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
  86. + handle->fd = socket(PF_INET, SOCK_PACKET, handle->opt.proto);
  87. if (handle->fd == -1) {
  88. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  89. "socket: %s", pcap_strerror(errno));
  90. --- a/pcap.c
  91. +++ b/pcap.c
  92. @@ -258,6 +258,8 @@ pcap_create_common(const char *source, c
  93. pcap_set_snaplen(p, 65535); /* max packet size */
  94. p->opt.promisc = 0;
  95. p->opt.buffer_size = 0;
  96. + p->opt.proto = -1;
  97. +
  98. return (p);
  99. }
  100. @@ -317,6 +319,15 @@ pcap_set_buffer_size(pcap_t *p, int buff
  101. return 0;
  102. }
  103. +int
  104. +pcap_set_protocol(pcap_t *p, unsigned short proto)
  105. +{
  106. + if (pcap_check_activated(p))
  107. + return PCAP_ERROR_ACTIVATED;
  108. + p->opt.proto = proto;
  109. + return 0;
  110. +}
  111. +
  112. int
  113. pcap_activate(pcap_t *p)
  114. {
  115. --- a/pcap/pcap.h
  116. +++ b/pcap/pcap.h
  117. @@ -68,6 +68,7 @@ extern "C" {
  118. #define PCAP_VERSION_MINOR 4
  119. #define PCAP_ERRBUF_SIZE 256
  120. +#define HAS_PROTO_EXTENSION
  121. /*
  122. * Compatibility for systems that have a bpf.h that
  123. @@ -276,6 +277,7 @@ int pcap_can_set_rfmon(pcap_t *);
  124. int pcap_set_rfmon(pcap_t *, int);
  125. int pcap_set_timeout(pcap_t *, int);
  126. int pcap_set_buffer_size(pcap_t *, int);
  127. +int pcap_set_protocol(pcap_t *, unsigned short);
  128. int pcap_activate(pcap_t *);
  129. pcap_t *pcap_open_live(const char *, int, int, int, char *);