101-debian_fix_any_intf.patch 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. --- a/pcap-linux.c
  2. +++ b/pcap-linux.c
  3. @@ -297,6 +297,12 @@ pcap_create(const char *device, char *eb
  4. {
  5. pcap_t *handle;
  6. + /*
  7. + * A null device name is equivalent to the "any" device.
  8. + */
  9. + if (device == NULL)
  10. + device = "any";
  11. +
  12. #ifdef HAVE_DAG_API
  13. if (strstr(device, "dag")) {
  14. return dag_create(device, ebuf);
  15. @@ -338,10 +344,9 @@ pcap_can_set_rfmon_linux(pcap_t *p)
  16. struct iwreq ireq;
  17. #endif
  18. - if (p->opt.source == NULL) {
  19. + if (strcmp(p->opt.source, "any") == 0) {
  20. /*
  21. - * This is equivalent to the "any" device, and we don't
  22. - * support monitor mode on it.
  23. + * Monitor mode makes no sense on the "any" device.
  24. */
  25. return 0;
  26. }
  27. @@ -518,12 +523,11 @@ pcap_activate_linux(pcap_t *handle)
  28. handle->stats_op = pcap_stats_linux;
  29. /*
  30. - * NULL and "any" are special devices which give us the hint to
  31. - * monitor all devices.
  32. + * The "any" device is a special device which causes us not
  33. + * to bind to a particular device and thus to look at all
  34. + * devices.
  35. */
  36. - if (!device || strcmp(device, "any") == 0) {
  37. - device = NULL;
  38. - handle->md.device = strdup("any");
  39. + if (strcmp(device, "any") == 0) {
  40. if (handle->opt.promisc) {
  41. handle->opt.promisc = 0;
  42. /* Just a warning. */
  43. @@ -531,10 +535,9 @@ pcap_activate_linux(pcap_t *handle)
  44. "Promiscuous mode not supported on the \"any\" device");
  45. status = PCAP_WARNING_PROMISC_NOTSUP;
  46. }
  47. + }
  48. - } else
  49. - handle->md.device = strdup(device);
  50. -
  51. + handle->md.device = strdup(device);
  52. if (handle->md.device == NULL) {
  53. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s",
  54. pcap_strerror(errno) );
  55. @@ -1657,19 +1660,21 @@ static int
  56. activate_new(pcap_t *handle)
  57. {
  58. #ifdef HAVE_PF_PACKET_SOCKETS
  59. + const char *device = handle->opt.source;
  60. + int is_any_device = (strcmp(device, "any") == 0);
  61. int sock_fd = -1, arptype, val;
  62. int err = 0;
  63. struct packet_mreq mr;
  64. - const char* device = handle->opt.source;
  65. /*
  66. - * Open a socket with protocol family packet. If a device is
  67. - * given we try to open it in raw mode otherwise we use
  68. - * the cooked interface.
  69. - */
  70. - sock_fd = device ?
  71. - socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
  72. - : socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  73. + * Open a socket with protocol family packet. If the
  74. + * "any" device was specified, we open a SOCK_DGRAM
  75. + * socket for the cooked interface, otherwise we first
  76. + * try a SOCK_RAW socket for the raw interface.
  77. + */
  78. + sock_fd = is_any_device ?
  79. + socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) :
  80. + socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  81. if (sock_fd == -1) {
  82. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
  83. @@ -1704,7 +1709,7 @@ activate_new(pcap_t *handle)
  84. * to cooked mode if we have an unknown interface type
  85. * or a type we know doesn't work well in raw mode.
  86. */
  87. - if (device) {
  88. + if (!is_any_device) {
  89. /* Assume for now we don't need cooked mode. */
  90. handle->md.cooked = 0;
  91. @@ -1819,15 +1824,23 @@ activate_new(pcap_t *handle)
  92. }
  93. } else {
  94. /*
  95. - * This is cooked mode.
  96. + * The "any" device.
  97. + */
  98. + if (handle->opt.rfmon) {
  99. + /*
  100. + * It doesn't support monitor mode.
  101. + */
  102. + return PCAP_ERROR_RFMON_NOTSUP;
  103. + }
  104. +
  105. + /*
  106. + * It uses cooked mode.
  107. */
  108. handle->md.cooked = 1;
  109. handle->linktype = DLT_LINUX_SLL;
  110. /*
  111. * We're not bound to a device.
  112. - * XXX - true? Or true only if we're using
  113. - * the "any" device?
  114. * For now, we're using this as an indication
  115. * that we can't transmit; stop doing that only
  116. * if we figure out how to transmit in cooked
  117. @@ -1852,10 +1865,13 @@ activate_new(pcap_t *handle)
  118. /*
  119. * Hmm, how can we set promiscuous mode on all interfaces?
  120. - * I am not sure if that is possible at all.
  121. + * I am not sure if that is possible at all. For now, we
  122. + * silently ignore attempts to turn promiscuous mode on
  123. + * for the "any" device (so you don't have to explicitly
  124. + * disable it in programs such as tcpdump).
  125. */
  126. - if (device && handle->opt.promisc) {
  127. + if (!is_any_device && handle->opt.promisc) {
  128. memset(&mr, 0, sizeof(mr));
  129. mr.mr_ifindex = handle->md.ifindex;
  130. mr.mr_type = PACKET_MR_PROMISC;
  131. @@ -3118,7 +3134,7 @@ activate_old(pcap_t *handle)
  132. /* Bind to the given device */
  133. - if (!device) {
  134. + if (strcmp(device, "any") == 0) {
  135. strncpy(handle->errbuf, "pcap_activate: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
  136. PCAP_ERRBUF_SIZE);
  137. return PCAP_ERROR;