4 * (C)opyright 1992-1998 Darren Reed. (from tcplog)
6 * See the IPFILTER.LICENCE file for details on licencing.
15 #include <sys/types.h>
17 #include <sys/timeb.h>
18 #include <sys/socket.h>
20 #include <sys/ioctl.h>
22 #include <sys/fcntlcom.h>
24 #include <net/nit_if.h>
25 #include <net/nit_pf.h>
26 #include <net/nit_buf.h>
27 #include <net/packetfilt.h>
28 #include <sys/stropts.h>
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/if_ether.h>
35 #include <netinet/ip_var.h>
36 #include <netinet/udp.h>
37 #include <netinet/udp_var.h>
38 #include <netinet/tcp.h>
39 #include <netinet/tcpip.h>
42 static char snitid[] = "@(#)snit.c 1.2 12/3/95 (C)1995 Darren Reed";
45 #define BUFSPACE 32768
48 * Be careful to only include those defined in the flags option for the
49 * interface are included in the header size.
51 #define BUFHDR_SIZE (sizeof(struct nit_bufhdr))
52 #define NIT_HDRSIZE (BUFHDR_SIZE)
64 ip = (struct ip *)&tip;
65 tcp = (struct tcphdr *)(ip + 1);
66 bcopy(ep + 14, (char *)ip, sizeof(*ip));
67 bcopy(ep + 14 + (ip->ip_hl << 2), (char *)tcp, sizeof(*tcp));
68 if (ip->ip_off & 0x1fff != 0)
70 if (0 == detect(ip, tcp))
80 static u_char buf[BUFSPACE];
81 register u_char *bp, *cp, *bufend;
82 register struct nit_bufhdr *hp;
84 time_t now = time(NULL);
87 while ((cc = read(fd, buf, BUFSPACE-1)) >= 0) {
89 if ((time(NULL) - now) > timeout)
96 * loop through each snapshot in the chunk
99 cp = (u_char *)((char *)bp + NIT_HDRSIZE);
101 * get past NIT buffer
103 hp = (struct nit_bufhdr *)bp;
107 bp += hp->nhb_totlen;
108 done += ack_recv(cp);
116 int initdevice(device, tout)
123 struct packetfilt pfil;
125 u_short *fwp = pfil.Pf_Filter;
126 int ret, offset, fd, snaplen= 76, chunksize = BUFSPACE;
128 if ((fd = open("/dev/nit", O_RDWR)) < 0)
135 * Create some filter rules for our TCP watcher. We only want ethernet
136 * pacets which are IP protocol and only the TCP packets from IP.
139 *fwp++ = ENF_PUSHWORD + offset;
140 *fwp++ = ENF_PUSHLIT | ENF_CAND;
141 *fwp++ = htons(ETHERTYPE_IP);
142 *fwp++ = ENF_PUSHWORD + sizeof(struct ether_header)/sizeof(short)+4;
143 *fwp++ = ENF_PUSHLIT | ENF_AND;
144 *fwp++ = htons(0x00ff);
145 *fwp++ = ENF_PUSHLIT | ENF_COR;
146 *fwp++ = htons(IPPROTO_TCP);
147 *fwp++ = ENF_PUSHWORD + sizeof(struct ether_header)/sizeof(short)+4;
148 *fwp++ = ENF_PUSHLIT | ENF_AND;
149 *fwp++ = htons(0x00ff);
150 *fwp++ = ENF_PUSHLIT | ENF_CAND;
151 *fwp++ = htons(IPPROTO_UDP);
152 pfil.Pf_FilterLen = fwp - &pfil.Pf_Filter[0];
154 * put filter in place.
156 if (ioctl(fd, I_PUSH, "pf") == -1)
158 perror("ioctl: I_PUSH pf");
161 if (ioctl(fd, NIOCSETF, &pfil) == -1)
163 perror("ioctl: NIOCSETF");
167 * arrange to get messages from the NIT STREAM and use NIT_BUF option
169 ioctl(fd, I_SRDOPT, (char*)RMSGD);
170 ioctl(fd, I_PUSH, "nbuf");
178 si.ic_cmd = NIOCSTIME;
179 si.ic_len = sizeof(to);
180 si.ic_dp = (char*)&to;
181 if (ioctl(fd, I_STR, (char*)&si) == -1)
183 perror("ioctl: NIT timeout");
189 si.ic_cmd = NIOCSCHUNK;
190 si.ic_len = sizeof(chunksize);
191 si.ic_dp = (char*)&chunksize;
192 if (ioctl(fd, I_STR, (char*)&si) == -1)
193 perror("ioctl: NIT chunksize");
194 if (ioctl(fd, NIOCGCHUNK, (char*)&chunksize) == -1)
196 perror("ioctl: NIT chunksize");
199 printf("NIT buffer size: %d\n", chunksize);
202 * request the interface
204 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
205 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' ';
206 si.ic_cmd = NIOCBIND;
207 si.ic_len = sizeof(ifr);
208 si.ic_dp = (char*)𝔦
209 if (ioctl(fd, I_STR, (char*)&si) == -1)
211 perror(ifr.ifr_name);
216 * set the snapshot length
218 si.ic_cmd = NIOCSSNAP;
219 si.ic_len = sizeof(snaplen);
220 si.ic_dp = (char*)&snaplen;
221 if (ioctl(fd, I_STR, (char*)&si) == -1)
223 perror("ioctl: NIT snaplen");
226 (void) ioctl(fd, I_FLUSH, (char*)FLUSHR);