xref: /onnv-gate/usr/src/cmd/ipf/lib/common/ipft_ef.c (revision 2393:76e0289ce525)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
6*2393Syz155240  * $Id: ipft_ef.c,v 1.14 2004/01/08 13:34:31 darrenr Exp $
70Sstevel@tonic-gate  */
80Sstevel@tonic-gate 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate                                             icmp type
110Sstevel@tonic-gate  lnth proto         source     destination   src port   dst port
120Sstevel@tonic-gate 
130Sstevel@tonic-gate etherfind -n
140Sstevel@tonic-gate 
150Sstevel@tonic-gate    60  tcp   128.250.20.20  128.250.133.13       2419     telnet
160Sstevel@tonic-gate 
170Sstevel@tonic-gate etherfind -n -t
180Sstevel@tonic-gate 
190Sstevel@tonic-gate  0.32    91   04    131.170.1.10  128.250.133.13
200Sstevel@tonic-gate  0.33   566  udp  128.250.37.155   128.250.133.3        901        901
210Sstevel@tonic-gate */
220Sstevel@tonic-gate 
230Sstevel@tonic-gate #include "ipf.h"
240Sstevel@tonic-gate #include "ipt.h"
250Sstevel@tonic-gate 
26*2393Syz155240 #ifndef linux
270Sstevel@tonic-gate #include <netinet/ip_var.h>
28*2393Syz155240 #endif
290Sstevel@tonic-gate #include <netinet/tcpip.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #if !defined(lint)
330Sstevel@tonic-gate static const char sccsid[] = "@(#)ipft_ef.c	1.6 2/4/96 (C)1995 Darren Reed";
34*2393Syz155240 static const char rcsid[] = "@(#)$Id: ipft_ef.c,v 1.14 2004/01/08 13:34:31 darrenr Exp $";
350Sstevel@tonic-gate #endif
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static	int	etherf_open __P((char *));
380Sstevel@tonic-gate static	int	etherf_close __P((void));
390Sstevel@tonic-gate static	int	etherf_readip __P((char *, int, char **, int *));
400Sstevel@tonic-gate 
410Sstevel@tonic-gate struct	ipread	etherf = { etherf_open, etherf_close, etherf_readip, 0 };
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static	FILE	*efp = NULL;
440Sstevel@tonic-gate static	int	efd = -1;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
etherf_open(fname)470Sstevel@tonic-gate static	int	etherf_open(fname)
480Sstevel@tonic-gate char	*fname;
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	if (efd != -1)
510Sstevel@tonic-gate 		return efd;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if (!strcmp(fname, "-")) {
540Sstevel@tonic-gate 		efd = 0;
550Sstevel@tonic-gate 		efp = stdin;
560Sstevel@tonic-gate 	} else {
570Sstevel@tonic-gate 		efd = open(fname, O_RDONLY);
580Sstevel@tonic-gate 		efp = fdopen(efd, "r");
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 	return efd;
610Sstevel@tonic-gate }
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
etherf_close()640Sstevel@tonic-gate static	int	etherf_close()
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	return close(efd);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 
etherf_readip(buf,cnt,ifn,dir)700Sstevel@tonic-gate static	int	etherf_readip(buf, cnt, ifn, dir)
710Sstevel@tonic-gate char	*buf, **ifn;
720Sstevel@tonic-gate int	cnt, *dir;
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	struct	tcpiphdr pkt;
750Sstevel@tonic-gate 	ip_t	*ip = (ip_t *)&pkt;
760Sstevel@tonic-gate 	char	src[16], dst[16], sprt[16], dprt[16];
770Sstevel@tonic-gate 	char	lbuf[128], len[8], prot[8], time[8], *s;
780Sstevel@tonic-gate 	int	slen, extra = 0, i;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (!fgets(lbuf, sizeof(lbuf) - 1, efp))
810Sstevel@tonic-gate 		return 0;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if ((s = strchr(lbuf, '\n')))
840Sstevel@tonic-gate 		*s = '\0';
850Sstevel@tonic-gate 	lbuf[sizeof(lbuf)-1] = '\0';
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	bzero(&pkt, sizeof(pkt));
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (sscanf(lbuf, "%7s %7s %15s %15s %15s %15s", len, prot, src, dst,
900Sstevel@tonic-gate 		   sprt, dprt) != 6)
910Sstevel@tonic-gate 		if (sscanf(lbuf, "%7s %7s %7s %15s %15s %15s %15s", time,
920Sstevel@tonic-gate 			   len, prot, src, dst, sprt, dprt) != 7)
930Sstevel@tonic-gate 			return -1;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	ip->ip_p = getproto(prot);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	switch (ip->ip_p) {
980Sstevel@tonic-gate 	case IPPROTO_TCP :
990Sstevel@tonic-gate 	case IPPROTO_UDP :
1000Sstevel@tonic-gate 		s = strtok(NULL, " :");
1010Sstevel@tonic-gate 		ip->ip_len += atoi(s);
1020Sstevel@tonic-gate 		if (ip->ip_p == IPPROTO_TCP)
1030Sstevel@tonic-gate 			extra = sizeof(struct tcphdr);
1040Sstevel@tonic-gate 		else if (ip->ip_p == IPPROTO_UDP)
1050Sstevel@tonic-gate 			extra = sizeof(struct udphdr);
1060Sstevel@tonic-gate 		break;
1070Sstevel@tonic-gate #ifdef	IGMP
1080Sstevel@tonic-gate 	case IPPROTO_IGMP :
1090Sstevel@tonic-gate 		extra = sizeof(struct igmp);
1100Sstevel@tonic-gate 		break;
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 	case IPPROTO_ICMP :
1130Sstevel@tonic-gate 		extra = sizeof(struct icmp);
1140Sstevel@tonic-gate 		break;
1150Sstevel@tonic-gate 	default :
1160Sstevel@tonic-gate 		break;
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	(void) inet_aton(src, &ip->ip_src);
1200Sstevel@tonic-gate 	(void) inet_aton(dst, &ip->ip_dst);
1210Sstevel@tonic-gate 	ip->ip_len = atoi(len);
1220Sstevel@tonic-gate 	IP_HL_A(ip, sizeof(ip_t));
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	slen = IP_HL(ip) + extra;
1250Sstevel@tonic-gate 	i = MIN(cnt, slen);
1260Sstevel@tonic-gate 	bcopy((char *)&pkt, buf, i);
1270Sstevel@tonic-gate 	return i;
1280Sstevel@tonic-gate }
129