1 commit 8fa17a5a554aaeb85d3ec4118b45a31f1efd6808
3 Date: Wed Nov 19 08:20:39 2008 +0000
5 Fix the handling of the "any" device, including making it reject
6 attempts to open it in monitor mode.
8 pcap-linux.c | 68 ++++++++++++++++++++++++++++++++++++-----------------------
9 1 file changed, 42 insertions(+), 26 deletions(-)
18 + * A null device name is equivalent to the "any" device.
24 if (strstr(device, "dag")) {
25 return dag_create(device, ebuf);
30 - if (p->opt.source == NULL) {
31 + if (strcmp(p->opt.source, "any") == 0) {
33 - * This is equivalent to the "any" device, and we don't
34 - * support monitor mode on it.
35 + * Monitor mode makes no sense on the "any" device.
40 handle->stats_op = pcap_stats_linux;
43 - * NULL and "any" are special devices which give us the hint to
44 - * monitor all devices.
45 + * The "any" device is a special device which causes us not
46 + * to bind to a particular device and thus to look at all
49 - if (!device || strcmp(device, "any") == 0) {
51 - handle->md.device = strdup("any");
52 + if (strcmp(device, "any") == 0) {
53 if (handle->opt.promisc) {
54 handle->opt.promisc = 0;
57 "Promiscuous mode not supported on the \"any\" device");
58 status = PCAP_WARNING_PROMISC_NOTSUP;
63 - handle->md.device = strdup(device);
65 + handle->md.device = strdup(device);
66 if (handle->md.device == NULL) {
67 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s",
68 pcap_strerror(errno) );
69 @@ -1657,19 +1660,21 @@
70 activate_new(pcap_t *handle)
72 #ifdef HAVE_PF_PACKET_SOCKETS
73 + const char *device = handle->opt.source;
74 + int is_any_device = (strcmp(device, "any") == 0);
75 int sock_fd = -1, arptype, val;
77 struct packet_mreq mr;
78 - const char* device = handle->opt.source;
81 - * Open a socket with protocol family packet. If a device is
82 - * given we try to open it in raw mode otherwise we use
83 - * the cooked interface.
86 - socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
87 - : socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
88 + * Open a socket with protocol family packet. If the
89 + * "any" device was specified, we open a SOCK_DGRAM
90 + * socket for the cooked interface, otherwise we first
91 + * try a SOCK_RAW socket for the raw interface.
93 + sock_fd = is_any_device ?
94 + socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) :
95 + socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
98 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
100 * to cooked mode if we have an unknown interface type
101 * or a type we know doesn't work well in raw mode.
104 + if (!is_any_device) {
105 /* Assume for now we don't need cooked mode. */
106 handle->md.cooked = 0;
108 @@ -1819,15 +1824,23 @@
112 - * This is cooked mode.
113 + * The "any" device.
115 + if (handle->opt.rfmon) {
117 + * It doesn't support monitor mode.
119 + return PCAP_ERROR_RFMON_NOTSUP;
123 + * It uses cooked mode.
125 handle->md.cooked = 1;
126 handle->linktype = DLT_LINUX_SLL;
129 * We're not bound to a device.
130 - * XXX - true? Or true only if we're using
131 - * the "any" device?
132 * For now, we're using this as an indication
133 * that we can't transmit; stop doing that only
134 * if we figure out how to transmit in cooked
135 @@ -1852,10 +1865,13 @@
138 * Hmm, how can we set promiscuous mode on all interfaces?
139 - * I am not sure if that is possible at all.
140 + * I am not sure if that is possible at all. For now, we
141 + * silently ignore attempts to turn promiscuous mode on
142 + * for the "any" device (so you don't have to explicitly
143 + * disable it in programs such as tcpdump).
146 - if (device && handle->opt.promisc) {
147 + if (!is_any_device && handle->opt.promisc) {
148 memset(&mr, 0, sizeof(mr));
149 mr.mr_ifindex = handle->md.ifindex;
150 mr.mr_type = PACKET_MR_PROMISC;
151 @@ -3118,7 +3134,7 @@
153 /* Bind to the given device */
156 + if (strcmp(device, "any") == 0) {
157 strncpy(handle->errbuf, "pcap_activate: The \"any\" device isn't supported on 2.0[.x]-kernel systems",