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