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_td.c,v 1.15 2004/01/08 13:34:31 darrenr Exp $
70Sstevel@tonic-gate */
80Sstevel@tonic-gate
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate tcpdump -n
110Sstevel@tonic-gate
120Sstevel@tonic-gate 00:05:47.816843 128.231.76.76.3291 > 224.2.252.231.36573: udp 36 (encap)
130Sstevel@tonic-gate
140Sstevel@tonic-gate tcpdump -nq
150Sstevel@tonic-gate
160Sstevel@tonic-gate 00:33:48.410771 192.73.213.11.1463 > 224.2.248.153.59360: udp 31 (encap)
170Sstevel@tonic-gate
180Sstevel@tonic-gate tcpdump -nqt
190Sstevel@tonic-gate
200Sstevel@tonic-gate 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
210Sstevel@tonic-gate
220Sstevel@tonic-gate tcpdump -nqtt
230Sstevel@tonic-gate
240Sstevel@tonic-gate 123456789.1234567 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
250Sstevel@tonic-gate
260Sstevel@tonic-gate tcpdump -nqte
270Sstevel@tonic-gate
280Sstevel@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
290Sstevel@tonic-gate
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include "ipf.h"
330Sstevel@tonic-gate #include "ipt.h"
340Sstevel@tonic-gate
35*2393Syz155240 #ifndef linux
360Sstevel@tonic-gate #include <netinet/ip_var.h>
37*2393Syz155240 #endif
380Sstevel@tonic-gate #include <netinet/tcpip.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate
410Sstevel@tonic-gate #if !defined(lint)
420Sstevel@tonic-gate static const char sccsid[] = "@(#)ipft_td.c 1.8 2/4/96 (C)1995 Darren Reed";
43*2393Syz155240 static const char rcsid[] = "@(#)$Id: ipft_td.c,v 1.15 2004/01/08 13:34:31 darrenr Exp $";
440Sstevel@tonic-gate #endif
450Sstevel@tonic-gate
460Sstevel@tonic-gate static int tcpd_open __P((char *));
470Sstevel@tonic-gate static int tcpd_close __P((void));
480Sstevel@tonic-gate static int tcpd_readip __P((char *, int, char **, int *));
490Sstevel@tonic-gate static int count_dots __P((char *));
500Sstevel@tonic-gate
510Sstevel@tonic-gate struct ipread tcpd = { tcpd_open, tcpd_close, tcpd_readip, 0 };
520Sstevel@tonic-gate
530Sstevel@tonic-gate static FILE *tfp = NULL;
540Sstevel@tonic-gate static int tfd = -1;
550Sstevel@tonic-gate
560Sstevel@tonic-gate
tcpd_open(fname)570Sstevel@tonic-gate static int tcpd_open(fname)
580Sstevel@tonic-gate char *fname;
590Sstevel@tonic-gate {
600Sstevel@tonic-gate if (tfd != -1)
610Sstevel@tonic-gate return tfd;
620Sstevel@tonic-gate
630Sstevel@tonic-gate if (!strcmp(fname, "-")) {
640Sstevel@tonic-gate tfd = 0;
650Sstevel@tonic-gate tfp = stdin;
660Sstevel@tonic-gate } else {
670Sstevel@tonic-gate tfd = open(fname, O_RDONLY);
680Sstevel@tonic-gate tfp = fdopen(tfd, "r");
690Sstevel@tonic-gate }
700Sstevel@tonic-gate return tfd;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate
tcpd_close()740Sstevel@tonic-gate static int tcpd_close()
750Sstevel@tonic-gate {
760Sstevel@tonic-gate (void) fclose(tfp);
770Sstevel@tonic-gate return close(tfd);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate
count_dots(str)810Sstevel@tonic-gate static int count_dots(str)
820Sstevel@tonic-gate char *str;
830Sstevel@tonic-gate {
840Sstevel@tonic-gate int i = 0;
850Sstevel@tonic-gate
860Sstevel@tonic-gate while (*str)
870Sstevel@tonic-gate if (*str++ == '.')
880Sstevel@tonic-gate i++;
890Sstevel@tonic-gate return i;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate
tcpd_readip(buf,cnt,ifn,dir)930Sstevel@tonic-gate static int tcpd_readip(buf, cnt, ifn, dir)
940Sstevel@tonic-gate char *buf, **ifn;
950Sstevel@tonic-gate int cnt, *dir;
960Sstevel@tonic-gate {
970Sstevel@tonic-gate struct tcpiphdr pkt;
980Sstevel@tonic-gate ip_t *ip = (ip_t *)&pkt;
990Sstevel@tonic-gate char src[32], dst[32], misc[256], time[32], link1[32], link2[32];
1000Sstevel@tonic-gate char lbuf[160], *s;
1010Sstevel@tonic-gate int n, slen, extra = 0;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate if (!fgets(lbuf, sizeof(lbuf) - 1, tfp))
1040Sstevel@tonic-gate return 0;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate if ((s = strchr(lbuf, '\n')))
1070Sstevel@tonic-gate *s = '\0';
1080Sstevel@tonic-gate lbuf[sizeof(lbuf)-1] = '\0';
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate bzero(&pkt, sizeof(pkt));
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate if ((n = sscanf(lbuf, "%31s > %31s: %255s", src, dst, misc)) != 3)
1130Sstevel@tonic-gate if ((n = sscanf(lbuf, "%31s %31s > %31s: %255s",
1140Sstevel@tonic-gate time, src, dst, misc)) != 4)
1150Sstevel@tonic-gate if ((n = sscanf(lbuf, "%31s %31s: %31s > %31s: %255s",
1160Sstevel@tonic-gate link1, link2, src, dst, misc)) != 5) {
1170Sstevel@tonic-gate n = sscanf(lbuf,
1180Sstevel@tonic-gate "%31s %31s %31s: %31s > %31s: %255s",
1190Sstevel@tonic-gate time, link1, link2, src, dst, misc);
1200Sstevel@tonic-gate if (n != 6)
1210Sstevel@tonic-gate return -1;
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (count_dots(dst) == 4) {
1250Sstevel@tonic-gate s = strrchr(src, '.');
1260Sstevel@tonic-gate *s++ = '\0';
1270Sstevel@tonic-gate (void) inet_aton(src, &ip->ip_src);
1280Sstevel@tonic-gate pkt.ti_sport = htons(atoi(s));
1290Sstevel@tonic-gate *--s = '.';
1300Sstevel@tonic-gate s = strrchr(dst, '.');
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate *s++ = '\0';
1330Sstevel@tonic-gate (void) inet_aton(src, &ip->ip_dst);
1340Sstevel@tonic-gate pkt.ti_dport = htons(atoi(s));
1350Sstevel@tonic-gate *--s = '.';
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate } else {
1380Sstevel@tonic-gate (void) inet_aton(src, &ip->ip_src);
1390Sstevel@tonic-gate (void) inet_aton(src, &ip->ip_dst);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate ip->ip_len = sizeof(ip_t);
1420Sstevel@tonic-gate IP_HL_A(ip, sizeof(ip_t));
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate s = strtok(misc, " :");
1450Sstevel@tonic-gate ip->ip_p = getproto(s);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate switch (ip->ip_p)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate case IPPROTO_TCP :
1500Sstevel@tonic-gate case IPPROTO_UDP :
1510Sstevel@tonic-gate s = strtok(NULL, " :");
1520Sstevel@tonic-gate ip->ip_len += atoi(s);
1530Sstevel@tonic-gate if (ip->ip_p == IPPROTO_TCP)
1540Sstevel@tonic-gate extra = sizeof(struct tcphdr);
1550Sstevel@tonic-gate else if (ip->ip_p == IPPROTO_UDP)
1560Sstevel@tonic-gate extra = sizeof(struct udphdr);
1570Sstevel@tonic-gate break;
1580Sstevel@tonic-gate #ifdef IGMP
1590Sstevel@tonic-gate case IPPROTO_IGMP :
1600Sstevel@tonic-gate extra = sizeof(struct igmp);
1610Sstevel@tonic-gate break;
1620Sstevel@tonic-gate #endif
1630Sstevel@tonic-gate case IPPROTO_ICMP :
1640Sstevel@tonic-gate extra = sizeof(struct icmp);
1650Sstevel@tonic-gate break;
1660Sstevel@tonic-gate default :
1670Sstevel@tonic-gate break;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate slen = IP_HL(ip) + extra + ip->ip_len;
1710Sstevel@tonic-gate return slen;
1720Sstevel@tonic-gate }
173