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
14 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/if_ether.h>
30 #include "interface.h"
32 struct packet_header {
34 struct LengthWords length;
35 struct tap_header tap;
42 #define BUFSPACE (4*1024)
45 static void efReadError(int, char *);
48 readloop(int cnt, int if_fd, struct bpf_program *fp, printfunc printit)
51 register struct packet_header *ph;
55 static struct timeval tv = { 0 };
57 register int cc, caplen;
58 register struct bpf_insn *fcode = fp->bf_insns;
60 struct packet_header hdr;
66 if ((cc = read(if_fd, (char *)buf.p, sizeof(buf))) < 0)
67 efReadError(if_fd, "reader");
71 * Loop through each packet.
75 ph = (struct packet_header *)bp;
76 caplen = ph->tap.th_wirelen > snaplen ? snaplen : ph->tap
78 if (bpf_filter(fcode, (char *)ph->packet,
79 ph->tap.th_wirelen, caplen)) {
80 if (cnt >= 0 && --cnt < 0)
82 (*printit)((char *)ph->packet,
83 (struct timeval *)ph->tap.th_timestamp,
84 ph->tap.th_wirelen, caplen);
86 inc = ph->length.PacketOffset;
91 caplen = cc > snaplen ? snaplen : cc ;
92 if (bpf_filter(fcode, buf.hdr.packet, cc, caplen)) {
93 if (cnt >= 0 && --cnt < 0)
95 (*printit)(buf.hdr.packet, &tv, cc, caplen);
103 /* Call ONLY if read() has returned an error on packet filter */
105 efReadError(int fid, char *msg)
107 if (errno == EINVAL) { /* read MAXINT bytes already! */
108 if (lseek(fid, 0, 0) < 0) {
109 perror("tcpdump: efReadError/lseek");
116 (void) fprintf(stderr, "tcpdump: ");
128 if (ioctl(fd, EIOSTATS, &es) == -1) {
129 perror("tcpdump: enet ioctl EIOSTATS error");
133 fprintf(stderr, "%d packets queued", es.enStat_Rcnt);
134 if (es.enStat_Rdrops > 0)
135 fprintf(stderr, ", %d dropped", es.enStat_Rdrops);
136 if (es.enStat_Reads > 0)
137 fprintf(stderr, ", %d tcpdump %s", es.enStat_Reads,
138 es.enStat_Reads > 1 ? "reads" : "read");
139 if (es.enStat_MaxRead > 1)
140 fprintf(stderr, ", %d packets in largest read",
148 initdevice(char *device, int pflag, int *linktype)
151 struct enfilter filter;
156 GETENETDEVICE(0, O_RDONLY, &if_fd);
158 if_fd = open("/dev/enet", O_RDONLY, 0);
162 perror("tcpdump: enet open error");
164 "your system may not be properly configured; see \"man enet(4)\"");
168 /* Get operating parameters. */
170 if (ioctl(if_fd, EIOCGETP, (char *)&ctl) == -1) {
171 perror("tcpdump: enet ioctl EIOCGETP error");
175 /* Set operating parameters. */
178 ctl.en_rtout = 1 * ctl.en_hz;
179 ctl.en_tr_etherhead = 1;
180 ctl.en_tap_network = 1;
181 ctl.en_multi_packet = 1;
182 ctl.en_maxlen = BUFSPACE;
184 ctl.en_rtout = 64; /* randomly picked value for HZ */
186 if (ioctl(if_fd, EIOCSETP, &ctl) == -1) {
187 perror("tcpdump: enet ioctl EIOCSETP error");
191 /* Flush the receive queue, since we've changed
192 the operating parameters and we otherwise might
193 receive data without headers. */
195 if (ioctl(if_fd, EIOCFLUSH) == -1) {
196 perror("tcpdump: enet ioctl EIOCFLUSH error");
200 /* Set the receive queue depth to its maximum. */
202 maxwaiting = ctl.en_maxwaiting;
203 if (ioctl(if_fd, EIOCSETW, &maxwaiting) == -1) {
204 perror("tcpdump: enet ioctl EIOCSETW error");
209 /* Clear statistics. */
211 if (ioctl(if_fd, EIOCLRSTAT, 0) == -1) {
212 perror("tcpdump: enet ioctl EIOCLRSTAT error");
217 /* Set the filter (accept all packets). */
219 filter.enf_Priority = 3;
220 filter.enf_FilterLen = 0;
221 if (ioctl(if_fd, EIOCSETF, &filter) == -1) {
222 perror("tcpdump: enet ioctl EIOCSETF error");
226 * "enetfilter" supports only ethernets.
228 *linktype = DLT_EN10MB;