xref: /freebsd/sbin/ipf/libipf/ipft_pc.c (revision 51e16cb8fc536913f490ac6bc9c17e92ebd0411b)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert  *
541edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert  *
741edb306SCy Schubert  * $Id$
841edb306SCy Schubert  */
941edb306SCy Schubert #include "ipf.h"
1041edb306SCy Schubert #include "ipt.h"
1141edb306SCy Schubert 
1241edb306SCy Schubert 
1341edb306SCy Schubert struct	llc	{
1441edb306SCy Schubert 	int	lc_type;
1541edb306SCy Schubert 	int	lc_sz;	/* LLC header length */
1641edb306SCy Schubert 	int	lc_to;	/* LLC Type offset */
1741edb306SCy Schubert 	int	lc_tl;	/* LLC Type length */
1841edb306SCy Schubert };
1941edb306SCy Schubert 
2041edb306SCy Schubert /*
2141edb306SCy Schubert  * While many of these maybe the same, some do have different header formats
2241edb306SCy Schubert  * which make this useful.
2341edb306SCy Schubert  */
2441edb306SCy Schubert 
2541edb306SCy Schubert static	struct	llc	llcs[] = {
2641edb306SCy Schubert 	{ 0, 0, 0, 0 },				/* DLT_NULL */
2741edb306SCy Schubert 	{ 1, 14, 12, 2 },			/* DLT_Ethernet */
2841edb306SCy Schubert 	{ 10, 0, 0, 0 },			/* DLT_FDDI */
2941edb306SCy Schubert 	{ 12, 0, 0, 0 },			/* DLT_RAW */
3041edb306SCy Schubert 	{ -1, -1, -1, -1 }
3141edb306SCy Schubert };
3241edb306SCy Schubert 
3341edb306SCy Schubert typedef struct {
3441edb306SCy Schubert 	u_int	id;
3541edb306SCy Schubert 	u_short	major;
3641edb306SCy Schubert 	u_short	minor;
3741edb306SCy Schubert 	u_int	timezone;
3841edb306SCy Schubert 	u_int	sigfigs;
3941edb306SCy Schubert 	u_int	snaplen;
4041edb306SCy Schubert 	u_int	type;
4141edb306SCy Schubert } fileheader_t;
4241edb306SCy Schubert 
4341edb306SCy Schubert typedef struct {
4441edb306SCy Schubert 	u_32_t	seconds;
4541edb306SCy Schubert 	u_32_t	microseconds;
4641edb306SCy Schubert 	u_32_t	caplen;
4741edb306SCy Schubert 	u_32_t	wirelen;
4841edb306SCy Schubert } packetheader_t;
4941edb306SCy Schubert 
5041edb306SCy Schubert static	int	ipcap_open(char *);
5141edb306SCy Schubert static	int	ipcap_close(void);
5241edb306SCy Schubert static	int	ipcap_readip(mb_t *, char **, int *);
5341edb306SCy Schubert static	int	ipcap_read_rec(packetheader_t *);
5441edb306SCy Schubert static	void	iswap_hdr(fileheader_t *);
5541edb306SCy Schubert 
5641edb306SCy Schubert static	int	pfd = -1, swapped = 0;
5741edb306SCy Schubert static	struct llc	*llcp = NULL;
5841edb306SCy Schubert 
5941edb306SCy Schubert struct	ipread	pcap = { ipcap_open, ipcap_close, ipcap_readip, 0 };
6041edb306SCy Schubert 
6141edb306SCy Schubert #define	SWAPLONG(y)	\
6241edb306SCy Schubert 	((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
6341edb306SCy Schubert #define	SWAPSHORT(y)	\
6441edb306SCy Schubert 	( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
6541edb306SCy Schubert 
66efeb8bffSCy Schubert static void
iswap_hdr(fileheader_t * p)67efeb8bffSCy Schubert iswap_hdr(fileheader_t *p)
6841edb306SCy Schubert {
6941edb306SCy Schubert 	p->major = SWAPSHORT(p->major);
7041edb306SCy Schubert 	p->minor = SWAPSHORT(p->minor);
7141edb306SCy Schubert 	p->timezone = SWAPLONG(p->timezone);
7241edb306SCy Schubert 	p->sigfigs = SWAPLONG(p->sigfigs);
7341edb306SCy Schubert 	p->snaplen = SWAPLONG(p->snaplen);
7441edb306SCy Schubert 	p->type = SWAPLONG(p->type);
7541edb306SCy Schubert }
7641edb306SCy Schubert 
77efeb8bffSCy Schubert static int
ipcap_open(char * fname)78efeb8bffSCy Schubert ipcap_open(char *fname)
7941edb306SCy Schubert {
8041edb306SCy Schubert 	fileheader_t ph;
8141edb306SCy Schubert 	int fd, i;
8241edb306SCy Schubert 
8341edb306SCy Schubert 	if (pfd != -1)
84*2582ae57SCy Schubert 		return (pfd);
8541edb306SCy Schubert 
8641edb306SCy Schubert 	if (!strcmp(fname, "-"))
8741edb306SCy Schubert 		fd = 0;
8841edb306SCy Schubert 	else if ((fd = open(fname, O_RDONLY)) == -1)
89*2582ae57SCy Schubert 		return (-1);
9041edb306SCy Schubert 
9141edb306SCy Schubert 	if (read(fd, (char *)&ph, sizeof(ph)) != sizeof(ph))
92*2582ae57SCy Schubert 		return (-2);
9341edb306SCy Schubert 
9441edb306SCy Schubert 	if (ph.id != 0xa1b2c3d4) {
9541edb306SCy Schubert 		if (SWAPLONG(ph.id) != 0xa1b2c3d4) {
9641edb306SCy Schubert 			(void) close(fd);
97*2582ae57SCy Schubert 			return (-2);
9841edb306SCy Schubert 		}
9941edb306SCy Schubert 		swapped = 1;
10041edb306SCy Schubert 		iswap_hdr(&ph);
10141edb306SCy Schubert 	}
10241edb306SCy Schubert 
10341edb306SCy Schubert 	for (i = 0; llcs[i].lc_type != -1; i++)
10441edb306SCy Schubert 		if (llcs[i].lc_type == ph.type) {
10541edb306SCy Schubert 			llcp = llcs + i;
10641edb306SCy Schubert 			break;
10741edb306SCy Schubert 		}
10841edb306SCy Schubert 
10941edb306SCy Schubert 	if (llcp == NULL) {
11041edb306SCy Schubert 		(void) close(fd);
111*2582ae57SCy Schubert 		return (-2);
11241edb306SCy Schubert 	}
11341edb306SCy Schubert 
11441edb306SCy Schubert 	pfd = fd;
11541edb306SCy Schubert 	printf("opened pcap file %s:\n", fname);
11641edb306SCy Schubert 	printf("\tid: %08x version: %d.%d type: %d snap %d\n",
11741edb306SCy Schubert 		ph.id, ph.major, ph.minor, ph.type, ph.snaplen);
11841edb306SCy Schubert 
119*2582ae57SCy Schubert 	return (fd);
12041edb306SCy Schubert }
12141edb306SCy Schubert 
12241edb306SCy Schubert 
123efeb8bffSCy Schubert static int
ipcap_close(void)124efeb8bffSCy Schubert ipcap_close(void)
12541edb306SCy Schubert {
126*2582ae57SCy Schubert 	return (close(pfd));
12741edb306SCy Schubert }
12841edb306SCy Schubert 
12941edb306SCy Schubert 
13041edb306SCy Schubert /*
13141edb306SCy Schubert  * read in the header (and validate) which should be the first record
13241edb306SCy Schubert  * in a pcap file.
13341edb306SCy Schubert  */
134efeb8bffSCy Schubert static int
ipcap_read_rec(packetheader_t * rec)135efeb8bffSCy Schubert ipcap_read_rec(packetheader_t *rec)
13641edb306SCy Schubert {
13741edb306SCy Schubert 	int	n, p, i;
13841edb306SCy Schubert 
13941edb306SCy Schubert 	n = sizeof(*rec);
14041edb306SCy Schubert 
14141edb306SCy Schubert 	while (n > 0) {
14241edb306SCy Schubert 		i = read(pfd, (char *)rec, sizeof(*rec));
14341edb306SCy Schubert 		if (i <= 0)
144*2582ae57SCy Schubert 			return (-2);
14541edb306SCy Schubert 		n -= i;
14641edb306SCy Schubert 	}
14741edb306SCy Schubert 
14841edb306SCy Schubert 	if (swapped) {
14941edb306SCy Schubert 		rec->caplen = SWAPLONG(rec->caplen);
15041edb306SCy Schubert 		rec->wirelen = SWAPLONG(rec->wirelen);
15141edb306SCy Schubert 		rec->seconds = SWAPLONG(rec->seconds);
15241edb306SCy Schubert 		rec->microseconds = SWAPLONG(rec->microseconds);
15341edb306SCy Schubert 	}
15441edb306SCy Schubert 	p = rec->caplen;
15541edb306SCy Schubert 	n = MIN(p, rec->wirelen);
15641edb306SCy Schubert 	if (!n || n < 0)
157*2582ae57SCy Schubert 		return (-3);
15841edb306SCy Schubert 
15941edb306SCy Schubert 	if (p < 0 || p > 65536)
160*2582ae57SCy Schubert 		return (-4);
161*2582ae57SCy Schubert 	return (p);
16241edb306SCy Schubert }
16341edb306SCy Schubert 
16441edb306SCy Schubert 
16541edb306SCy Schubert #ifdef	notyet
16641edb306SCy Schubert /*
16741edb306SCy Schubert  * read an entire pcap packet record.  only the data part is copied into
16841edb306SCy Schubert  * the available buffer, with the number of bytes copied returned.
16941edb306SCy Schubert  */
170efeb8bffSCy Schubert static int
ipcap_read(char * buf,int cnt)171efeb8bffSCy Schubert ipcap_read(char *buf, int cnt)
17241edb306SCy Schubert {
17341edb306SCy Schubert 	packetheader_t rec;
17441edb306SCy Schubert 	static	char	*bufp = NULL;
17541edb306SCy Schubert 	int	i, n;
17641edb306SCy Schubert 
17741edb306SCy Schubert 	if ((i = ipcap_read_rec(&rec)) <= 0)
178*2582ae57SCy Schubert 		return (i);
17941edb306SCy Schubert 
18041edb306SCy Schubert 	if (!bufp)
18141edb306SCy Schubert 		bufp = malloc(i);
18241edb306SCy Schubert 	else
18341edb306SCy Schubert 		bufp = realloc(bufp, i);
18441edb306SCy Schubert 
18541edb306SCy Schubert 	if (read(pfd, bufp, i) != i)
186*2582ae57SCy Schubert 		return (-2);
18741edb306SCy Schubert 
18841edb306SCy Schubert 	n = MIN(i, cnt);
18941edb306SCy Schubert 	bcopy(bufp, buf, n);
190*2582ae57SCy Schubert 	return (n);
19141edb306SCy Schubert }
19241edb306SCy Schubert #endif
19341edb306SCy Schubert 
19441edb306SCy Schubert 
19541edb306SCy Schubert /*
19641edb306SCy Schubert * return only an IP packet read into buf
19741edb306SCy Schubert  */
198efeb8bffSCy Schubert static int
ipcap_readip(mb_t * mb,char ** ifn,int * dir)199efeb8bffSCy Schubert ipcap_readip(mb_t *mb, char **ifn, int *dir)
20041edb306SCy Schubert {
20141edb306SCy Schubert 	static	char	*bufp = NULL;
20241edb306SCy Schubert 	packetheader_t	rec;
20341edb306SCy Schubert 	struct	llc	*l;
20441edb306SCy Schubert 	char	*s, ty[4];
20541edb306SCy Schubert 	int	i, j, n;
20641edb306SCy Schubert 	char	*buf;
20741edb306SCy Schubert 	int	cnt;
20841edb306SCy Schubert 
20941edb306SCy Schubert #if 0
21041edb306SCy Schubert 	ifn = ifn;	/* gcc -Wextra */
21141edb306SCy Schubert 	dir = dir;	/* gcc -Wextra */
21241edb306SCy Schubert #endif
21341edb306SCy Schubert 	buf = (char *)mb->mb_buf;
21441edb306SCy Schubert 	cnt = sizeof(mb->mb_buf);
21541edb306SCy Schubert 	l = llcp;
21641edb306SCy Schubert 
21741edb306SCy Schubert 	/* do { */
21841edb306SCy Schubert 		if ((i = ipcap_read_rec(&rec)) <= 0)
219*2582ae57SCy Schubert 			return (i);
22041edb306SCy Schubert 
22141edb306SCy Schubert 		if (!bufp)
22241edb306SCy Schubert 			bufp = malloc(i);
22341edb306SCy Schubert 		else
22441edb306SCy Schubert 			bufp = realloc(bufp, i);
22541edb306SCy Schubert 		s = bufp;
22641edb306SCy Schubert 
22741edb306SCy Schubert 		for (j = i, n = 0; j > 0; ) {
22841edb306SCy Schubert 			n = read(pfd, s, j);
22941edb306SCy Schubert 			if (n <= 0)
230*2582ae57SCy Schubert 				return (-2);
23141edb306SCy Schubert 			j -= n;
23241edb306SCy Schubert 			s += n;
23341edb306SCy Schubert 		}
23441edb306SCy Schubert 		s = bufp;
23541edb306SCy Schubert 
23641edb306SCy Schubert 		i -= l->lc_sz;
23741edb306SCy Schubert 		s += l->lc_to;
23841edb306SCy Schubert 		bcopy(s, ty, l->lc_tl);
23941edb306SCy Schubert 		s += l->lc_tl;
24041edb306SCy Schubert 	/* } while (ty[0] != 0x8 && ty[1] != 0); */
24141edb306SCy Schubert 	n = MIN(i, cnt);
24241edb306SCy Schubert 	bcopy(s, buf, n);
24341edb306SCy Schubert 	mb->mb_len = n;
244*2582ae57SCy Schubert 	return (n);
24541edb306SCy Schubert }
246