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