1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  * $Id: ipft_td.c,v 1.11 2003/06/03 16:01:01 darrenr Exp $
7*0Sstevel@tonic-gate  */
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate tcpdump -n
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate 00:05:47.816843 128.231.76.76.3291 > 224.2.252.231.36573: udp 36 (encap)
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate tcpdump -nq
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate 00:33:48.410771 192.73.213.11.1463 > 224.2.248.153.59360: udp 31 (encap)
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate tcpdump -nqt
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
21*0Sstevel@tonic-gate 
22*0Sstevel@tonic-gate tcpdump -nqtt
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate 123456789.1234567 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate tcpdump -nqte
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate 8:0:20:f:65:f7 0:0:c:1:8a:c5 81: 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include "ipf.h"
33*0Sstevel@tonic-gate #include "ipt.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #undef	ICMP_MAXTYPE
36*0Sstevel@tonic-gate #include <netinet/ip_icmp.h>
37*0Sstevel@tonic-gate #include <netinet/ip_var.h>
38*0Sstevel@tonic-gate #include <netinet/udp.h>
39*0Sstevel@tonic-gate #include <netinet/tcpip.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #if !defined(lint)
43*0Sstevel@tonic-gate static const char sccsid[] = "@(#)ipft_td.c	1.8 2/4/96 (C)1995 Darren Reed";
44*0Sstevel@tonic-gate static const char rcsid[] = "@(#)$Id: ipft_td.c,v 1.11 2003/06/03 16:01:01 darrenr Exp $";
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static	int	tcpd_open __P((char *));
48*0Sstevel@tonic-gate static	int	tcpd_close __P((void));
49*0Sstevel@tonic-gate static	int	tcpd_readip __P((char *, int, char **, int *));
50*0Sstevel@tonic-gate static	int	count_dots __P((char *));
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate struct	ipread	tcpd = { tcpd_open, tcpd_close, tcpd_readip, 0 };
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static	FILE	*tfp = NULL;
55*0Sstevel@tonic-gate static	int	tfd = -1;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static	int	tcpd_open(fname)
59*0Sstevel@tonic-gate char	*fname;
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	if (tfd != -1)
62*0Sstevel@tonic-gate 		return tfd;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if (!strcmp(fname, "-")) {
65*0Sstevel@tonic-gate 		tfd = 0;
66*0Sstevel@tonic-gate 		tfp = stdin;
67*0Sstevel@tonic-gate 	} else {
68*0Sstevel@tonic-gate 		tfd = open(fname, O_RDONLY);
69*0Sstevel@tonic-gate 		tfp = fdopen(tfd, "r");
70*0Sstevel@tonic-gate 	}
71*0Sstevel@tonic-gate 	return tfd;
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static	int	tcpd_close()
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	(void) fclose(tfp);
78*0Sstevel@tonic-gate 	return close(tfd);
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static	int	count_dots(str)
83*0Sstevel@tonic-gate char	*str;
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	int	i = 0;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	while (*str)
88*0Sstevel@tonic-gate 		if (*str++ == '.')
89*0Sstevel@tonic-gate 			i++;
90*0Sstevel@tonic-gate 	return i;
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate static	int	tcpd_readip(buf, cnt, ifn, dir)
95*0Sstevel@tonic-gate char	*buf, **ifn;
96*0Sstevel@tonic-gate int	cnt, *dir;
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	struct	tcpiphdr pkt;
99*0Sstevel@tonic-gate 	ip_t	*ip = (ip_t *)&pkt;
100*0Sstevel@tonic-gate 	struct	protoent *p;
101*0Sstevel@tonic-gate 	char	src[32], dst[32], misc[256], time[32], link1[32], link2[32];
102*0Sstevel@tonic-gate 	char	lbuf[160], *s;
103*0Sstevel@tonic-gate 	int	n, slen, extra = 0;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	if (!fgets(lbuf, sizeof(lbuf) - 1, tfp))
106*0Sstevel@tonic-gate 		return 0;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	if ((s = strchr(lbuf, '\n')))
109*0Sstevel@tonic-gate 		*s = '\0';
110*0Sstevel@tonic-gate 	lbuf[sizeof(lbuf)-1] = '\0';
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	bzero(&pkt, sizeof(pkt));
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	if ((n = sscanf(lbuf, "%31s > %31s: %255s", src, dst, misc)) != 3)
115*0Sstevel@tonic-gate 		if ((n = sscanf(lbuf, "%31s %31s > %31s: %255s",
116*0Sstevel@tonic-gate 				time, src, dst, misc)) != 4)
117*0Sstevel@tonic-gate 			if ((n = sscanf(lbuf, "%31s %31s: %31s > %31s: %255s",
118*0Sstevel@tonic-gate 					link1, link2, src, dst, misc)) != 5) {
119*0Sstevel@tonic-gate 				n = sscanf(lbuf,
120*0Sstevel@tonic-gate 					   "%31s %31s %31s: %31s > %31s: %255s",
121*0Sstevel@tonic-gate 					   time, link1, link2, src, dst, misc);
122*0Sstevel@tonic-gate 				if (n != 6)
123*0Sstevel@tonic-gate 					return -1;
124*0Sstevel@tonic-gate 			}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if (count_dots(dst) == 4) {
127*0Sstevel@tonic-gate 		s = strrchr(src, '.');
128*0Sstevel@tonic-gate 		*s++ = '\0';
129*0Sstevel@tonic-gate 		(void) inet_aton(src, &ip->ip_src);
130*0Sstevel@tonic-gate 		pkt.ti_sport = htons(atoi(s));
131*0Sstevel@tonic-gate 		*--s = '.';
132*0Sstevel@tonic-gate 		s = strrchr(dst, '.');
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		*s++ = '\0';
135*0Sstevel@tonic-gate 		(void) inet_aton(src, &ip->ip_dst);
136*0Sstevel@tonic-gate 		pkt.ti_dport = htons(atoi(s));
137*0Sstevel@tonic-gate 		*--s = '.';
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	} else {
140*0Sstevel@tonic-gate 		(void) inet_aton(src, &ip->ip_src);
141*0Sstevel@tonic-gate 		(void) inet_aton(src, &ip->ip_dst);
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 	ip->ip_len = sizeof(ip_t);
144*0Sstevel@tonic-gate 	IP_HL_A(ip, sizeof(ip_t));
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	s = strtok(misc, " :");
147*0Sstevel@tonic-gate 	ip->ip_p = getproto(s);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	switch (ip->ip_p)
150*0Sstevel@tonic-gate 	{
151*0Sstevel@tonic-gate 	case IPPROTO_TCP :
152*0Sstevel@tonic-gate 	case IPPROTO_UDP :
153*0Sstevel@tonic-gate 		s = strtok(NULL, " :");
154*0Sstevel@tonic-gate 		ip->ip_len += atoi(s);
155*0Sstevel@tonic-gate 		if (ip->ip_p == IPPROTO_TCP)
156*0Sstevel@tonic-gate 			extra = sizeof(struct tcphdr);
157*0Sstevel@tonic-gate 		else if (ip->ip_p == IPPROTO_UDP)
158*0Sstevel@tonic-gate 			extra = sizeof(struct udphdr);
159*0Sstevel@tonic-gate 		break;
160*0Sstevel@tonic-gate #ifdef	IGMP
161*0Sstevel@tonic-gate 	case IPPROTO_IGMP :
162*0Sstevel@tonic-gate 		extra = sizeof(struct igmp);
163*0Sstevel@tonic-gate 		break;
164*0Sstevel@tonic-gate #endif
165*0Sstevel@tonic-gate 	case IPPROTO_ICMP :
166*0Sstevel@tonic-gate 		extra = sizeof(struct icmp);
167*0Sstevel@tonic-gate 		break;
168*0Sstevel@tonic-gate 	default :
169*0Sstevel@tonic-gate 		break;
170*0Sstevel@tonic-gate 	}
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	slen = IP_HL(ip) + extra + ip->ip_len;
173*0Sstevel@tonic-gate 	return slen;
174*0Sstevel@tonic-gate }
175