1 /*
2 * Stanford Enetfilter subroutines for tcpdump
3 *
4 * Based on the MERIT NNstat etherifrt.c and the Ultrix pcap-pf.c
5 * subroutines.
6 *
7 * Rayan Zachariassen, CA*Net
8 */
9
10
11 #include <stdio.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/file.h>
16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
18
19 #include <net/if.h>
20 #include <netinet/in.h>
21 #include <netinet/if_ether.h>
22 #include <net/bpf.h>
23 #include <net/enet.h>
24
25 #include "interface.h"
26
27 struct packet_header {
28 #ifdef IBMRTPC
29 struct LengthWords length;
30 struct tap_header tap;
31 #endif /* IBMRTPC */
32 u_char packet[8]
33 };
34
35 extern int errno;
36
37 #define BUFSPACE (4*1024)
38
39 void
readloop(cnt,if_fd,fp,printit)40 readloop(cnt, if_fd, fp, printit)
41 int cnt;
42 int if_fd;
43 struct bpf_program *fp;
44 void (*printit)();
45 {
46 #ifdef IBMRTPC
47 register struct packet_header *ph;
48 register u_char *bp;
49 register int inc;
50 #else /* !IBMRTPC */
51 static struct timeval tv = { 0 };
52 #endif /* IBMRTPC */
53 register int cc, caplen;
54 register struct bpf_insn *fcode = fp->bf_insns;
55 union {
56 struct packet_header hdr;
57 u_char p[BUFSPACE];
58 u_short s;
59 } buf;
60 extern void efReadError();
61
62
63 while (1) {
64 if ((cc = read(if_fd, (char *)buf.p, sizeof(buf))) < 0)
65 efReadError(if_fd, "reader");
66
67 #ifdef IBMRTPC
68 /*
69 * Loop through each packet.
70 */
71 bp = buf.p;
72 while (cc > 0) {
73 ph = (struct packet_header *)bp;
74 caplen = ph->tap.th_wirelen > snaplen ? snaplen : ph->tap
75 .th_wirelen ;
76 if (bpf_filter(fcode, (char *)ph->packet,
77 ph->tap.th_wirelen, caplen)) {
78 (*printit)((char *)ph->packet,
79 (struct timeval *)ph->tap.th_timestamp,
80 ph->tap.th_wirelen, caplen);
81 if (cflag && --cnt == 0)
82 goto out;
83 }
84 inc = ph->length.PacketOffset;
85 cc -= inc;
86 bp += inc;
87 }
88 #else /* !IBMRTPC */
89 caplen = cc > snaplen ? snaplen : cc ;
90 if (bpf_filter(fcode, buf.hdr.packet, cc, caplen)) {
91 (*printit)(buf.hdr.packet, &tv, cc, caplen);
92 if (cflag && --cnt == 0)
93 goto out;
94 }
95 #endif /* IBMRTPC */
96 }
97 out:
98 wrapup(if_fd);
99 }
100
101 /* Call ONLY if read() has returned an error on packet filter */
102 void
efReadError(fid,msg)103 efReadError(fid, msg)
104 int fid;
105 char *msg;
106 {
107 if (errno == EINVAL) { /* read MAXINT bytes already! */
108 if (lseek(fid, 0, 0) < 0) {
109 perror("efReadError/lseek");
110 exit(-1);
111 }
112 else
113 return;
114 }
115 else {
116 perror(msg);
117 exit(-1);
118 }
119 }
120
wrapup(fd)121 wrapup(fd)
122 int fd;
123 {
124 #ifdef IBMRTPC
125 struct enstats es;
126
127 if (ioctl(fd, EIOSTATS, &es) == -1) {
128 perror("enet ioctl EIOSTATS error");
129 exit(-1);
130 }
131
132 fprintf(stderr, "%d packets queued", es.enStat_Rcnt);
133 if (es.enStat_Rdrops > 0)
134 fprintf(stderr, ", %d dropped", es.enStat_Rdrops);
135 if (es.enStat_Reads > 0)
136 fprintf(stderr, ", %d tcpdump %s", es.enStat_Reads,
137 es.enStat_Reads > 1 ? "reads" : "read");
138 if (es.enStat_MaxRead > 1)
139 fprintf(stderr, ", %d packets in largest read",
140 es.enStat_MaxRead);
141 putc('\n', stderr);
142 #endif /* IBMRTPC */
143 close(fd);
144 }
145
146 int
initdevice(device,pflag,linktype)147 initdevice(device, pflag, linktype)
148 char *device;
149 int pflag;
150 int *linktype;
151 {
152 struct eniocb ctl;
153 struct enfilter filter;
154 unsigned int maxwaiting;
155 int if_fd;
156
157 #ifdef IBMRTPC
158 GETENETDEVICE(0, O_RDONLY, &if_fd);
159 #else /* !IBMRTPC */
160 if_fd = open("/dev/enet", O_RDONLY, 0);
161 #endif /* IBMRTPC */
162
163 if (if_fd == -1) {
164 perror("enet open error");
165 error(
166 "your system may not be properly configured; see \"man enet(4)\"");
167 exit(-1);
168 }
169
170 /* Get operating parameters. */
171
172 if (ioctl(if_fd, EIOCGETP, (char *)&ctl) == -1) {
173 perror("enet ioctl EIOCGETP error");
174 exit(-1);
175 }
176
177 /* Set operating parameters. */
178
179 #ifdef IBMRTPC
180 ctl.en_rtout = 1 * ctl.en_hz;
181 ctl.en_tr_etherhead = 1;
182 ctl.en_tap_network = 1;
183 ctl.en_multi_packet = 1;
184 ctl.en_maxlen = BUFSPACE;
185 #else /* !IBMRTPC */
186 ctl.en_rtout = 64; /* randomly picked value for HZ */
187 #endif /* IBMRTPC */
188 if (ioctl(if_fd, EIOCSETP, &ctl) == -1) {
189 perror("enet ioctl EIOCSETP error");
190 exit(-1);
191 }
192
193 /* Flush the receive queue, since we've changed
194 the operating parameters and we otherwise might
195 receive data without headers. */
196
197 if (ioctl(if_fd, EIOCFLUSH) == -1) {
198 perror("enet ioctl EIOCFLUSH error");
199 exit(-1);
200 }
201
202 /* Set the receive queue depth to its maximum. */
203
204 maxwaiting = ctl.en_maxwaiting;
205 if (ioctl(if_fd, EIOCSETW, &maxwaiting) == -1) {
206 perror("enet ioctl EIOCSETW error");
207 exit(-1);
208 }
209
210 #ifdef IBMRTPC
211 /* Clear statistics. */
212
213 if (ioctl(if_fd, EIOCLRSTAT, 0) == -1) {
214 perror("enet ioctl EIOCLRSTAT error");
215 exit(-1);
216 }
217 #endif /* IBMRTPC */
218
219 /* Set the filter (accept all packets). */
220
221 filter.enf_Priority = 3;
222 filter.enf_FilterLen = 0;
223 if (ioctl(if_fd, EIOCSETF, &filter) == -1) {
224 perror("enet ioctl EIOCSETF error");
225 exit(-1);
226 }
227 /*
228 * "enetfilter" supports only ethernets.
229 */
230 *linktype = DLT_EN10MB;
231
232 return(if_fd);
233 }
234