]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ipft_sn.c
Import of ipfilter 3.3.3 in anticipation of its revival.
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ipft_sn.c
1 /*
2  * Copyright (C) 1993-1998 by Darren Reed.
3  *
4  * Redistribution and use in source and binary forms are permitted
5  * provided that this notice is preserved and due credit is given
6  * to the original author and the contributors.
7  */
8
9 /*
10  * Written to comply with the recent RFC 1761 from Sun.
11  */
12 #include <stdio.h>
13 #include <string.h>
14 #if !defined(__SVR4) && !defined(__GNUC__)
15 #include <strings.h>
16 #endif
17 #include <sys/types.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stddef.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
24 #include <sys/time.h>
25 #include <netinet/in.h>
26 #include <netinet/in_systm.h>
27 #ifndef linux
28 #include <netinet/ip_var.h>
29 #endif
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
32 #include <net/if.h>
33 #include "ip_compat.h"
34 #include <netinet/tcpip.h>
35 #include "ipf.h"
36 #include "snoop.h"
37 #include "ipt.h"
38
39 #if !defined(lint)
40 static const char rcsid[] = "@(#)$Id: ipft_sn.c,v 2.1 1999/08/04 17:30:04 darrenr Exp $";
41 #endif
42
43 struct  llc     {
44         int     lc_sz;  /* LLC header length */
45         int     lc_to;  /* LLC Type offset */
46         int     lc_tl;  /* LLC Type length */
47 };
48
49 /*
50  * While many of these maybe the same, some do have different header formats
51  * which make this useful.
52  */
53 static  struct  llc     llcs[SDL_MAX+1] = {
54         { 0, 0, 0 },    /* SDL_8023 */
55         { 0, 0, 0 },    /* SDL_8024 */
56         { 0, 0, 0 },    /* SDL_8025 */
57         { 0, 0, 0 },    /* SDL_8026 */
58         { 14, 12, 2 },  /* SDL_ETHER */
59         { 0, 0, 0 },    /* SDL_HDLC */
60         { 0, 0, 0 },    /* SDL_CHSYNC */
61         { 0, 0, 0 },    /* SDL_IBMCC */
62         { 0, 0, 0 },    /* SDL_FDDI */
63         { 0, 0, 0 },    /* SDL_OTHER */
64 };
65
66 static  int     snoop_open __P((char *));
67 static  int     snoop_close __P((void));
68 static  int     snoop_readip __P((char *, int, char **, int *));
69
70 static  int     sfd = -1, s_type = -1;
71 static  int     snoop_read_rec __P((struct snooppkt *));
72
73 struct  ipread  snoop = { snoop_open, snoop_close, snoop_readip };
74
75
76 static  int     snoop_open(fname)
77 char    *fname;
78 {
79         struct  snoophdr sh;
80         int     fd;
81
82         if (sfd != -1)
83                 return sfd;
84
85         if (!strcmp(fname, "-"))
86                 fd = 0;
87         else if ((fd = open(fname, O_RDONLY)) == -1)
88                 return -1;
89
90         if (read(fd, (char *)&sh, sizeof(sh)) != sizeof(sh))
91                 return -2;
92
93         if (sh.s_v != SNOOP_VERSION ||
94             sh.s_type < 0 || sh.s_type > SDL_MAX) {
95                 (void) close(fd);
96                 return -2;
97         }
98
99         sfd = fd;
100         s_type = sh.s_type;
101         printf("opened snoop file %s:\n", fname);
102         printf("\tid: %8.8s version: %d type: %d\n", sh.s_id, sh.s_v, s_type);
103
104         return fd;
105 }
106
107
108 static  int     snoop_close()
109 {
110         return close(sfd);
111 }
112
113
114 /*
115  * read in the header (and validate) which should be the first record
116  * in a snoop file.
117  */
118 static  int     snoop_read_rec(rec)
119 struct  snooppkt *rec;
120 {
121         int     n, p;
122
123         if (read(sfd, (char *)rec, sizeof(*rec)) != sizeof(*rec))
124                 return -2;
125
126         if (rec->sp_ilen > rec->sp_plen || rec->sp_plen < sizeof(*rec))
127                 return -2;
128
129         p = rec->sp_plen - sizeof(*rec);
130         n = MIN(p, rec->sp_ilen);
131         if (!n || n < 0)
132                 return -3;
133
134         return p;
135 }
136
137
138 #ifdef  notyet
139 /*
140  * read an entire snoop packet record.  only the data part is copied into
141  * the available buffer, with the number of bytes copied returned.
142  */
143 static  int     snoop_read(buf, cnt)
144 char    *buf;
145 int     cnt;
146 {
147         struct  snooppkt rec;
148         static  char    *bufp = NULL;
149         int     i, n;
150
151         if ((i = snoop_read_rec(&rec)) <= 0)
152                 return i;
153
154         if (!bufp)
155                 bufp = malloc(i);
156         else
157                 bufp = realloc(bufp, i);
158
159         if (read(sfd, bufp, i) != i)
160                 return -2;
161
162         n = MIN(i, cnt);
163         bcopy(bufp, buf, n);
164         return n;
165 }
166 #endif
167
168
169 /*
170  * return only an IP packet read into buf
171  */
172 static  int     snoop_readip(buf, cnt, ifn, dir)
173 char    *buf, **ifn;
174 int     cnt, *dir;
175 {
176         static  char    *bufp = NULL;
177         struct  snooppkt rec;
178         struct  llc     *l;
179         char    ty[4], *s;
180         int     i, n;
181
182         do {
183                 if ((i = snoop_read_rec(&rec)) <= 0)
184                         return i;
185
186                 if (!bufp)
187                         bufp = malloc(i);
188                 else
189                         bufp = realloc(bufp, i);
190                 s = bufp;
191
192                 if (read(sfd, s, i) != i)
193                         return -2;
194
195                 l = &llcs[s_type];
196                 i -= l->lc_to;
197                 s += l->lc_to;
198                 /*
199                  * XXX - bogus assumption here on the part of the time field
200                  * that it won't be greater than 4 bytes and the 1st two will
201                  * have the values 8 and 0 for IP.  Should be a table of
202                  * these too somewhere.  Really only works for SDL_ETHER.
203                  */
204                 bcopy(s, ty, l->lc_tl);
205         } while (ty[0] != 0x8 && ty[1] != 0);
206
207         i -= l->lc_tl;
208         s += l->lc_tl;
209         n = MIN(i, cnt);
210         bcopy(s, buf, n);
211
212         return n;
213 }