17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (C) 1993-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate *
6*ab25eeb5Syz155240 * $Id: ipft_ef.c,v 1.14 2004/01/08 13:34:31 darrenr Exp $
77c478bd9Sstevel@tonic-gate */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate icmp type
117c478bd9Sstevel@tonic-gate lnth proto source destination src port dst port
127c478bd9Sstevel@tonic-gate
137c478bd9Sstevel@tonic-gate etherfind -n
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate 60 tcp 128.250.20.20 128.250.133.13 2419 telnet
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate etherfind -n -t
187c478bd9Sstevel@tonic-gate
197c478bd9Sstevel@tonic-gate 0.32 91 04 131.170.1.10 128.250.133.13
207c478bd9Sstevel@tonic-gate 0.33 566 udp 128.250.37.155 128.250.133.3 901 901
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate
237c478bd9Sstevel@tonic-gate #include "ipf.h"
247c478bd9Sstevel@tonic-gate #include "ipt.h"
257c478bd9Sstevel@tonic-gate
26*ab25eeb5Syz155240 #ifndef linux
277c478bd9Sstevel@tonic-gate #include <netinet/ip_var.h>
28*ab25eeb5Syz155240 #endif
297c478bd9Sstevel@tonic-gate #include <netinet/tcpip.h>
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #if !defined(lint)
337c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)ipft_ef.c 1.6 2/4/96 (C)1995 Darren Reed";
34*ab25eeb5Syz155240 static const char rcsid[] = "@(#)$Id: ipft_ef.c,v 1.14 2004/01/08 13:34:31 darrenr Exp $";
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate static int etherf_open __P((char *));
387c478bd9Sstevel@tonic-gate static int etherf_close __P((void));
397c478bd9Sstevel@tonic-gate static int etherf_readip __P((char *, int, char **, int *));
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate struct ipread etherf = { etherf_open, etherf_close, etherf_readip, 0 };
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static FILE *efp = NULL;
447c478bd9Sstevel@tonic-gate static int efd = -1;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate
etherf_open(fname)477c478bd9Sstevel@tonic-gate static int etherf_open(fname)
487c478bd9Sstevel@tonic-gate char *fname;
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate if (efd != -1)
517c478bd9Sstevel@tonic-gate return efd;
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate if (!strcmp(fname, "-")) {
547c478bd9Sstevel@tonic-gate efd = 0;
557c478bd9Sstevel@tonic-gate efp = stdin;
567c478bd9Sstevel@tonic-gate } else {
577c478bd9Sstevel@tonic-gate efd = open(fname, O_RDONLY);
587c478bd9Sstevel@tonic-gate efp = fdopen(efd, "r");
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate return efd;
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate
etherf_close()647c478bd9Sstevel@tonic-gate static int etherf_close()
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate return close(efd);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate
etherf_readip(buf,cnt,ifn,dir)707c478bd9Sstevel@tonic-gate static int etherf_readip(buf, cnt, ifn, dir)
717c478bd9Sstevel@tonic-gate char *buf, **ifn;
727c478bd9Sstevel@tonic-gate int cnt, *dir;
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate struct tcpiphdr pkt;
757c478bd9Sstevel@tonic-gate ip_t *ip = (ip_t *)&pkt;
767c478bd9Sstevel@tonic-gate char src[16], dst[16], sprt[16], dprt[16];
777c478bd9Sstevel@tonic-gate char lbuf[128], len[8], prot[8], time[8], *s;
787c478bd9Sstevel@tonic-gate int slen, extra = 0, i;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate if (!fgets(lbuf, sizeof(lbuf) - 1, efp))
817c478bd9Sstevel@tonic-gate return 0;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate if ((s = strchr(lbuf, '\n')))
847c478bd9Sstevel@tonic-gate *s = '\0';
857c478bd9Sstevel@tonic-gate lbuf[sizeof(lbuf)-1] = '\0';
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate bzero(&pkt, sizeof(pkt));
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate if (sscanf(lbuf, "%7s %7s %15s %15s %15s %15s", len, prot, src, dst,
907c478bd9Sstevel@tonic-gate sprt, dprt) != 6)
917c478bd9Sstevel@tonic-gate if (sscanf(lbuf, "%7s %7s %7s %15s %15s %15s %15s", time,
927c478bd9Sstevel@tonic-gate len, prot, src, dst, sprt, dprt) != 7)
937c478bd9Sstevel@tonic-gate return -1;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate ip->ip_p = getproto(prot);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate switch (ip->ip_p) {
987c478bd9Sstevel@tonic-gate case IPPROTO_TCP :
997c478bd9Sstevel@tonic-gate case IPPROTO_UDP :
1007c478bd9Sstevel@tonic-gate s = strtok(NULL, " :");
1017c478bd9Sstevel@tonic-gate ip->ip_len += atoi(s);
1027c478bd9Sstevel@tonic-gate if (ip->ip_p == IPPROTO_TCP)
1037c478bd9Sstevel@tonic-gate extra = sizeof(struct tcphdr);
1047c478bd9Sstevel@tonic-gate else if (ip->ip_p == IPPROTO_UDP)
1057c478bd9Sstevel@tonic-gate extra = sizeof(struct udphdr);
1067c478bd9Sstevel@tonic-gate break;
1077c478bd9Sstevel@tonic-gate #ifdef IGMP
1087c478bd9Sstevel@tonic-gate case IPPROTO_IGMP :
1097c478bd9Sstevel@tonic-gate extra = sizeof(struct igmp);
1107c478bd9Sstevel@tonic-gate break;
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate case IPPROTO_ICMP :
1137c478bd9Sstevel@tonic-gate extra = sizeof(struct icmp);
1147c478bd9Sstevel@tonic-gate break;
1157c478bd9Sstevel@tonic-gate default :
1167c478bd9Sstevel@tonic-gate break;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate (void) inet_aton(src, &ip->ip_src);
1207c478bd9Sstevel@tonic-gate (void) inet_aton(dst, &ip->ip_dst);
1217c478bd9Sstevel@tonic-gate ip->ip_len = atoi(len);
1227c478bd9Sstevel@tonic-gate IP_HL_A(ip, sizeof(ip_t));
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate slen = IP_HL(ip) + extra;
1257c478bd9Sstevel@tonic-gate i = MIN(cnt, slen);
1267c478bd9Sstevel@tonic-gate bcopy((char *)&pkt, buf, i);
1277c478bd9Sstevel@tonic-gate return i;
1287c478bd9Sstevel@tonic-gate }
129