2 * Stanford Enetfilter subroutines for tcpdump
4 * Based on the MERIT NNstat etherifrt.c and the Ultrix pcap-pf.c
7 * Rayan Zachariassen, CA*Net
10 #include <sys/types.h>
13 #include <sys/ioctl.h>
14 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netinet/if_ether.h>
26 #include "interface.h"
28 struct packet_header
{
30 struct LengthWords length
;
31 struct tap_header tap
;
38 #define BUFSPACE (4*1024)
41 static void efReadError(int, char *);
44 readloop(int cnt
, int if_fd
, struct bpf_program
*fp
, printfunc printit
)
47 register struct packet_header
*ph
;
51 static struct timeval tv
= { 0 };
53 register int cc
, caplen
;
54 register struct bpf_insn
*fcode
= fp
->bf_insns
;
56 struct packet_header hdr
;
62 if ((cc
= read(if_fd
, (char *)buf
.p
, sizeof(buf
))) < 0)
63 efReadError(if_fd
, "reader");
67 * Loop through each packet.
71 ph
= (struct packet_header
*)bp
;
72 caplen
= ph
->tap
.th_wirelen
> snaplen
? snaplen
: ph
->tap
74 if (bpf_filter(fcode
, (char *)ph
->packet
,
75 ph
->tap
.th_wirelen
, caplen
)) {
76 if (cnt
>= 0 && --cnt
< 0)
78 (*printit
)((char *)ph
->packet
,
79 (struct timeval
*)ph
->tap
.th_timestamp
,
80 ph
->tap
.th_wirelen
, caplen
);
82 inc
= ph
->length
.PacketOffset
;
87 caplen
= cc
> snaplen
? snaplen
: cc
;
88 if (bpf_filter(fcode
, buf
.hdr
.packet
, cc
, caplen
)) {
89 if (cnt
>= 0 && --cnt
< 0)
91 (*printit
)(buf
.hdr
.packet
, &tv
, cc
, caplen
);
99 /* Call ONLY if read() has returned an error on packet filter */
101 efReadError(int fid
, char *msg
)
103 if (errno
== EINVAL
) { /* read MAXINT bytes already! */
104 if (lseek(fid
, 0, 0) < 0) {
105 perror("tcpdump: efReadError/lseek");
112 (void) fprintf(stderr
, "tcpdump: ");
124 if (ioctl(fd
, EIOSTATS
, &es
) == -1) {
125 perror("tcpdump: enet ioctl EIOSTATS error");
129 fprintf(stderr
, "%d packets queued", es
.enStat_Rcnt
);
130 if (es
.enStat_Rdrops
> 0)
131 fprintf(stderr
, ", %d dropped", es
.enStat_Rdrops
);
132 if (es
.enStat_Reads
> 0)
133 fprintf(stderr
, ", %d tcpdump %s", es
.enStat_Reads
,
134 es
.enStat_Reads
> 1 ? "reads" : "read");
135 if (es
.enStat_MaxRead
> 1)
136 fprintf(stderr
, ", %d packets in largest read",
144 initdevice(char *device
, int pflag
, int *linktype
)
147 struct enfilter filter
;
152 GETENETDEVICE(0, O_RDONLY
, &if_fd
);
154 if_fd
= open("/dev/enet", O_RDONLY
, 0);
158 perror("tcpdump: enet open error");
160 "your system may not be properly configured; see \"man enet(4)\"");
164 /* Get operating parameters. */
166 if (ioctl(if_fd
, EIOCGETP
, (char *)&ctl
) == -1) {
167 perror("tcpdump: enet ioctl EIOCGETP error");
171 /* Set operating parameters. */
174 ctl
.en_rtout
= 1 * ctl
.en_hz
;
175 ctl
.en_tr_etherhead
= 1;
176 ctl
.en_tap_network
= 1;
177 ctl
.en_multi_packet
= 1;
178 ctl
.en_maxlen
= BUFSPACE
;
180 ctl
.en_rtout
= 64; /* randomly picked value for HZ */
182 if (ioctl(if_fd
, EIOCSETP
, &ctl
) == -1) {
183 perror("tcpdump: enet ioctl EIOCSETP error");
187 /* Flush the receive queue, since we've changed
188 the operating parameters and we otherwise might
189 receive data without headers. */
191 if (ioctl(if_fd
, EIOCFLUSH
) == -1) {
192 perror("tcpdump: enet ioctl EIOCFLUSH error");
196 /* Set the receive queue depth to its maximum. */
198 maxwaiting
= ctl
.en_maxwaiting
;
199 if (ioctl(if_fd
, EIOCSETW
, &maxwaiting
) == -1) {
200 perror("tcpdump: enet ioctl EIOCSETW error");
205 /* Clear statistics. */
207 if (ioctl(if_fd
, EIOCLRSTAT
, 0) == -1) {
208 perror("tcpdump: enet ioctl EIOCLRSTAT error");
213 /* Set the filter (accept all packets). */
215 filter
.enf_Priority
= 3;
216 filter
.enf_FilterLen
= 0;
217 if (ioctl(if_fd
, EIOCSETF
, &filter
) == -1) {
218 perror("tcpdump: enet ioctl EIOCSETF error");
222 * "enetfilter" supports only ethernets.
224 *linktype
= DLT_EN10MB
;
This page took 0.05169 seconds and 5 git commands to generate.