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