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