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