Sync usage with man page.
[netbsd-mini2440.git] / dist / ipf / ipsd / sdlpi.c
blob09026cfd8420178988df31a8554df2dc20bc222a
1 /* $NetBSD: sdlpi.c,v 1.2 2002/01/24 08:21:38 martti Exp $ */
3 /*
4 * (C)opyright 1992-1998 Darren Reed. (from tcplog)
6 * See the IPFILTER.LICENCE file for details on licencing.
8 */
10 #include <stdio.h>
11 #include <netdb.h>
12 #include <ctype.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/timeb.h>
19 #include <sys/socket.h>
20 #include <sys/file.h>
21 #include <sys/ioctl.h>
22 #include <sys/stropts.h>
24 #include <sys/pfmod.h>
25 #include <sys/bufmod.h>
26 #include <sys/dlpi.h>
28 #include <net/if.h>
29 #include <netinet/in.h>
30 #include <netinet/in_systm.h>
31 #include <netinet/ip.h>
32 #include <netinet/if_ether.h>
33 #include <netinet/ip_var.h>
34 #include <netinet/udp.h>
35 #include <netinet/udp_var.h>
36 #include <netinet/tcp.h>
37 #include <netinet/tcpip.h>
39 #include "ip_compat.h"
41 #ifndef lint
42 static char snitid[] = "%W% %G% (C)1995 Darren Reed";
43 #endif
45 #define BUFSPACE 32768
47 static int solfd;
50 * Be careful to only include those defined in the flags option for the
51 * interface are included in the header size.
53 static int timeout;
56 void nullbell()
58 return 0;
62 int ack_recv(ep)
63 char *ep;
65 struct tcpiphdr tip;
66 tcphdr_t *tcp;
67 ip_t *ip;
69 ip = (ip_t *)&tip;
70 tcp = (tcphdr_t *)(ip + 1);
71 bcopy(ep, (char *)ip, sizeof(*ip));
72 bcopy(ep + (ip->ip_hl << 2), (char *)tcp, sizeof(*tcp));
74 if (ip->ip_off & 0x1fff != 0)
75 return 0;
76 if (0 == detect(ip, tcp))
77 return 1;
78 return 0;
82 int readloop(fd, port, dst)
83 int fd, port;
84 struct in_addr dst;
86 static u_char buf[BUFSPACE];
87 register u_char *bp, *cp, *bufend;
88 register struct sb_hdr *hp;
89 register int cc;
90 struct strbuf dbuf;
91 ether_header_t eh;
92 time_t now = time(NULL);
93 int flags = 0, i, done = 0;
95 fd = solfd;
96 dbuf.len = 0;
97 dbuf.buf = buf;
98 dbuf.maxlen = sizeof(buf);
100 * no control data buffer...
102 while (1) {
103 (void) signal(SIGALRM, nullbell);
104 alarm(1);
105 i = getmsg(fd, NULL, &dbuf, &flags);
106 alarm(0);
107 (void) signal(SIGALRM, nullbell);
109 cc = dbuf.len;
110 if ((time(NULL) - now) > timeout)
111 return done;
112 if (i == -1)
113 if (errno == EINTR)
114 continue;
115 else
116 break;
117 bp = buf;
118 bufend = buf + cc;
120 * loop through each snapshot in the chunk
122 while (bp < bufend) {
124 * get past bufmod header
126 hp = (struct sb_hdr *)bp;
127 cp = (u_char *)((char *)bp + sizeof(*hp));
128 bcopy(cp, (char *)&eh, sizeof(eh));
130 * next snapshot
132 bp += hp->sbh_totlen;
133 cc -= hp->sbh_totlen;
135 if (eh.ether_type != ETHERTYPE_IP)
136 continue;
138 cp += sizeof(eh);
139 done += ack_recv(cp);
141 alarm(1);
143 perror("getmsg");
144 exit(-1);
147 int initdevice(device, tout)
148 char *device;
149 int tout;
151 struct strioctl si;
152 struct timeval to;
153 struct ifreq ifr;
154 struct packetfilt pfil;
155 u_long if_flags;
156 u_short *fwp = pfil.Pf_Filter;
157 char devname[16], *s, buf[256];
158 int i, offset, fd, snaplen= 58, chunksize = BUFSPACE;
160 (void) sprintf(devname, "/dev/%s", device);
162 s = devname + 5;
163 while (*s && !ISDIGIT(*s))
164 s++;
165 if (!*s)
167 fprintf(stderr, "bad device name %s\n", devname);
168 exit(-1);
170 i = atoi(s);
171 *s = '\0';
173 * For reading
175 if ((fd = open(devname, O_RDWR)) < 0)
177 fprintf(stderr, "O_RDWR(0) ");
178 perror(devname);
179 exit(-1);
181 if (dlattachreq(fd, i) == -1 || dlokack(fd, buf) == -1)
183 fprintf(stderr, "DLPI error\n");
184 exit(-1);
186 dlbindreq(fd, ETHERTYPE_IP, 0, DL_CLDLS, 0, 0);
187 dlbindack(fd, buf);
189 * read full headers
191 if (strioctl(fd, DLIOCRAW, -1, 0, NULL) == -1)
193 fprintf(stderr, "DLIOCRAW error\n");
194 exit(-1);
197 * Create some filter rules for our TCP watcher. We only want ethernet
198 * pacets which are IP protocol and only the TCP packets from IP.
200 offset = 6;
201 *fwp++ = ENF_PUSHWORD + offset;
202 *fwp++ = ENF_PUSHLIT | ENF_CAND;
203 *fwp++ = htons(ETHERTYPE_IP);
204 *fwp++ = ENF_PUSHWORD + sizeof(struct ether_header)/sizeof(short)+4;
205 *fwp++ = ENF_PUSHLIT | ENF_AND;
206 *fwp++ = htons(0x00ff);
207 *fwp++ = ENF_PUSHLIT | ENF_COR;
208 *fwp++ = htons(IPPROTO_TCP);
209 *fwp++ = ENF_PUSHWORD + sizeof(struct ether_header)/sizeof(short)+4;
210 *fwp++ = ENF_PUSHLIT | ENF_AND;
211 *fwp++ = htons(0x00ff);
212 *fwp++ = ENF_PUSHLIT | ENF_CAND;
213 *fwp++ = htons(IPPROTO_UDP);
214 pfil.Pf_FilterLen = (fwp - &pfil.Pf_Filter[0]);
216 * put filter in place.
219 if (ioctl(fd, I_PUSH, "pfmod") == -1)
221 perror("ioctl: I_PUSH pf");
222 exit(1);
224 if (strioctl(fd, PFIOCSETF, -1, sizeof(pfil), (char *)&pfil) == -1)
226 perror("ioctl: PFIOCSETF");
227 exit(1);
231 * arrange to get messages from the NIT STREAM and use NIT_BUF option
233 if (ioctl(fd, I_PUSH, "bufmod") == -1)
235 perror("ioctl: I_PUSH bufmod");
236 exit(1);
238 i = 128;
239 strioctl(fd, SBIOCSSNAP, -1, sizeof(i), (char *)&i);
241 * set the timeout
243 to.tv_sec = 1;
244 to.tv_usec = 0;
245 if (strioctl(fd, SBIOCSTIME, -1, sizeof(to), (char *)&to) == -1)
247 perror("strioctl(SBIOCSTIME)");
248 exit(-1);
251 * flush read queue
253 if (ioctl(fd, I_FLUSH, FLUSHR) == -1)
255 perror("I_FLUSHR");
256 exit(-1);
258 timeout = tout;
259 solfd = fd;
260 return fd;