xref: /freebsd/sbin/ipf/libipf/ipft_pc.c (revision 963f5dc7a30624e95d72fb7f87b8892651164e46)
1 /*	$FreeBSD$	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id$
9  */
10 #include "ipf.h"
11 #include "ipt.h"
12 
13 #if !defined(lint)
14 static const char rcsid[] = "@(#)$Id$";
15 #endif
16 
17 struct	llc	{
18 	int	lc_type;
19 	int	lc_sz;	/* LLC header length */
20 	int	lc_to;	/* LLC Type offset */
21 	int	lc_tl;	/* LLC Type length */
22 };
23 
24 /*
25  * While many of these maybe the same, some do have different header formats
26  * which make this useful.
27  */
28 
29 static	struct	llc	llcs[] = {
30 	{ 0, 0, 0, 0 },				/* DLT_NULL */
31 	{ 1, 14, 12, 2 },			/* DLT_Ethernet */
32 	{ 10, 0, 0, 0 },			/* DLT_FDDI */
33 	{ 12, 0, 0, 0 },			/* DLT_RAW */
34 	{ -1, -1, -1, -1 }
35 };
36 
37 typedef struct {
38 	u_int	id;
39 	u_short	major;
40 	u_short	minor;
41 	u_int	timezone;
42 	u_int	sigfigs;
43 	u_int	snaplen;
44 	u_int	type;
45 } fileheader_t;
46 
47 typedef struct {
48 	u_32_t	seconds;
49 	u_32_t	microseconds;
50 	u_32_t	caplen;
51 	u_32_t	wirelen;
52 } packetheader_t;
53 
54 static	int	ipcap_open(char *);
55 static	int	ipcap_close(void);
56 static	int	ipcap_readip(mb_t *, char **, int *);
57 static	int	ipcap_read_rec(packetheader_t *);
58 static	void	iswap_hdr(fileheader_t *);
59 
60 static	int	pfd = -1, swapped = 0;
61 static	struct llc	*llcp = NULL;
62 
63 struct	ipread	pcap = { ipcap_open, ipcap_close, ipcap_readip, 0 };
64 
65 #define	SWAPLONG(y)	\
66 	((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
67 #define	SWAPSHORT(y)	\
68 	( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
69 
70 static	void	iswap_hdr(p)
71 	fileheader_t	*p;
72 {
73 	p->major = SWAPSHORT(p->major);
74 	p->minor = SWAPSHORT(p->minor);
75 	p->timezone = SWAPLONG(p->timezone);
76 	p->sigfigs = SWAPLONG(p->sigfigs);
77 	p->snaplen = SWAPLONG(p->snaplen);
78 	p->type = SWAPLONG(p->type);
79 }
80 
81 static	int	ipcap_open(fname)
82 	char	*fname;
83 {
84 	fileheader_t ph;
85 	int fd, i;
86 
87 	if (pfd != -1)
88 		return pfd;
89 
90 	if (!strcmp(fname, "-"))
91 		fd = 0;
92 	else if ((fd = open(fname, O_RDONLY)) == -1)
93 		return -1;
94 
95 	if (read(fd, (char *)&ph, sizeof(ph)) != sizeof(ph))
96 		return -2;
97 
98 	if (ph.id != 0xa1b2c3d4) {
99 		if (SWAPLONG(ph.id) != 0xa1b2c3d4) {
100 			(void) close(fd);
101 			return -2;
102 		}
103 		swapped = 1;
104 		iswap_hdr(&ph);
105 	}
106 
107 	for (i = 0; llcs[i].lc_type != -1; i++)
108 		if (llcs[i].lc_type == ph.type) {
109 			llcp = llcs + i;
110 			break;
111 		}
112 
113 	if (llcp == NULL) {
114 		(void) close(fd);
115 		return -2;
116 	}
117 
118 	pfd = fd;
119 	printf("opened pcap file %s:\n", fname);
120 	printf("\tid: %08x version: %d.%d type: %d snap %d\n",
121 		ph.id, ph.major, ph.minor, ph.type, ph.snaplen);
122 
123 	return fd;
124 }
125 
126 
127 static	int	ipcap_close()
128 {
129 	return close(pfd);
130 }
131 
132 
133 /*
134  * read in the header (and validate) which should be the first record
135  * in a pcap file.
136  */
137 static	int	ipcap_read_rec(rec)
138 	packetheader_t *rec;
139 {
140 	int	n, p, i;
141 
142 	n = sizeof(*rec);
143 
144 	while (n > 0) {
145 		i = read(pfd, (char *)rec, sizeof(*rec));
146 		if (i <= 0)
147 			return -2;
148 		n -= i;
149 	}
150 
151 	if (swapped) {
152 		rec->caplen = SWAPLONG(rec->caplen);
153 		rec->wirelen = SWAPLONG(rec->wirelen);
154 		rec->seconds = SWAPLONG(rec->seconds);
155 		rec->microseconds = SWAPLONG(rec->microseconds);
156 	}
157 	p = rec->caplen;
158 	n = MIN(p, rec->wirelen);
159 	if (!n || n < 0)
160 		return -3;
161 
162 	if (p < 0 || p > 65536)
163 		return -4;
164 	return p;
165 }
166 
167 
168 #ifdef	notyet
169 /*
170  * read an entire pcap packet record.  only the data part is copied into
171  * the available buffer, with the number of bytes copied returned.
172  */
173 static	int	ipcap_read(buf, cnt)
174 	char	*buf;
175 	int	cnt;
176 {
177 	packetheader_t rec;
178 	static	char	*bufp = NULL;
179 	int	i, n;
180 
181 	if ((i = ipcap_read_rec(&rec)) <= 0)
182 		return i;
183 
184 	if (!bufp)
185 		bufp = malloc(i);
186 	else
187 		bufp = realloc(bufp, i);
188 
189 	if (read(pfd, bufp, i) != i)
190 		return -2;
191 
192 	n = MIN(i, cnt);
193 	bcopy(bufp, buf, n);
194 	return n;
195 }
196 #endif
197 
198 
199 /*
200  * return only an IP packet read into buf
201  */
202 static	int	ipcap_readip(mb, ifn, dir)
203 	mb_t	*mb;
204 	char	**ifn;
205 	int	*dir;
206 {
207 	static	char	*bufp = NULL;
208 	packetheader_t	rec;
209 	struct	llc	*l;
210 	char	*s, ty[4];
211 	int	i, j, n;
212 	char	*buf;
213 	int	cnt;
214 
215 #if 0
216 	ifn = ifn;	/* gcc -Wextra */
217 	dir = dir;	/* gcc -Wextra */
218 #endif
219 	buf = (char *)mb->mb_buf;
220 	cnt = sizeof(mb->mb_buf);
221 	l = llcp;
222 
223 	/* do { */
224 		if ((i = ipcap_read_rec(&rec)) <= 0)
225 			return i;
226 
227 		if (!bufp)
228 			bufp = malloc(i);
229 		else
230 			bufp = realloc(bufp, i);
231 		s = bufp;
232 
233 		for (j = i, n = 0; j > 0; ) {
234 			n = read(pfd, s, j);
235 			if (n <= 0)
236 				return -2;
237 			j -= n;
238 			s += n;
239 		}
240 		s = bufp;
241 
242 		i -= l->lc_sz;
243 		s += l->lc_to;
244 		bcopy(s, ty, l->lc_tl);
245 		s += l->lc_tl;
246 	/* } while (ty[0] != 0x8 && ty[1] != 0); */
247 	n = MIN(i, cnt);
248 	bcopy(s, buf, n);
249 	mb->mb_len = n;
250 	return n;
251 }
252