2 * Stanford Enetfilter subroutines for tcpdump
4 * Based on the MERIT NNstat etherifrt.c and the Ultrix pcap-pf.c
7 * Rayan Zachariassen, CA*Net
10 static const char rcsid[] _U_ =
11 "@(#) $Header: /tcpdump/master/libpcap/pcap-enet.c,v 1.7.2.1 2003/11/15 23:26:44 guy Exp $";
18 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netinet/if_ether.h>
34 #include "interface.h"
36 struct packet_header {
38 struct LengthWords length;
39 struct tap_header tap;
46 #define BUFSPACE (4*1024)
49 static void efReadError(int, char *);
52 readloop(int cnt, int if_fd, struct bpf_program *fp, printfunc printit)
55 register struct packet_header *ph;
59 static struct timeval tv = { 0 };
61 register int cc, caplen;
62 register struct bpf_insn *fcode = fp->bf_insns;
64 struct packet_header hdr;
70 if ((cc = read(if_fd, (char *)buf.p, sizeof(buf))) < 0)
71 efReadError(if_fd, "reader");
75 * Loop through each packet.
79 ph = (struct packet_header *)bp;
80 caplen = ph->tap.th_wirelen > snaplen ? snaplen : ph->tap
82 if (bpf_filter(fcode, (char *)ph->packet,
83 ph->tap.th_wirelen, caplen)) {
84 if (cnt >= 0 && --cnt < 0)
86 (*printit)((char *)ph->packet,
87 (struct timeval *)ph->tap.th_timestamp,
88 ph->tap.th_wirelen, caplen);
90 inc = ph->length.PacketOffset;
95 caplen = cc > snaplen ? snaplen : cc ;
96 if (bpf_filter(fcode, buf.hdr.packet, cc, caplen)) {
97 if (cnt >= 0 && --cnt < 0)
99 (*printit)(buf.hdr.packet, &tv, cc, caplen);
107 /* Call ONLY if read() has returned an error on packet filter */
109 efReadError(int fid, char *msg)
111 if (errno == EINVAL) { /* read MAXINT bytes already! */
112 if (lseek(fid, 0, 0) < 0) {
113 perror("tcpdump: efReadError/lseek");
120 (void) fprintf(stderr, "tcpdump: ");
132 if (ioctl(fd, EIOSTATS, &es) == -1) {
133 perror("tcpdump: enet ioctl EIOSTATS error");
137 fprintf(stderr, "%d packets queued", es.enStat_Rcnt);
138 if (es.enStat_Rdrops > 0)
139 fprintf(stderr, ", %d dropped", es.enStat_Rdrops);
140 if (es.enStat_Reads > 0)
141 fprintf(stderr, ", %d tcpdump %s", es.enStat_Reads,
142 es.enStat_Reads > 1 ? "reads" : "read");
143 if (es.enStat_MaxRead > 1)
144 fprintf(stderr, ", %d packets in largest read",
152 initdevice(char *device, int pflag, int *linktype)
155 struct enfilter filter;
160 GETENETDEVICE(0, O_RDONLY, &if_fd);
162 if_fd = open("/dev/enet", O_RDONLY, 0);
166 perror("tcpdump: enet open error");
168 "your system may not be properly configured; see \"man enet(4)\"");
172 /* Get operating parameters. */
174 if (ioctl(if_fd, EIOCGETP, (char *)&ctl) == -1) {
175 perror("tcpdump: enet ioctl EIOCGETP error");
179 /* Set operating parameters. */
182 ctl.en_rtout = 1 * ctl.en_hz;
183 ctl.en_tr_etherhead = 1;
184 ctl.en_tap_network = 1;
185 ctl.en_multi_packet = 1;
186 ctl.en_maxlen = BUFSPACE;
188 ctl.en_rtout = 64; /* randomly picked value for HZ */
190 if (ioctl(if_fd, EIOCSETP, &ctl) == -1) {
191 perror("tcpdump: enet ioctl EIOCSETP error");
195 /* Flush the receive queue, since we've changed
196 the operating parameters and we otherwise might
197 receive data without headers. */
199 if (ioctl(if_fd, EIOCFLUSH) == -1) {
200 perror("tcpdump: enet ioctl EIOCFLUSH error");
204 /* Set the receive queue depth to its maximum. */
206 maxwaiting = ctl.en_maxwaiting;
207 if (ioctl(if_fd, EIOCSETW, &maxwaiting) == -1) {
208 perror("tcpdump: enet ioctl EIOCSETW error");
213 /* Clear statistics. */
215 if (ioctl(if_fd, EIOCLRSTAT, 0) == -1) {
216 perror("tcpdump: enet ioctl EIOCLRSTAT error");
221 /* Set the filter (accept all packets). */
223 filter.enf_Priority = 3;
224 filter.enf_FilterLen = 0;
225 if (ioctl(if_fd, EIOCSETF, &filter) == -1) {
226 perror("tcpdump: enet ioctl EIOCSETF error");
230 * "enetfilter" supports only ethernets.
232 *linktype = DLT_EN10MB;