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