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_ef.c,v 1.11 2003/06/02 12:22:49 darrenr Exp $
7*0Sstevel@tonic-gate  */
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate                                             icmp type
11*0Sstevel@tonic-gate  lnth proto         source     destination   src port   dst port
12*0Sstevel@tonic-gate 
13*0Sstevel@tonic-gate etherfind -n
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate    60  tcp   128.250.20.20  128.250.133.13       2419     telnet
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate etherfind -n -t
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate  0.32    91   04    131.170.1.10  128.250.133.13
20*0Sstevel@tonic-gate  0.33   566  udp  128.250.37.155   128.250.133.3        901        901
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #include "ipf.h"
24*0Sstevel@tonic-gate #include "ipt.h"
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #undef	ICMP_MAXTYPE
27*0Sstevel@tonic-gate #include <netinet/ip_icmp.h>
28*0Sstevel@tonic-gate #include <netinet/ip_var.h>
29*0Sstevel@tonic-gate #include <netinet/udp.h>
30*0Sstevel@tonic-gate #include <netinet/tcpip.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #if !defined(lint)
34*0Sstevel@tonic-gate static const char sccsid[] = "@(#)ipft_ef.c	1.6 2/4/96 (C)1995 Darren Reed";
35*0Sstevel@tonic-gate static const char rcsid[] = "@(#)$Id: ipft_ef.c,v 1.11 2003/06/02 12:22:49 darrenr Exp $";
36*0Sstevel@tonic-gate #endif
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static	int	etherf_open __P((char *));
39*0Sstevel@tonic-gate static	int	etherf_close __P((void));
40*0Sstevel@tonic-gate static	int	etherf_readip __P((char *, int, char **, int *));
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate struct	ipread	etherf = { etherf_open, etherf_close, etherf_readip, 0 };
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static	FILE	*efp = NULL;
45*0Sstevel@tonic-gate static	int	efd = -1;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static	int	etherf_open(fname)
49*0Sstevel@tonic-gate char	*fname;
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	if (efd != -1)
52*0Sstevel@tonic-gate 		return efd;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	if (!strcmp(fname, "-")) {
55*0Sstevel@tonic-gate 		efd = 0;
56*0Sstevel@tonic-gate 		efp = stdin;
57*0Sstevel@tonic-gate 	} else {
58*0Sstevel@tonic-gate 		efd = open(fname, O_RDONLY);
59*0Sstevel@tonic-gate 		efp = fdopen(efd, "r");
60*0Sstevel@tonic-gate 	}
61*0Sstevel@tonic-gate 	return efd;
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static	int	etherf_close()
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	return close(efd);
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static	int	etherf_readip(buf, cnt, ifn, dir)
72*0Sstevel@tonic-gate char	*buf, **ifn;
73*0Sstevel@tonic-gate int	cnt, *dir;
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	struct	tcpiphdr pkt;
76*0Sstevel@tonic-gate 	ip_t	*ip = (ip_t *)&pkt;
77*0Sstevel@tonic-gate 	char	src[16], dst[16], sprt[16], dprt[16];
78*0Sstevel@tonic-gate 	char	lbuf[128], len[8], prot[8], time[8], *s;
79*0Sstevel@tonic-gate 	int	slen, extra = 0, i;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (!fgets(lbuf, sizeof(lbuf) - 1, efp))
82*0Sstevel@tonic-gate 		return 0;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	if ((s = strchr(lbuf, '\n')))
85*0Sstevel@tonic-gate 		*s = '\0';
86*0Sstevel@tonic-gate 	lbuf[sizeof(lbuf)-1] = '\0';
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	bzero(&pkt, sizeof(pkt));
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	if (sscanf(lbuf, "%7s %7s %15s %15s %15s %15s", len, prot, src, dst,
91*0Sstevel@tonic-gate 		   sprt, dprt) != 6)
92*0Sstevel@tonic-gate 		if (sscanf(lbuf, "%7s %7s %7s %15s %15s %15s %15s", time,
93*0Sstevel@tonic-gate 			   len, prot, src, dst, sprt, dprt) != 7)
94*0Sstevel@tonic-gate 			return -1;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	ip->ip_p = getproto(prot);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	switch (ip->ip_p) {
99*0Sstevel@tonic-gate 	case IPPROTO_TCP :
100*0Sstevel@tonic-gate 	case IPPROTO_UDP :
101*0Sstevel@tonic-gate 		s = strtok(NULL, " :");
102*0Sstevel@tonic-gate 		ip->ip_len += atoi(s);
103*0Sstevel@tonic-gate 		if (ip->ip_p == IPPROTO_TCP)
104*0Sstevel@tonic-gate 			extra = sizeof(struct tcphdr);
105*0Sstevel@tonic-gate 		else if (ip->ip_p == IPPROTO_UDP)
106*0Sstevel@tonic-gate 			extra = sizeof(struct udphdr);
107*0Sstevel@tonic-gate 		break;
108*0Sstevel@tonic-gate #ifdef	IGMP
109*0Sstevel@tonic-gate 	case IPPROTO_IGMP :
110*0Sstevel@tonic-gate 		extra = sizeof(struct igmp);
111*0Sstevel@tonic-gate 		break;
112*0Sstevel@tonic-gate #endif
113*0Sstevel@tonic-gate 	case IPPROTO_ICMP :
114*0Sstevel@tonic-gate 		extra = sizeof(struct icmp);
115*0Sstevel@tonic-gate 		break;
116*0Sstevel@tonic-gate 	default :
117*0Sstevel@tonic-gate 		break;
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	(void) inet_aton(src, &ip->ip_src);
121*0Sstevel@tonic-gate 	(void) inet_aton(dst, &ip->ip_dst);
122*0Sstevel@tonic-gate 	ip->ip_len = atoi(len);
123*0Sstevel@tonic-gate 	IP_HL_A(ip, sizeof(ip_t));
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	slen = IP_HL(ip) + extra;
126*0Sstevel@tonic-gate 	i = MIN(cnt, slen);
127*0Sstevel@tonic-gate 	bcopy((char *)&pkt, buf, i);
128*0Sstevel@tonic-gate 	return i;
129*0Sstevel@tonic-gate }
130