4 * (C)opyright 1995-1998 Darren Reed. (from tcplog)
6 * See the IPFILTER.LICENCE file for details on licencing.
17 #include <sys/types.h>
18 #include <sys/param.h>
21 #include <sys/timeb.h>
22 #include <sys/socket.h>
24 #include <sys/ioctl.h>
26 #include <sys/fcntlcom.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <netinet/if_ether.h>
36 #include <netinet/ip_var.h>
37 #include <netinet/udp.h>
38 #include <netinet/udp_var.h>
39 #include <netinet/tcp.h>
40 #include <netinet/tcpip.h>
41 #include "ip_compat.h"
44 static char sbpf[] = "@(#)sbpf.c 1.2 12/3/95 (C)1995 Darren Reed";
49 (001) jeq #0x800 jt 2 jf 5
51 (003) jeq #0x6 jt 4 jf 5
55 struct bpf_insn filter[] = {
56 /* 0. */ { BPF_LD|BPF_H|BPF_ABS, 0, 0, 12 },
57 /* 1. */ { BPF_JMP|BPF_JEQ, 0, 3, 0x0800 },
58 /* 2. */ { BPF_LD|BPF_B|BPF_ABS, 0, 0, 23 },
59 /* 3. */ { BPF_JMP|BPF_JEQ, 0, 1, 0x06 },
60 /* 4. */ { BPF_RET, 0, 0, 68 },
61 /* 5. */ { BPF_RET, 0, 0, 0 }
64 * the code herein is dervied from libpcap.
66 static u_char *buf = NULL;
67 static u_int bufsize = 32768, timeout = 1;
78 tcp = (tcphdr_t *)(ip + 1);
79 bcopy(ep + 14, (char *)ip, sizeof(*ip));
80 bcopy(ep + 14 + (ip->ip_hl << 2), (char *)tcp, sizeof(*tcp));
81 if (ip->ip_p != IPPROTO_TCP && ip->ip_p != IPPROTO_UDP)
83 if (ip->ip_p & 0x1fff != 0)
85 if (0 == detect(ip, tcp))
91 int readloop(fd, port, dst)
95 register u_char *bp, *cp, *bufend;
96 register struct bpf_hdr *bh;
98 time_t in = time(NULL);
101 while ((cc = read(fd, buf, bufsize)) >= 0) {
102 if (!cc && (time(NULL) - in) > timeout)
107 * loop through each snapshot in the chunk
109 while (bp < bufend) {
110 bh = (struct bpf_hdr *)bp;
111 cp = bp + bh->bh_hdrlen;
112 done += ack_recv(cp);
113 bp += BPF_WORDALIGN(bh->bh_caplen + bh->bh_hdrlen);
121 int initdevice(device, tout)
125 struct bpf_program prog;
126 struct bpf_version bv;
130 char *bpfname = _PATH_BPF;
133 if ((fd = open(bpfname, O_RDWR)) < 0)
135 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
142 for (i = 0; i < 16; i++)
144 (void) sprintf(bpfname, "/dev/bpf%d", i);
145 if ((fd = open(bpfname, O_RDWR)) >= 0)
150 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
155 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
157 perror("BIOCVERSION");
160 if (bv.bv_major != BPF_MAJOR_VERSION ||
161 bv.bv_minor < BPF_MINOR_VERSION)
163 fprintf(stderr, "kernel bpf (v%d.%d) filter out of date:\n",
164 bv.bv_major, bv.bv_minor);
165 fprintf(stderr, "current version: %d.%d\n",
166 BPF_MAJOR_VERSION, BPF_MINOR_VERSION);
170 (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
171 if (ioctl(fd, BIOCSETIF, &ifr) == -1)
173 fprintf(stderr, "%s(%d):", ifr.ifr_name, fd);
183 if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1)
185 perror("BIOCSRTIMEOUT");
189 * get kernel buffer size
191 if (ioctl(fd, BIOCSBLEN, &bufsize) == -1)
193 if (ioctl(fd, BIOCGBLEN, &bufsize) == -1)
198 printf("BPF buffer size: %d\n", bufsize);
199 buf = (u_char*)malloc(bufsize);
201 prog.bf_len = sizeof(filter) / sizeof(struct bpf_insn);
202 prog.bf_insns = filter;
203 if (ioctl(fd, BIOCSETF, (caddr_t)&prog) == -1)
208 (void) ioctl(fd, BIOCFLUSH, 0);