1 /* $NetBSD: pcap-enet.c,v 1.2 2014/11/19 19:33:30 christos Exp $ */
4 * Stanford Enetfilter subroutines for tcpdump
6 * Based on the MERIT NNstat etherifrt.c and the Ultrix pcap-pf.c
9 * Rayan Zachariassen, CA*Net
12 #include <sys/cdefs.h>
13 __RCSID("$NetBSD: pcap-enet.c,v 1.2 2014/11/19 19:33:30 christos Exp $");
19 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netinet/if_ether.h>
35 #include "interface.h"
37 struct packet_header
{
39 struct LengthWords length
;
40 struct tap_header tap
;
47 #define BUFSPACE (4*1024)
50 static void efReadError(int, char *);
53 readloop(int cnt
, int if_fd
, struct bpf_program
*fp
, printfunc printit
)
56 register struct packet_header
*ph
;
60 static struct timeval tv
= { 0 };
62 register int cc
, caplen
;
63 register struct bpf_insn
*fcode
= fp
->bf_insns
;
65 struct packet_header hdr
;
71 if ((cc
= read(if_fd
, (char *)buf
.p
, sizeof(buf
))) < 0)
72 efReadError(if_fd
, "reader");
76 * Loop through each packet.
80 ph
= (struct packet_header
*)bp
;
81 caplen
= ph
->tap
.th_wirelen
> snaplen
? snaplen
: ph
->tap
83 if (bpf_filter(fcode
, (char *)ph
->packet
,
84 ph
->tap
.th_wirelen
, caplen
)) {
85 if (cnt
>= 0 && --cnt
< 0)
87 (*printit
)((char *)ph
->packet
,
88 (struct timeval
*)ph
->tap
.th_timestamp
,
89 ph
->tap
.th_wirelen
, caplen
);
91 inc
= ph
->length
.PacketOffset
;
96 caplen
= cc
> snaplen
? snaplen
: cc
;
97 if (bpf_filter(fcode
, buf
.hdr
.packet
, cc
, caplen
)) {
98 if (cnt
>= 0 && --cnt
< 0)
100 (*printit
)(buf
.hdr
.packet
, &tv
, cc
, caplen
);
108 /* Call ONLY if read() has returned an error on packet filter */
110 efReadError(int fid
, char *msg
)
112 if (errno
== EINVAL
) { /* read MAXINT bytes already! */
113 if (lseek(fid
, 0, 0) < 0) {
114 perror("tcpdump: efReadError/lseek");
121 (void) fprintf(stderr
, "tcpdump: ");
133 if (ioctl(fd
, EIOSTATS
, &es
) == -1) {
134 perror("tcpdump: enet ioctl EIOSTATS error");
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",
153 initdevice(char *device
, int pflag
, int *linktype
)
156 struct enfilter filter
;
161 GETENETDEVICE(0, O_RDONLY
, &if_fd
);
163 if_fd
= open("/dev/enet", O_RDONLY
, 0);
167 perror("tcpdump: enet open error");
169 "your system may not be properly configured; see \"man enet(4)\"");
173 /* Get operating parameters. */
175 if (ioctl(if_fd
, EIOCGETP
, (char *)&ctl
) == -1) {
176 perror("tcpdump: enet ioctl EIOCGETP error");
180 /* Set operating parameters. */
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
;
189 ctl
.en_rtout
= 64; /* randomly picked value for HZ */
191 if (ioctl(if_fd
, EIOCSETP
, &ctl
) == -1) {
192 perror("tcpdump: enet ioctl EIOCSETP error");
196 /* Flush the receive queue, since we've changed
197 the operating parameters and we otherwise might
198 receive data without headers. */
200 if (ioctl(if_fd
, EIOCFLUSH
) == -1) {
201 perror("tcpdump: enet ioctl EIOCFLUSH error");
205 /* Set the receive queue depth to its maximum. */
207 maxwaiting
= ctl
.en_maxwaiting
;
208 if (ioctl(if_fd
, EIOCSETW
, &maxwaiting
) == -1) {
209 perror("tcpdump: enet ioctl EIOCSETW error");
214 /* Clear statistics. */
216 if (ioctl(if_fd
, EIOCLRSTAT
, 0) == -1) {
217 perror("tcpdump: enet ioctl EIOCLRSTAT error");
222 /* Set the filter (accept all packets). */
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");
231 * "enetfilter" supports only ethernets.
233 *linktype
= DLT_EN10MB
;