1997-02-09 22:50:16 +00:00
|
|
|
/*
|
2002-03-19 11:48:16 +00:00
|
|
|
* Copyright (C) 1993-2002 by Darren Reed.
|
1997-02-09 22:50:16 +00:00
|
|
|
*
|
2001-07-28 12:08:15 +00:00
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
#ifndef SOLARIS
|
|
|
|
#define SOLARIS (defined(__SVR4) || defined(__svr4__)) && defined(sun)
|
|
|
|
#endif
|
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
#ifdef __sgi
|
|
|
|
# include <sys/ptimers.h>
|
|
|
|
#endif
|
2000-02-10 03:17:51 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#if !defined(__SVR4) && !defined(__svr4__)
|
2000-02-10 03:17:51 +00:00
|
|
|
# if (__FreeBSD_version >= 300000)
|
|
|
|
# include <sys/dirent.h>
|
|
|
|
# else
|
|
|
|
# include <sys/dir.h>
|
|
|
|
# endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#else
|
2000-02-10 03:17:51 +00:00
|
|
|
# include <sys/filio.h>
|
|
|
|
# include <sys/byteorder.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
2001-07-28 12:08:15 +00:00
|
|
|
#if !defined(__SVR4) && !defined(__GNUC__)
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2000-02-10 03:17:51 +00:00
|
|
|
#include <signal.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include <net/if.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netinet/ip.h>
|
2000-02-10 03:17:51 +00:00
|
|
|
#include <netinet/tcp_fsm.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#ifndef linux
|
|
|
|
# include <sys/protosw.h>
|
|
|
|
# include <netinet/ip_var.h>
|
|
|
|
#endif
|
1997-04-03 10:22:02 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <ctype.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#include "netinet/ip_compat.h"
|
|
|
|
#include <netinet/tcpip.h>
|
|
|
|
#include "netinet/ip_fil.h"
|
|
|
|
#include "netinet/ip_nat.h"
|
|
|
|
#include "netinet/ip_state.h"
|
|
|
|
|
2001-07-28 12:08:15 +00:00
|
|
|
#if !defined(lint)
|
|
|
|
static const char sccsid[] = "@(#)ipmon.c 1.21 6/5/96 (C)1993-2000 Darren Reed";
|
|
|
|
/* static const char rcsid[] = "@(#)$Id: ipmon.c,v 2.12.2.13 2001/07/19 12:24:59 darrenr Exp $"; */
|
|
|
|
static const char rcsid[] = "@(#)$FreeBSD$";
|
|
|
|
#endif
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#if defined(sun) && !defined(SOLARIS2)
|
|
|
|
#define STRERROR(x) sys_errlist[x]
|
|
|
|
extern char *sys_errlist[];
|
|
|
|
#else
|
|
|
|
#define STRERROR(x) strerror(x)
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
struct flags {
|
|
|
|
int value;
|
|
|
|
char flag;
|
|
|
|
};
|
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
|
|
|
|
typedef struct icmp_subtype {
|
|
|
|
int ist_val;
|
|
|
|
char *ist_name;
|
|
|
|
} icmp_subtype_t;
|
|
|
|
|
|
|
|
typedef struct icmp_type {
|
|
|
|
int it_val;
|
|
|
|
struct icmp_subtype *it_subtable;
|
|
|
|
size_t it_stsize;
|
|
|
|
char *it_name;
|
|
|
|
} icmp_type_t;
|
|
|
|
|
|
|
|
|
|
|
|
#define IST_SZ(x) (sizeof(x)/sizeof(icmp_subtype_t))
|
|
|
|
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
struct flags tcpfl[] = {
|
|
|
|
{ TH_ACK, 'A' },
|
|
|
|
{ TH_RST, 'R' },
|
|
|
|
{ TH_SYN, 'S' },
|
|
|
|
{ TH_FIN, 'F' },
|
|
|
|
{ TH_URG, 'U' },
|
|
|
|
{ TH_PUSH,'P' },
|
2001-02-04 14:26:56 +00:00
|
|
|
{ TH_ECN, 'E' },
|
|
|
|
{ TH_CWR, 'C' },
|
1997-02-09 22:50:16 +00:00
|
|
|
{ 0, '\0' }
|
|
|
|
};
|
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
#if SOLARIS
|
|
|
|
static char *pidfile = "/etc/opt/ipf/ipmon.pid";
|
|
|
|
#else
|
|
|
|
# if BSD >= 199306
|
|
|
|
static char *pidfile = "/var/run/ipmon.pid";
|
|
|
|
# else
|
|
|
|
static char *pidfile = "/etc/ipmon.pid";
|
|
|
|
# endif
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
static char line[2048];
|
|
|
|
static int opts = 0;
|
1998-06-20 18:43:25 +00:00
|
|
|
static FILE *newlog = NULL;
|
|
|
|
static char *logfile = NULL;
|
|
|
|
static int donehup = 0;
|
1997-04-03 10:22:02 +00:00
|
|
|
static void usage __P((char *));
|
1998-10-17 14:47:00 +00:00
|
|
|
static void handlehup __P((int));
|
1997-11-16 04:52:19 +00:00
|
|
|
static void flushlogs __P((char *, FILE *));
|
|
|
|
static void print_log __P((int, FILE *, char *, int));
|
1997-04-03 10:22:02 +00:00
|
|
|
static void print_ipflog __P((FILE *, char *, int));
|
|
|
|
static void print_natlog __P((FILE *, char *, int));
|
|
|
|
static void print_statelog __P((FILE *, char *, int));
|
|
|
|
static void dumphex __P((FILE *, u_char *, int));
|
2000-02-10 03:17:51 +00:00
|
|
|
static int read_log __P((int, int *, char *, int));
|
|
|
|
static void write_pid __P((char *));
|
2001-07-28 12:08:15 +00:00
|
|
|
static char *icmpname __P((u_int, u_int));
|
2002-03-19 11:48:16 +00:00
|
|
|
static char *icmpname6 __P((u_int, u_int));
|
|
|
|
static icmp_type_t *find_icmptype __P((int, icmp_type_t *, size_t));
|
|
|
|
static icmp_subtype_t *find_icmpsubtype __P((int, icmp_subtype_t *, size_t));
|
2000-02-10 03:17:51 +00:00
|
|
|
|
2000-05-24 02:19:15 +00:00
|
|
|
char *hostname __P((int, int, u_32_t *));
|
2000-02-10 03:17:51 +00:00
|
|
|
char *portname __P((int, char *, u_int));
|
1997-04-03 10:22:02 +00:00
|
|
|
int main __P((int, char *[]));
|
|
|
|
|
1998-06-20 18:43:25 +00:00
|
|
|
static void logopts __P((int, char *));
|
2000-02-10 03:17:51 +00:00
|
|
|
static void init_tabs __P((void));
|
|
|
|
static char *getproto __P((u_int));
|
|
|
|
|
|
|
|
static char **protocols = NULL;
|
|
|
|
static char **udp_ports = NULL;
|
|
|
|
static char **tcp_ports = NULL;
|
1997-04-03 10:22:02 +00:00
|
|
|
|
|
|
|
#define OPT_SYSLOG 0x001
|
|
|
|
#define OPT_RESOLVE 0x002
|
|
|
|
#define OPT_HEXBODY 0x004
|
|
|
|
#define OPT_VERBOSE 0x008
|
|
|
|
#define OPT_HEXHDR 0x010
|
|
|
|
#define OPT_TAIL 0x020
|
|
|
|
#define OPT_NAT 0x080
|
|
|
|
#define OPT_STATE 0x100
|
1998-03-21 10:04:55 +00:00
|
|
|
#define OPT_FILTER 0x200
|
|
|
|
#define OPT_PORTNUM 0x400
|
2000-02-10 03:17:51 +00:00
|
|
|
#define OPT_LOGALL (OPT_NAT|OPT_STATE|OPT_FILTER)
|
2001-02-04 14:26:56 +00:00
|
|
|
#define OPT_LOGBODY 0x800
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2000-05-24 02:19:15 +00:00
|
|
|
#define HOSTNAME_V4(a,b) hostname((a), 4, (u_32_t *)&(b))
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
#ifndef LOGFAC
|
|
|
|
#define LOGFAC LOG_LOCAL0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
static icmp_subtype_t icmpunreachnames[] = {
|
|
|
|
{ ICMP_UNREACH_NET, "net" },
|
|
|
|
{ ICMP_UNREACH_HOST, "host" },
|
|
|
|
{ ICMP_UNREACH_PROTOCOL, "protocol" },
|
|
|
|
{ ICMP_UNREACH_PORT, "port" },
|
|
|
|
{ ICMP_UNREACH_NEEDFRAG, "needfrag" },
|
|
|
|
{ ICMP_UNREACH_SRCFAIL, "srcfail" },
|
|
|
|
{ ICMP_UNREACH_NET_UNKNOWN, "net_unknown" },
|
|
|
|
{ ICMP_UNREACH_HOST_UNKNOWN, "host_unknown" },
|
|
|
|
{ ICMP_UNREACH_NET, "isolated" },
|
|
|
|
{ ICMP_UNREACH_NET_PROHIB, "net_prohib" },
|
|
|
|
{ ICMP_UNREACH_NET_PROHIB, "host_prohib" },
|
|
|
|
{ ICMP_UNREACH_TOSNET, "tosnet" },
|
|
|
|
{ ICMP_UNREACH_TOSHOST, "toshost" },
|
|
|
|
{ ICMP_UNREACH_ADMIN_PROHIBIT, "admin_prohibit" },
|
|
|
|
{ -2, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_subtype_t redirectnames[] = {
|
|
|
|
{ ICMP_REDIRECT_NET, "net" },
|
|
|
|
{ ICMP_REDIRECT_HOST, "host" },
|
|
|
|
{ ICMP_REDIRECT_TOSNET, "tosnet" },
|
|
|
|
{ ICMP_REDIRECT_TOSHOST, "toshost" },
|
|
|
|
{ -2, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_subtype_t timxceednames[] = {
|
|
|
|
{ ICMP_TIMXCEED_INTRANS, "transit" },
|
|
|
|
{ ICMP_TIMXCEED_REASS, "reassem" },
|
|
|
|
{ -2, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_subtype_t paramnames[] = {
|
|
|
|
{ ICMP_PARAMPROB_ERRATPTR, "errata_pointer" },
|
|
|
|
{ ICMP_PARAMPROB_OPTABSENT, "optmissing" },
|
|
|
|
{ ICMP_PARAMPROB_LENGTH, "length" },
|
|
|
|
{ -2, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_type_t icmptypes[] = {
|
|
|
|
{ ICMP_ECHOREPLY, NULL, 0, "echoreply" },
|
|
|
|
{ -1, NULL, 0, NULL },
|
|
|
|
{ -1, NULL, 0, NULL },
|
|
|
|
{ ICMP_UNREACH, icmpunreachnames,
|
|
|
|
IST_SZ(icmpunreachnames),"unreach" },
|
|
|
|
{ ICMP_SOURCEQUENCH, NULL, 0, "sourcequench" },
|
|
|
|
{ ICMP_REDIRECT, redirectnames,
|
|
|
|
IST_SZ(redirectnames), "redirect" },
|
|
|
|
{ -1, NULL, 0, NULL },
|
|
|
|
{ -1, NULL, 0, NULL },
|
|
|
|
{ ICMP_ECHO, NULL, 0, "echo" },
|
|
|
|
{ ICMP_ROUTERADVERT, NULL, 0, "routeradvert" },
|
|
|
|
{ ICMP_ROUTERSOLICIT, NULL, 0, "routersolicit" },
|
|
|
|
{ ICMP_TIMXCEED, timxceednames,
|
|
|
|
IST_SZ(timxceednames), "timxceed" },
|
|
|
|
{ ICMP_PARAMPROB, paramnames,
|
|
|
|
IST_SZ(paramnames), "paramprob" },
|
|
|
|
{ ICMP_TSTAMP, NULL, 0, "timestamp" },
|
|
|
|
{ ICMP_TSTAMPREPLY, NULL, 0, "timestampreply" },
|
|
|
|
{ ICMP_IREQ, NULL, 0, "inforeq" },
|
|
|
|
{ ICMP_IREQREPLY, NULL, 0, "inforeply" },
|
|
|
|
{ ICMP_MASKREQ, NULL, 0, "maskreq" },
|
|
|
|
{ ICMP_MASKREPLY, NULL, 0, "maskreply" },
|
|
|
|
{ -2, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_subtype_t icmpredirect6[] = {
|
|
|
|
{ ICMP6_DST_UNREACH_NOROUTE, "noroute" },
|
|
|
|
{ ICMP6_DST_UNREACH_ADMIN, "admin" },
|
|
|
|
{ ICMP6_DST_UNREACH_NOTNEIGHBOR, "neighbour" },
|
|
|
|
{ ICMP6_DST_UNREACH_ADDR, "address" },
|
|
|
|
{ ICMP6_DST_UNREACH_NOPORT, "noport" },
|
|
|
|
{ -2, NULL }
|
2001-07-28 12:08:15 +00:00
|
|
|
};
|
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
static icmp_subtype_t icmptimexceed6[] = {
|
|
|
|
{ ICMP6_TIME_EXCEED_TRANSIT, "intransit" },
|
|
|
|
{ ICMP6_TIME_EXCEED_REASSEMBLY, "reassem" },
|
|
|
|
{ -2, NULL }
|
2001-07-28 12:08:15 +00:00
|
|
|
};
|
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
static icmp_subtype_t icmpparamprob6[] = {
|
|
|
|
{ ICMP6_PARAMPROB_HEADER, "header" },
|
|
|
|
{ ICMP6_PARAMPROB_NEXTHEADER, "nextheader" },
|
|
|
|
{ ICMP6_PARAMPROB_OPTION, "option" },
|
|
|
|
{ -2, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_subtype_t icmpquerysubject6[] = {
|
|
|
|
{ ICMP6_NI_SUBJ_IPV6, "ipv6" },
|
|
|
|
{ ICMP6_NI_SUBJ_FQDN, "fqdn" },
|
|
|
|
{ ICMP6_NI_SUBJ_IPV4, "ipv4" },
|
|
|
|
{ -2, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_subtype_t icmpnodeinfo6[] = {
|
|
|
|
{ ICMP6_NI_SUCCESS, "success" },
|
|
|
|
{ ICMP6_NI_REFUSED, "refused" },
|
|
|
|
{ ICMP6_NI_UNKNOWN, "unknown" },
|
|
|
|
{ -2, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_subtype_t icmprenumber6[] = {
|
|
|
|
{ ICMP6_ROUTER_RENUMBERING_COMMAND, "command" },
|
|
|
|
{ ICMP6_ROUTER_RENUMBERING_RESULT, "result" },
|
|
|
|
{ ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET, "seqnum_reset" },
|
|
|
|
{ -2, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_type_t icmptypes6[] = {
|
|
|
|
{ 0, NULL, 0, NULL },
|
|
|
|
{ ICMP6_DST_UNREACH, icmpredirect6,
|
|
|
|
IST_SZ(icmpredirect6), "unreach" },
|
|
|
|
{ ICMP6_PACKET_TOO_BIG, NULL, 0, "toobig" },
|
|
|
|
{ ICMP6_TIME_EXCEEDED, icmptimexceed6,
|
|
|
|
IST_SZ(icmptimexceed6), "timxceed" },
|
|
|
|
{ ICMP6_PARAM_PROB, icmpparamprob6,
|
|
|
|
IST_SZ(icmpparamprob6), "paramprob" },
|
|
|
|
{ ICMP6_ECHO_REQUEST, NULL, 0, "echo" },
|
|
|
|
{ ICMP6_ECHO_REPLY, NULL, 0, "echoreply" },
|
|
|
|
{ ICMP6_MEMBERSHIP_QUERY, icmpquerysubject6,
|
|
|
|
IST_SZ(icmpquerysubject6), "groupmemberquery" },
|
|
|
|
{ ICMP6_MEMBERSHIP_REPORT,NULL, 0, "groupmemberreport" },
|
|
|
|
{ ICMP6_MEMBERSHIP_REDUCTION,NULL, 0, "groupmemberterm" },
|
|
|
|
{ ND_ROUTER_SOLICIT, NULL, 0, "routersolicit" },
|
|
|
|
{ ND_ROUTER_ADVERT, NULL, 0, "routeradvert" },
|
|
|
|
{ ND_NEIGHBOR_SOLICIT, NULL, 0, "neighborsolicit" },
|
|
|
|
{ ND_NEIGHBOR_ADVERT, NULL, 0, "neighboradvert" },
|
|
|
|
{ ND_REDIRECT, NULL, 0, "redirect" },
|
|
|
|
{ ICMP6_ROUTER_RENUMBERING, icmprenumber6,
|
|
|
|
IST_SZ(icmprenumber6), "routerrenumber" },
|
|
|
|
{ ICMP6_WRUREQUEST, NULL, 0, "whoareyourequest" },
|
|
|
|
{ ICMP6_WRUREPLY, NULL, 0, "whoareyoureply" },
|
|
|
|
{ ICMP6_FQDN_QUERY, NULL, 0, "fqdnquery" },
|
|
|
|
{ ICMP6_FQDN_REPLY, NULL, 0, "fqdnreply" },
|
|
|
|
{ ICMP6_NI_QUERY, icmpnodeinfo6,
|
|
|
|
IST_SZ(icmpnodeinfo6), "nodeinforequest" },
|
|
|
|
{ ICMP6_NI_REPLY, NULL, 0, "nodeinforeply" },
|
|
|
|
{ MLD6_MTRACE_RESP, NULL, 0, "mtraceresponse" },
|
|
|
|
{ MLD6_MTRACE, NULL, 0, "mtracerequest" },
|
|
|
|
{ -2, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static icmp_subtype_t *find_icmpsubtype(type, table, tablesz)
|
|
|
|
int type;
|
|
|
|
icmp_subtype_t *table;
|
|
|
|
size_t tablesz;
|
|
|
|
{
|
|
|
|
icmp_subtype_t *ist;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (tablesz < 2)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((type < 0) || (type > table[tablesz - 2].ist_val))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = type;
|
|
|
|
if (table[type].ist_val == type)
|
|
|
|
return table + type;
|
|
|
|
|
|
|
|
for (i = 0, ist = table; ist->ist_val != -2; i++, ist++)
|
|
|
|
if (ist->ist_val == type)
|
|
|
|
return ist;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static icmp_type_t *find_icmptype(type, table, tablesz)
|
|
|
|
int type;
|
|
|
|
icmp_type_t *table;
|
|
|
|
size_t tablesz;
|
|
|
|
{
|
|
|
|
icmp_type_t *it;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (tablesz < 2)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((type < 0) || (type > table[tablesz - 2].it_val))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = type;
|
|
|
|
if (table[type].it_val == type)
|
|
|
|
return table + type;
|
|
|
|
|
|
|
|
for (i = 0, it = table; it->it_val != -2; i++, it++)
|
|
|
|
if (it->it_val == type)
|
|
|
|
return it;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-07-28 12:08:15 +00:00
|
|
|
|
|
|
|
static void handlehup(sig)
|
1998-10-17 14:47:00 +00:00
|
|
|
int sig;
|
1998-06-20 18:43:25 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
signal(SIGHUP, handlehup);
|
|
|
|
if (logfile && (fp = fopen(logfile, "a")))
|
|
|
|
newlog = fp;
|
2000-02-10 03:17:51 +00:00
|
|
|
init_tabs();
|
1998-06-20 18:43:25 +00:00
|
|
|
donehup = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
static void init_tabs()
|
|
|
|
{
|
|
|
|
struct protoent *p;
|
|
|
|
struct servent *s;
|
|
|
|
char *name, **tab;
|
2000-05-24 02:19:15 +00:00
|
|
|
int port;
|
2000-02-10 03:17:51 +00:00
|
|
|
|
|
|
|
if (protocols != NULL) {
|
|
|
|
free(protocols);
|
|
|
|
protocols = NULL;
|
|
|
|
}
|
|
|
|
protocols = (char **)malloc(256 * sizeof(*protocols));
|
|
|
|
if (protocols != NULL) {
|
|
|
|
bzero((char *)protocols, 256 * sizeof(*protocols));
|
|
|
|
|
|
|
|
setprotoent(1);
|
|
|
|
while ((p = getprotoent()) != NULL)
|
|
|
|
if (p->p_proto >= 0 && p->p_proto <= 255 &&
|
2002-03-19 11:48:16 +00:00
|
|
|
p->p_name != NULL && protocols[p->p_proto] == NULL)
|
2000-02-10 03:17:51 +00:00
|
|
|
protocols[p->p_proto] = strdup(p->p_name);
|
|
|
|
endprotoent();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (udp_ports != NULL) {
|
|
|
|
free(udp_ports);
|
|
|
|
udp_ports = NULL;
|
|
|
|
}
|
|
|
|
udp_ports = (char **)malloc(65536 * sizeof(*udp_ports));
|
|
|
|
if (udp_ports != NULL)
|
|
|
|
bzero((char *)udp_ports, 65536 * sizeof(*udp_ports));
|
|
|
|
|
|
|
|
if (tcp_ports != NULL) {
|
|
|
|
free(tcp_ports);
|
|
|
|
tcp_ports = NULL;
|
|
|
|
}
|
|
|
|
tcp_ports = (char **)malloc(65536 * sizeof(*tcp_ports));
|
|
|
|
if (tcp_ports != NULL)
|
|
|
|
bzero((char *)tcp_ports, 65536 * sizeof(*tcp_ports));
|
|
|
|
|
|
|
|
setservent(1);
|
|
|
|
while ((s = getservent()) != NULL) {
|
|
|
|
if (s->s_proto == NULL)
|
|
|
|
continue;
|
|
|
|
else if (!strcmp(s->s_proto, "tcp")) {
|
2000-07-19 13:56:18 +00:00
|
|
|
port = ntohs(s->s_port);
|
2000-02-10 03:17:51 +00:00
|
|
|
name = s->s_name;
|
|
|
|
tab = tcp_ports;
|
|
|
|
} else if (!strcmp(s->s_proto, "udp")) {
|
2000-07-19 13:56:18 +00:00
|
|
|
port = ntohs(s->s_port);
|
2000-02-10 03:17:51 +00:00
|
|
|
name = s->s_name;
|
|
|
|
tab = udp_ports;
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
if ((port < 0 || port > 65535) || (name == NULL))
|
|
|
|
continue;
|
|
|
|
tab[port] = strdup(name);
|
|
|
|
}
|
|
|
|
endservent();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *getproto(p)
|
|
|
|
u_int p;
|
|
|
|
{
|
|
|
|
static char pnum[4];
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
p &= 0xff;
|
|
|
|
s = protocols ? protocols[p] : NULL;
|
|
|
|
if (s == NULL) {
|
|
|
|
sprintf(pnum, "%u", p);
|
|
|
|
s = pnum;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int read_log(fd, lenp, buf, bufsize)
|
1997-04-03 10:22:02 +00:00
|
|
|
int fd, bufsize, *lenp;
|
|
|
|
char *buf;
|
|
|
|
{
|
1997-11-16 04:52:19 +00:00
|
|
|
int nr;
|
1997-04-03 10:22:02 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
nr = read(fd, buf, bufsize);
|
|
|
|
if (!nr)
|
1997-04-03 10:22:02 +00:00
|
|
|
return 2;
|
1997-11-16 04:52:19 +00:00
|
|
|
if ((nr < 0) && (errno != EINTR))
|
|
|
|
return -1;
|
|
|
|
*lenp = nr;
|
1997-02-09 22:50:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-24 02:19:15 +00:00
|
|
|
char *hostname(res, v, ip)
|
|
|
|
int res, v;
|
|
|
|
u_32_t *ip;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
2001-07-28 12:08:15 +00:00
|
|
|
# define MAX_INETA 16
|
|
|
|
static char hname[MAXHOSTNAMELEN + MAX_INETA + 3];
|
2000-05-24 02:19:15 +00:00
|
|
|
#ifdef USE_INET6
|
|
|
|
static char hostbuf[MAXHOSTNAMELEN+1];
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
struct hostent *hp;
|
2000-05-24 02:19:15 +00:00
|
|
|
struct in_addr ipa;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2000-05-24 02:19:15 +00:00
|
|
|
if (v == 4) {
|
|
|
|
ipa.s_addr = *ip;
|
|
|
|
if (!res)
|
|
|
|
return inet_ntoa(ipa);
|
2002-03-19 11:48:16 +00:00
|
|
|
hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
|
2000-05-24 02:19:15 +00:00
|
|
|
if (!hp)
|
|
|
|
return inet_ntoa(ipa);
|
2002-03-19 11:48:16 +00:00
|
|
|
sprintf(hname, "%.*s[%s]", MAXHOSTNAMELEN, hp->h_name,
|
|
|
|
inet_ntoa(ipa));
|
2001-07-28 12:08:15 +00:00
|
|
|
return hname;
|
2000-05-24 02:19:15 +00:00
|
|
|
}
|
|
|
|
#ifdef USE_INET6
|
|
|
|
(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
|
|
|
|
hostbuf[MAXHOSTNAMELEN] = '\0';
|
|
|
|
return hostbuf;
|
|
|
|
#else
|
|
|
|
return "IPv6";
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *portname(res, proto, port)
|
|
|
|
int res;
|
|
|
|
char *proto;
|
2000-02-10 03:17:51 +00:00
|
|
|
u_int port;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
static char pname[8];
|
2000-02-10 03:17:51 +00:00
|
|
|
char *s;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
port = ntohs(port);
|
|
|
|
port &= 0xffff;
|
|
|
|
(void) sprintf(pname, "%u", port);
|
1998-03-21 10:04:55 +00:00
|
|
|
if (!res || (opts & OPT_PORTNUM))
|
1997-02-09 22:50:16 +00:00
|
|
|
return pname;
|
2000-02-10 03:17:51 +00:00
|
|
|
s = NULL;
|
|
|
|
if (!strcmp(proto, "tcp"))
|
|
|
|
s = tcp_ports[port];
|
|
|
|
else if (!strcmp(proto, "udp"))
|
|
|
|
s = udp_ports[port];
|
|
|
|
if (s == NULL)
|
|
|
|
s = pname;
|
|
|
|
return s;
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-28 12:08:15 +00:00
|
|
|
static char *icmpname(type, code)
|
|
|
|
u_int type;
|
|
|
|
u_int code;
|
|
|
|
{
|
|
|
|
static char name[80];
|
2002-03-19 11:48:16 +00:00
|
|
|
icmp_subtype_t *ist;
|
|
|
|
icmp_type_t *it;
|
|
|
|
char *s;
|
2001-07-28 12:08:15 +00:00
|
|
|
|
|
|
|
s = NULL;
|
2002-03-19 11:48:16 +00:00
|
|
|
it = find_icmptype(type, icmptypes, sizeof(icmptypes) / sizeof(*it));
|
|
|
|
if (it != NULL)
|
|
|
|
s = it->it_name;
|
|
|
|
|
2001-07-28 12:08:15 +00:00
|
|
|
if (s == NULL)
|
|
|
|
sprintf(name, "icmptype(%d)/", type);
|
|
|
|
else
|
|
|
|
sprintf(name, "%s/", s);
|
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
ist = NULL;
|
|
|
|
if (it != NULL && it->it_subtable != NULL)
|
|
|
|
ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
|
2001-07-28 12:08:15 +00:00
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
if (ist != NULL && ist->ist_name != NULL)
|
|
|
|
strcat(name, ist->ist_name);
|
|
|
|
else
|
|
|
|
sprintf(name + strlen(name), "%d", code);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *icmpname6(type, code)
|
|
|
|
u_int type;
|
|
|
|
u_int code;
|
|
|
|
{
|
|
|
|
static char name[80];
|
|
|
|
icmp_subtype_t *ist;
|
|
|
|
icmp_type_t *it;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
s = NULL;
|
|
|
|
it = find_icmptype(type, icmptypes6, sizeof(icmptypes6) / sizeof(*it));
|
|
|
|
if (it != NULL)
|
|
|
|
s = it->it_name;
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
sprintf(name, "icmpv6type(%d)/", type);
|
|
|
|
else
|
|
|
|
sprintf(name, "%s/", s);
|
|
|
|
|
|
|
|
ist = NULL;
|
|
|
|
if (it != NULL && it->it_subtable != NULL)
|
|
|
|
ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
|
|
|
|
|
|
|
|
if (ist != NULL && ist->ist_name != NULL)
|
|
|
|
strcat(name, ist->ist_name);
|
|
|
|
else
|
|
|
|
sprintf(name + strlen(name), "%d", code);
|
2001-07-28 12:08:15 +00:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
static void dumphex(log, buf, len)
|
|
|
|
FILE *log;
|
|
|
|
u_char *buf;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
char line[80];
|
|
|
|
int i, j, k;
|
|
|
|
u_char *s = buf, *t = (u_char *)line;
|
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
if (len == 0 || buf == 0)
|
|
|
|
return;
|
|
|
|
*line = '\0';
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
for (i = len, j = 0; i; i--, j++, s++) {
|
|
|
|
if (j && !(j & 0xf)) {
|
|
|
|
*t++ = '\n';
|
|
|
|
*t = '\0';
|
1997-11-16 04:52:19 +00:00
|
|
|
if (!(opts & OPT_SYSLOG))
|
1998-06-20 18:43:25 +00:00
|
|
|
fputs(line, log);
|
1997-11-16 04:52:19 +00:00
|
|
|
else
|
|
|
|
syslog(LOG_INFO, "%s", line);
|
1997-02-09 22:50:16 +00:00
|
|
|
t = (u_char *)line;
|
|
|
|
*t = '\0';
|
|
|
|
}
|
1997-11-16 04:52:19 +00:00
|
|
|
sprintf((char *)t, "%02x", *s & 0xff);
|
1997-02-09 22:50:16 +00:00
|
|
|
t += 2;
|
|
|
|
if (!((j + 1) & 0xf)) {
|
|
|
|
s -= 15;
|
2002-03-19 11:48:16 +00:00
|
|
|
sprintf((char *)t, " ");
|
1997-02-09 22:50:16 +00:00
|
|
|
t += 8;
|
|
|
|
for (k = 16; k; k--, s++)
|
|
|
|
*t++ = (isprint(*s) ? *s : '.');
|
|
|
|
s--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((j + 1) & 0xf)
|
|
|
|
*t++ = ' ';;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j & 0xf) {
|
|
|
|
for (k = 16 - (j & 0xf); k; k--) {
|
|
|
|
*t++ = ' ';
|
|
|
|
*t++ = ' ';
|
|
|
|
*t++ = ' ';
|
|
|
|
}
|
1997-11-16 04:52:19 +00:00
|
|
|
sprintf((char *)t, " ");
|
1997-02-09 22:50:16 +00:00
|
|
|
t += 7;
|
|
|
|
s -= j & 0xf;
|
|
|
|
for (k = j & 0xf; k; k--, s++)
|
|
|
|
*t++ = (isprint(*s) ? *s : '.');
|
|
|
|
*t++ = '\n';
|
|
|
|
*t = '\0';
|
|
|
|
}
|
1997-11-16 04:52:19 +00:00
|
|
|
if (!(opts & OPT_SYSLOG)) {
|
1998-06-20 18:43:25 +00:00
|
|
|
fputs(line, log);
|
|
|
|
fflush(log);
|
1997-11-16 04:52:19 +00:00
|
|
|
} else
|
|
|
|
syslog(LOG_INFO, "%s", line);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
1997-04-03 10:22:02 +00:00
|
|
|
static void print_natlog(log, buf, blen)
|
|
|
|
FILE *log;
|
|
|
|
char *buf;
|
|
|
|
int blen;
|
|
|
|
{
|
1997-11-16 04:52:19 +00:00
|
|
|
struct natlog *nl;
|
|
|
|
iplog_t *ipl = (iplog_t *)buf;
|
1997-04-03 10:22:02 +00:00
|
|
|
char *t = line;
|
|
|
|
struct tm *tm;
|
1998-06-20 18:43:25 +00:00
|
|
|
int res, i, len;
|
2000-02-10 03:17:51 +00:00
|
|
|
char *proto;
|
1997-04-03 10:22:02 +00:00
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
nl = (struct natlog *)((char *)ipl + IPLOG_SIZE);
|
1997-04-03 10:22:02 +00:00
|
|
|
res = (opts & OPT_RESOLVE) ? 1 : 0;
|
1997-11-16 04:52:19 +00:00
|
|
|
tm = localtime((time_t *)&ipl->ipl_sec);
|
1998-06-20 18:43:25 +00:00
|
|
|
len = sizeof(line);
|
1997-04-03 10:22:02 +00:00
|
|
|
if (!(opts & OPT_SYSLOG)) {
|
1998-06-20 18:43:25 +00:00
|
|
|
(void) strftime(t, len, "%d/%m/%Y ", tm);
|
|
|
|
i = strlen(t);
|
|
|
|
len -= i;
|
|
|
|
t += i;
|
1997-04-03 10:22:02 +00:00
|
|
|
}
|
1998-06-20 18:43:25 +00:00
|
|
|
(void) strftime(t, len, "%T", tm);
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, ".%-.6ld @%hd ", ipl->ipl_usec, nl->nl_rule + 1);
|
1997-04-03 10:22:02 +00:00
|
|
|
t += strlen(t);
|
|
|
|
|
|
|
|
if (nl->nl_type == NL_NEWMAP)
|
|
|
|
strcpy(t, "NAT:MAP ");
|
|
|
|
else if (nl->nl_type == NL_NEWRDR)
|
|
|
|
strcpy(t, "NAT:RDR ");
|
2000-02-10 03:17:51 +00:00
|
|
|
else if (nl->nl_type == NL_EXPIRE)
|
1997-04-03 10:22:02 +00:00
|
|
|
strcpy(t, "NAT:EXPIRE ");
|
2001-02-04 14:26:56 +00:00
|
|
|
else if (nl->nl_type == NL_FLUSH)
|
|
|
|
strcpy(t, "NAT:FLUSH ");
|
2000-07-19 13:56:18 +00:00
|
|
|
else if (nl->nl_type == NL_NEWBIMAP)
|
|
|
|
strcpy(t, "NAT:BIMAP ");
|
|
|
|
else if (nl->nl_type == NL_NEWBLOCK)
|
|
|
|
strcpy(t, "NAT:MAPBLOCK ");
|
1997-04-03 10:22:02 +00:00
|
|
|
else
|
|
|
|
sprintf(t, "Type: %d ", nl->nl_type);
|
|
|
|
t += strlen(t);
|
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
proto = getproto(nl->nl_p);
|
|
|
|
|
2000-05-24 02:19:15 +00:00
|
|
|
(void) sprintf(t, "%s,%s <- -> ", HOSTNAME_V4(res, nl->nl_inip),
|
2000-02-10 03:17:51 +00:00
|
|
|
portname(res, proto, (u_int)nl->nl_inport));
|
1997-04-03 10:22:02 +00:00
|
|
|
t += strlen(t);
|
2000-05-24 02:19:15 +00:00
|
|
|
(void) sprintf(t, "%s,%s ", HOSTNAME_V4(res, nl->nl_outip),
|
2000-02-10 03:17:51 +00:00
|
|
|
portname(res, proto, (u_int)nl->nl_outport));
|
1997-04-03 10:22:02 +00:00
|
|
|
t += strlen(t);
|
2000-05-24 02:19:15 +00:00
|
|
|
(void) sprintf(t, "[%s,%s]", HOSTNAME_V4(res, nl->nl_origip),
|
2000-02-10 03:17:51 +00:00
|
|
|
portname(res, proto, (u_int)nl->nl_origport));
|
1997-04-03 10:22:02 +00:00
|
|
|
t += strlen(t);
|
1997-05-25 15:45:04 +00:00
|
|
|
if (nl->nl_type == NL_EXPIRE) {
|
|
|
|
#ifdef USE_QUAD_T
|
|
|
|
(void) sprintf(t, " Pkts %qd Bytes %qd",
|
2000-05-24 02:19:15 +00:00
|
|
|
(long long)nl->nl_pkts,
|
|
|
|
(long long)nl->nl_bytes);
|
1997-05-25 15:45:04 +00:00
|
|
|
#else
|
|
|
|
(void) sprintf(t, " Pkts %ld Bytes %ld",
|
|
|
|
nl->nl_pkts, nl->nl_bytes);
|
2000-05-24 02:19:15 +00:00
|
|
|
#endif
|
1997-05-25 15:45:04 +00:00
|
|
|
t += strlen(t);
|
|
|
|
}
|
1997-04-03 10:22:02 +00:00
|
|
|
|
|
|
|
*t++ = '\n';
|
|
|
|
*t++ = '\0';
|
|
|
|
if (opts & OPT_SYSLOG)
|
|
|
|
syslog(LOG_INFO, "%s", line);
|
|
|
|
else
|
|
|
|
(void) fprintf(log, "%s", line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void print_statelog(log, buf, blen)
|
|
|
|
FILE *log;
|
|
|
|
char *buf;
|
|
|
|
int blen;
|
|
|
|
{
|
1997-11-16 04:52:19 +00:00
|
|
|
struct ipslog *sl;
|
|
|
|
iplog_t *ipl = (iplog_t *)buf;
|
2000-02-10 03:17:51 +00:00
|
|
|
char *t = line, *proto;
|
1997-04-03 10:22:02 +00:00
|
|
|
struct tm *tm;
|
1998-06-20 18:43:25 +00:00
|
|
|
int res, i, len;
|
1997-04-03 10:22:02 +00:00
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
sl = (struct ipslog *)((char *)ipl + IPLOG_SIZE);
|
1997-04-03 10:22:02 +00:00
|
|
|
res = (opts & OPT_RESOLVE) ? 1 : 0;
|
1997-11-16 04:52:19 +00:00
|
|
|
tm = localtime((time_t *)&ipl->ipl_sec);
|
1998-06-20 18:43:25 +00:00
|
|
|
len = sizeof(line);
|
1997-04-03 10:22:02 +00:00
|
|
|
if (!(opts & OPT_SYSLOG)) {
|
1998-06-20 18:43:25 +00:00
|
|
|
(void) strftime(t, len, "%d/%m/%Y ", tm);
|
|
|
|
i = strlen(t);
|
|
|
|
len -= i;
|
|
|
|
t += i;
|
1997-04-03 10:22:02 +00:00
|
|
|
}
|
1998-06-20 18:43:25 +00:00
|
|
|
(void) strftime(t, len, "%T", tm);
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
|
1997-04-03 10:22:02 +00:00
|
|
|
t += strlen(t);
|
|
|
|
|
|
|
|
if (sl->isl_type == ISL_NEW)
|
|
|
|
strcpy(t, "STATE:NEW ");
|
2000-02-10 03:17:51 +00:00
|
|
|
else if (sl->isl_type == ISL_EXPIRE) {
|
|
|
|
if ((sl->isl_p == IPPROTO_TCP) &&
|
|
|
|
(sl->isl_state[0] > TCPS_ESTABLISHED ||
|
|
|
|
sl->isl_state[1] > TCPS_ESTABLISHED))
|
|
|
|
strcpy(t, "STATE:CLOSE ");
|
|
|
|
else
|
|
|
|
strcpy(t, "STATE:EXPIRE ");
|
|
|
|
} else if (sl->isl_type == ISL_FLUSH)
|
|
|
|
strcpy(t, "STATE:FLUSH ");
|
2000-05-24 02:19:15 +00:00
|
|
|
else if (sl->isl_type == ISL_REMOVE)
|
|
|
|
strcpy(t, "STATE:REMOVE ");
|
1997-04-03 10:22:02 +00:00
|
|
|
else
|
|
|
|
sprintf(t, "Type: %d ", sl->isl_type);
|
|
|
|
t += strlen(t);
|
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
proto = getproto(sl->isl_p);
|
1997-04-03 10:22:02 +00:00
|
|
|
|
|
|
|
if (sl->isl_p == IPPROTO_TCP || sl->isl_p == IPPROTO_UDP) {
|
|
|
|
(void) sprintf(t, "%s,%s -> ",
|
2000-05-24 02:19:15 +00:00
|
|
|
hostname(res, sl->isl_v, (u_32_t *)&sl->isl_src),
|
2000-02-10 03:17:51 +00:00
|
|
|
portname(res, proto, (u_int)sl->isl_sport));
|
1997-04-03 10:22:02 +00:00
|
|
|
t += strlen(t);
|
1997-05-25 15:45:04 +00:00
|
|
|
(void) sprintf(t, "%s,%s PR %s",
|
2000-05-24 02:19:15 +00:00
|
|
|
hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
|
2000-02-10 03:17:51 +00:00
|
|
|
portname(res, proto, (u_int)sl->isl_dport), proto);
|
1997-04-03 10:22:02 +00:00
|
|
|
} else if (sl->isl_p == IPPROTO_ICMP) {
|
2000-05-24 02:19:15 +00:00
|
|
|
(void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
|
|
|
|
(u_32_t *)&sl->isl_src));
|
1997-04-03 10:22:02 +00:00
|
|
|
t += strlen(t);
|
1997-05-25 15:45:04 +00:00
|
|
|
(void) sprintf(t, "%s PR icmp %d",
|
2000-05-24 02:19:15 +00:00
|
|
|
hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
|
|
|
|
sl->isl_itype);
|
2002-03-19 11:48:16 +00:00
|
|
|
} else if (sl->isl_p == IPPROTO_ICMPV6) {
|
|
|
|
(void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
|
|
|
|
(u_32_t *)&sl->isl_src));
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, "%s PR icmpv6 %d",
|
|
|
|
hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
|
|
|
|
sl->isl_itype);
|
1997-04-03 10:22:02 +00:00
|
|
|
}
|
|
|
|
t += strlen(t);
|
|
|
|
if (sl->isl_type != ISL_NEW) {
|
|
|
|
#ifdef USE_QUAD_T
|
1997-05-25 15:45:04 +00:00
|
|
|
(void) sprintf(t, " Pkts %qd Bytes %qd",
|
2000-05-24 02:19:15 +00:00
|
|
|
(long long)sl->isl_pkts,
|
|
|
|
(long long)sl->isl_bytes);
|
1997-04-03 10:22:02 +00:00
|
|
|
#else
|
1997-05-25 15:45:04 +00:00
|
|
|
(void) sprintf(t, " Pkts %ld Bytes %ld",
|
1997-04-03 10:22:02 +00:00
|
|
|
sl->isl_pkts, sl->isl_bytes);
|
2000-05-24 02:19:15 +00:00
|
|
|
#endif
|
1997-04-03 10:22:02 +00:00
|
|
|
t += strlen(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
*t++ = '\n';
|
|
|
|
*t++ = '\0';
|
|
|
|
if (opts & OPT_SYSLOG)
|
|
|
|
syslog(LOG_INFO, "%s", line);
|
|
|
|
else
|
|
|
|
(void) fprintf(log, "%s", line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
static void print_log(logtype, log, buf, blen)
|
|
|
|
FILE *log;
|
|
|
|
char *buf;
|
|
|
|
int logtype, blen;
|
|
|
|
{
|
|
|
|
iplog_t *ipl;
|
1998-06-20 18:43:25 +00:00
|
|
|
char *bp = NULL, *bpo = NULL;
|
1997-11-16 04:52:19 +00:00
|
|
|
int psize;
|
|
|
|
|
|
|
|
while (blen > 0) {
|
|
|
|
ipl = (iplog_t *)buf;
|
1998-06-20 18:43:25 +00:00
|
|
|
if ((u_long)ipl & (sizeof(long)-1)) {
|
|
|
|
if (bp)
|
|
|
|
bpo = bp;
|
|
|
|
bp = (char *)malloc(blen);
|
|
|
|
bcopy((char *)ipl, bp, blen);
|
|
|
|
if (bpo) {
|
|
|
|
free(bpo);
|
|
|
|
bpo = NULL;
|
|
|
|
}
|
|
|
|
buf = bp;
|
|
|
|
continue;
|
|
|
|
}
|
1997-11-16 04:52:19 +00:00
|
|
|
if (ipl->ipl_magic != IPL_MAGIC) {
|
|
|
|
/* invalid data or out of sync */
|
1998-06-20 18:43:25 +00:00
|
|
|
break;
|
1997-11-16 04:52:19 +00:00
|
|
|
}
|
|
|
|
psize = ipl->ipl_dsize;
|
|
|
|
switch (logtype)
|
|
|
|
{
|
|
|
|
case IPL_LOGIPF :
|
|
|
|
print_ipflog(log, buf, psize);
|
|
|
|
break;
|
|
|
|
case IPL_LOGNAT :
|
|
|
|
print_natlog(log, buf, psize);
|
|
|
|
break;
|
|
|
|
case IPL_LOGSTATE :
|
|
|
|
print_statelog(log, buf, psize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
blen -= psize;
|
|
|
|
buf += psize;
|
|
|
|
}
|
1998-06-20 18:43:25 +00:00
|
|
|
if (bp)
|
|
|
|
free(bp);
|
|
|
|
return;
|
1997-11-16 04:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-03 10:22:02 +00:00
|
|
|
static void print_ipflog(log, buf, blen)
|
1997-02-09 22:50:16 +00:00
|
|
|
FILE *log;
|
|
|
|
char *buf;
|
|
|
|
int blen;
|
|
|
|
{
|
2000-02-10 03:17:51 +00:00
|
|
|
tcphdr_t *tp;
|
1997-02-09 22:50:16 +00:00
|
|
|
struct icmp *ic;
|
2000-08-13 05:00:44 +00:00
|
|
|
struct icmp *icmp;
|
1997-02-09 22:50:16 +00:00
|
|
|
struct tm *tm;
|
2000-02-10 03:17:51 +00:00
|
|
|
char *t, *proto;
|
2000-05-24 02:19:15 +00:00
|
|
|
int i, v, lvl, res, len, off, plen, ipoff;
|
1997-11-16 04:52:19 +00:00
|
|
|
ip_t *ipc, *ip;
|
2000-05-24 02:19:15 +00:00
|
|
|
u_short hl, p;
|
1997-11-16 04:52:19 +00:00
|
|
|
ipflog_t *ipf;
|
2000-05-24 02:19:15 +00:00
|
|
|
iplog_t *ipl;
|
|
|
|
u_32_t *s, *d;
|
|
|
|
#ifdef USE_INET6
|
|
|
|
ip6_t *ip6;
|
|
|
|
#endif
|
1997-11-16 04:52:19 +00:00
|
|
|
|
|
|
|
ipl = (iplog_t *)buf;
|
2002-03-19 11:48:16 +00:00
|
|
|
ipf = (ipflog_t *)((char *)buf + IPLOG_SIZE);
|
1997-11-16 04:52:19 +00:00
|
|
|
ip = (ip_t *)((char *)ipf + sizeof(*ipf));
|
2000-05-24 02:19:15 +00:00
|
|
|
v = ip->ip_v;
|
1997-02-09 22:50:16 +00:00
|
|
|
res = (opts & OPT_RESOLVE) ? 1 : 0;
|
|
|
|
t = line;
|
|
|
|
*t = '\0';
|
1997-11-16 04:52:19 +00:00
|
|
|
tm = localtime((time_t *)&ipl->ipl_sec);
|
|
|
|
#ifdef linux
|
2000-05-24 02:19:15 +00:00
|
|
|
if (v == 4)
|
|
|
|
ip->ip_len = ntohs(ip->ip_len);
|
1997-11-16 04:52:19 +00:00
|
|
|
#endif
|
|
|
|
|
1998-06-20 18:43:25 +00:00
|
|
|
len = sizeof(line);
|
1997-02-09 22:50:16 +00:00
|
|
|
if (!(opts & OPT_SYSLOG)) {
|
1998-06-20 18:43:25 +00:00
|
|
|
(void) strftime(t, len, "%d/%m/%Y ", tm);
|
|
|
|
i = strlen(t);
|
|
|
|
len -= i;
|
|
|
|
t += i;
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
1998-06-20 18:43:25 +00:00
|
|
|
(void) strftime(t, len, "%T", tm);
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
|
1997-11-16 04:52:19 +00:00
|
|
|
t += strlen(t);
|
|
|
|
if (ipl->ipl_count > 1) {
|
|
|
|
(void) sprintf(t, "%dx ", ipl->ipl_count);
|
|
|
|
t += strlen(t);
|
|
|
|
}
|
|
|
|
#if (SOLARIS || \
|
|
|
|
(defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
|
|
|
|
(defined(OpenBSD) && (OpenBSD >= 199603))) || defined(linux)
|
2002-03-19 11:48:16 +00:00
|
|
|
{
|
|
|
|
char ifname[sizeof(ipf->fl_ifname) + 1];
|
|
|
|
|
|
|
|
strncpy(ifname, (char *)ipf->fl_ifname, sizeof(ipf->fl_ifname));
|
|
|
|
ifname[sizeof(ipf->fl_ifname)] = '\0';
|
|
|
|
(void) sprintf(t, "%s", ifname);
|
2000-02-10 03:17:51 +00:00
|
|
|
t += strlen(t);
|
|
|
|
# if SOLARIS
|
2002-03-19 11:48:16 +00:00
|
|
|
if (isalpha(*(t - 1))) {
|
|
|
|
sprintf(t, "%d", ipf->fl_unit);
|
|
|
|
t += strlen(t);
|
|
|
|
}
|
2000-02-10 03:17:51 +00:00
|
|
|
# endif
|
2002-03-19 11:48:16 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
#else
|
|
|
|
for (len = 0; len < 3; len++)
|
2000-02-10 03:17:51 +00:00
|
|
|
if (ipf->fl_ifname[len] == '\0')
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
1997-11-16 04:52:19 +00:00
|
|
|
if (ipf->fl_ifname[len])
|
1997-02-09 22:50:16 +00:00
|
|
|
len++;
|
1997-11-16 04:52:19 +00:00
|
|
|
(void) sprintf(t, "%*.*s%u", len, len, ipf->fl_ifname, ipf->fl_unit);
|
|
|
|
t += strlen(t);
|
2000-02-10 03:17:51 +00:00
|
|
|
#endif
|
2002-03-19 11:48:16 +00:00
|
|
|
if (ipf->fl_group == 0xffffffff)
|
|
|
|
strcat(t, " @-1:");
|
|
|
|
else
|
|
|
|
(void) sprintf(t, " @%u:", ipf->fl_group);
|
|
|
|
t += strlen(t);
|
|
|
|
if (ipf->fl_rule == 0xffffffff)
|
|
|
|
strcat(t, "-1 ");
|
|
|
|
else
|
|
|
|
(void) sprintf(t, "%u ", ipf->fl_rule + 1);
|
2000-02-10 03:17:51 +00:00
|
|
|
t += strlen(t);
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (ipf->fl_flags & FF_SHORT) {
|
2000-02-10 03:17:51 +00:00
|
|
|
*t++ = 'S';
|
1997-02-09 22:50:16 +00:00
|
|
|
lvl = LOG_ERR;
|
1997-11-16 04:52:19 +00:00
|
|
|
} else if (ipf->fl_flags & FR_PASS) {
|
|
|
|
if (ipf->fl_flags & FR_LOGP)
|
2000-02-10 03:17:51 +00:00
|
|
|
*t++ = 'p';
|
1997-02-09 22:50:16 +00:00
|
|
|
else
|
2000-02-10 03:17:51 +00:00
|
|
|
*t++ = 'P';
|
1997-02-09 22:50:16 +00:00
|
|
|
lvl = LOG_NOTICE;
|
1997-11-16 04:52:19 +00:00
|
|
|
} else if (ipf->fl_flags & FR_BLOCK) {
|
|
|
|
if (ipf->fl_flags & FR_LOGB)
|
2000-02-10 03:17:51 +00:00
|
|
|
*t++ = 'b';
|
1997-02-09 22:50:16 +00:00
|
|
|
else
|
2000-02-10 03:17:51 +00:00
|
|
|
*t++ = 'B';
|
1997-02-09 22:50:16 +00:00
|
|
|
lvl = LOG_WARNING;
|
1997-11-16 04:52:19 +00:00
|
|
|
} else if (ipf->fl_flags & FF_LOGNOMATCH) {
|
2000-02-10 03:17:51 +00:00
|
|
|
*t++ = 'n';
|
1997-02-09 22:50:16 +00:00
|
|
|
lvl = LOG_NOTICE;
|
|
|
|
} else {
|
2000-02-10 03:17:51 +00:00
|
|
|
*t++ = 'L';
|
1997-02-09 22:50:16 +00:00
|
|
|
lvl = LOG_INFO;
|
|
|
|
}
|
2000-02-10 03:17:51 +00:00
|
|
|
if (ipf->fl_loglevel != 0xffff)
|
|
|
|
lvl = ipf->fl_loglevel;
|
|
|
|
*t++ = ' ';
|
|
|
|
*t = '\0';
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2000-05-24 02:19:15 +00:00
|
|
|
if (v == 6) {
|
|
|
|
#ifdef USE_INET6
|
|
|
|
off = 0;
|
|
|
|
ipoff = 0;
|
|
|
|
hl = sizeof(ip6_t);
|
|
|
|
ip6 = (ip6_t *)ip;
|
|
|
|
p = (u_short)ip6->ip6_nxt;
|
|
|
|
s = (u_32_t *)&ip6->ip6_src;
|
|
|
|
d = (u_32_t *)&ip6->ip6_dst;
|
|
|
|
plen = ntohs(ip6->ip6_plen);
|
|
|
|
#else
|
|
|
|
sprintf(t, "ipv6");
|
|
|
|
goto printipflog;
|
|
|
|
#endif
|
|
|
|
} else if (v == 4) {
|
|
|
|
hl = (ip->ip_hl << 2);
|
|
|
|
ipoff = ip->ip_off;
|
|
|
|
off = ipoff & IP_OFFMASK;
|
|
|
|
p = (u_short)ip->ip_p;
|
|
|
|
s = (u_32_t *)&ip->ip_src;
|
|
|
|
d = (u_32_t *)&ip->ip_dst;
|
2000-10-26 12:47:40 +00:00
|
|
|
plen = ip->ip_len;
|
2000-05-24 02:19:15 +00:00
|
|
|
} else {
|
|
|
|
goto printipflog;
|
|
|
|
}
|
|
|
|
proto = getproto(p);
|
|
|
|
|
|
|
|
if ((p == IPPROTO_TCP || p == IPPROTO_UDP) && !off) {
|
2000-02-10 03:17:51 +00:00
|
|
|
tp = (tcphdr_t *)((char *)ip + hl);
|
2002-03-19 11:48:16 +00:00
|
|
|
if (!(ipf->fl_flags & FF_SHORT)) {
|
2000-05-24 02:19:15 +00:00
|
|
|
(void) sprintf(t, "%s,%s -> ", hostname(res, v, s),
|
2000-02-10 03:17:51 +00:00
|
|
|
portname(res, proto, (u_int)tp->th_sport));
|
1997-02-09 22:50:16 +00:00
|
|
|
t += strlen(t);
|
2002-03-19 11:48:16 +00:00
|
|
|
(void) sprintf(t, "%s,%s PR %s len %hu %hu",
|
2000-05-24 02:19:15 +00:00
|
|
|
hostname(res, v, d),
|
2000-02-10 03:17:51 +00:00
|
|
|
portname(res, proto, (u_int)tp->th_dport),
|
2000-05-24 02:19:15 +00:00
|
|
|
proto, hl, plen);
|
1997-02-09 22:50:16 +00:00
|
|
|
t += strlen(t);
|
|
|
|
|
|
|
|
if (p == IPPROTO_TCP) {
|
2002-03-19 11:48:16 +00:00
|
|
|
*t++ = ' ';
|
1997-02-09 22:50:16 +00:00
|
|
|
*t++ = '-';
|
|
|
|
for (i = 0; tcpfl[i].value; i++)
|
|
|
|
if (tp->th_flags & tcpfl[i].value)
|
|
|
|
*t++ = tcpfl[i].flag;
|
2000-02-10 03:17:51 +00:00
|
|
|
if (opts & OPT_VERBOSE) {
|
|
|
|
(void) sprintf(t, " %lu %lu %hu",
|
|
|
|
(u_long)(ntohl(tp->th_seq)),
|
|
|
|
(u_long)(ntohl(tp->th_ack)),
|
|
|
|
ntohs(tp->th_win));
|
|
|
|
t += strlen(t);
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
*t = '\0';
|
|
|
|
} else {
|
2000-05-24 02:19:15 +00:00
|
|
|
(void) sprintf(t, "%s -> ", hostname(res, v, s));
|
1997-02-09 22:50:16 +00:00
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, "%s PR %s len %hu %hu",
|
2000-05-24 02:19:15 +00:00
|
|
|
hostname(res, v, d), proto, hl, plen);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
2002-03-19 11:48:16 +00:00
|
|
|
} else if ((p == IPPROTO_ICMPV6) && !off && (v == 6)) {
|
|
|
|
ic = (struct icmp *)((char *)ip + hl);
|
|
|
|
(void) sprintf(t, "%s -> ", hostname(res, v, s));
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, "%s PR icmpv6 len %hu %hu icmpv6 %s",
|
|
|
|
hostname(res, v, d), hl, plen,
|
|
|
|
icmpname6(ic->icmp_type, ic->icmp_code));
|
2000-05-24 02:19:15 +00:00
|
|
|
} else if ((p == IPPROTO_ICMP) && !off && (v == 4)) {
|
1997-02-09 22:50:16 +00:00
|
|
|
ic = (struct icmp *)((char *)ip + hl);
|
2000-05-24 02:19:15 +00:00
|
|
|
(void) sprintf(t, "%s -> ", hostname(res, v, s));
|
1997-02-09 22:50:16 +00:00
|
|
|
t += strlen(t);
|
2001-07-28 12:08:15 +00:00
|
|
|
(void) sprintf(t, "%s PR icmp len %hu %hu icmp %s",
|
2000-05-24 02:19:15 +00:00
|
|
|
hostname(res, v, d), hl, plen,
|
2002-03-19 11:48:16 +00:00
|
|
|
icmpname(ic->icmp_type, ic->icmp_code));
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ic->icmp_type == ICMP_UNREACH ||
|
|
|
|
ic->icmp_type == ICMP_SOURCEQUENCH ||
|
|
|
|
ic->icmp_type == ICMP_PARAMPROB ||
|
|
|
|
ic->icmp_type == ICMP_REDIRECT ||
|
|
|
|
ic->icmp_type == ICMP_TIMXCEED) {
|
|
|
|
ipc = &ic->icmp_ip;
|
2000-08-13 05:00:44 +00:00
|
|
|
i = ntohs(ipc->ip_len);
|
|
|
|
ipoff = ntohs(ipc->ip_off);
|
2000-02-10 03:17:51 +00:00
|
|
|
proto = getproto(ipc->ip_p);
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2000-08-13 05:00:44 +00:00
|
|
|
if (!(ipoff & IP_OFFMASK) &&
|
|
|
|
((ipc->ip_p == IPPROTO_TCP) ||
|
|
|
|
(ipc->ip_p == IPPROTO_UDP))) {
|
|
|
|
tp = (tcphdr_t *)((char *)ipc + hl);
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, " for %s,%s -",
|
|
|
|
HOSTNAME_V4(res, ipc->ip_src),
|
|
|
|
portname(res, proto,
|
|
|
|
(u_int)tp->th_sport));
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, " %s,%s PR %s len %hu %hu",
|
|
|
|
HOSTNAME_V4(res, ipc->ip_dst),
|
|
|
|
portname(res, proto,
|
|
|
|
(u_int)tp->th_dport),
|
|
|
|
proto, ipc->ip_hl << 2, i);
|
|
|
|
} else if (!(ipoff & IP_OFFMASK) &&
|
|
|
|
(ipc->ip_p == IPPROTO_ICMP)) {
|
|
|
|
icmp = (icmphdr_t *)((char *)ipc + hl);
|
|
|
|
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, " for %s -",
|
|
|
|
HOSTNAME_V4(res, ipc->ip_src));
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t,
|
|
|
|
" %s PR icmp len %hu %hu icmp %d/%d",
|
|
|
|
HOSTNAME_V4(res, ipc->ip_dst),
|
|
|
|
ipc->ip_hl << 2, i,
|
|
|
|
icmp->icmp_type, icmp->icmp_code);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, " for %s -",
|
|
|
|
HOSTNAME_V4(res, ipc->ip_src));
|
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, " %s PR %s len %hu (%hu)",
|
|
|
|
HOSTNAME_V4(res, ipc->ip_dst), proto,
|
|
|
|
ipc->ip_hl << 2, i);
|
|
|
|
t += strlen(t);
|
|
|
|
if (ipoff & IP_OFFMASK) {
|
|
|
|
(void) sprintf(t, " frag %s%s%hu@%hu",
|
|
|
|
ipoff & IP_MF ? "+" : "",
|
|
|
|
ipoff & IP_DF ? "-" : "",
|
|
|
|
i - (ipc->ip_hl<<2),
|
|
|
|
(ipoff & IP_OFFMASK) << 3);
|
|
|
|
}
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2000-05-24 02:19:15 +00:00
|
|
|
(void) sprintf(t, "%s -> ", hostname(res, v, s));
|
1997-02-09 22:50:16 +00:00
|
|
|
t += strlen(t);
|
|
|
|
(void) sprintf(t, "%s PR %s len %hu (%hu)",
|
2000-05-24 02:19:15 +00:00
|
|
|
hostname(res, v, d), proto, hl, plen);
|
1997-02-09 22:50:16 +00:00
|
|
|
t += strlen(t);
|
2000-05-24 02:19:15 +00:00
|
|
|
if (off & IP_OFFMASK)
|
1997-02-09 22:50:16 +00:00
|
|
|
(void) sprintf(t, " frag %s%s%hu@%hu",
|
2000-05-24 02:19:15 +00:00
|
|
|
ipoff & IP_MF ? "+" : "",
|
|
|
|
ipoff & IP_DF ? "-" : "",
|
|
|
|
plen - hl, (off & IP_OFFMASK) << 3);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
t += strlen(t);
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (ipf->fl_flags & FR_KEEPSTATE) {
|
1997-02-09 22:50:16 +00:00
|
|
|
(void) strcpy(t, " K-S");
|
|
|
|
t += strlen(t);
|
|
|
|
}
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (ipf->fl_flags & FR_KEEPFRAG) {
|
1997-02-09 22:50:16 +00:00
|
|
|
(void) strcpy(t, " K-F");
|
|
|
|
t += strlen(t);
|
|
|
|
}
|
|
|
|
|
2002-03-19 11:48:16 +00:00
|
|
|
if (ipf->fl_dir == 0)
|
2000-02-10 03:17:51 +00:00
|
|
|
strcpy(t, " IN");
|
2002-03-19 11:48:16 +00:00
|
|
|
else if (ipf->fl_dir == 1)
|
2000-02-10 03:17:51 +00:00
|
|
|
strcpy(t, " OUT");
|
|
|
|
t += strlen(t);
|
2000-05-24 02:19:15 +00:00
|
|
|
printipflog:
|
1997-02-09 22:50:16 +00:00
|
|
|
*t++ = '\n';
|
|
|
|
*t++ = '\0';
|
|
|
|
if (opts & OPT_SYSLOG)
|
|
|
|
syslog(lvl, "%s", line);
|
|
|
|
else
|
|
|
|
(void) fprintf(log, "%s", line);
|
|
|
|
if (opts & OPT_HEXHDR)
|
2000-02-10 03:17:51 +00:00
|
|
|
dumphex(log, (u_char *)buf, sizeof(iplog_t) + sizeof(*ipf));
|
1997-02-09 22:50:16 +00:00
|
|
|
if (opts & OPT_HEXBODY)
|
1997-11-16 04:52:19 +00:00
|
|
|
dumphex(log, (u_char *)ip, ipf->fl_plen + ipf->fl_hlen);
|
2001-02-04 14:26:56 +00:00
|
|
|
else if ((opts & OPT_LOGBODY) && (ipf->fl_flags & FR_LOGBODY))
|
|
|
|
dumphex(log, (u_char *)ip + ipf->fl_hlen, ipf->fl_plen);
|
1997-04-03 10:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
static void usage(prog)
|
1997-04-03 10:22:02 +00:00
|
|
|
char *prog;
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: [-NFhstvxX] [-f <logfile>]\n", prog);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
static void write_pid(file)
|
|
|
|
char *file;
|
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0644)) >= 0)
|
|
|
|
fp = fdopen(fd, "w");
|
|
|
|
if (!fp) {
|
|
|
|
close(fd);
|
|
|
|
fprintf(stderr, "unable to open/create pid file: %s\n", file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(fp, "%d", getpid());
|
|
|
|
fclose(fp);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
static void flushlogs(file, log)
|
1997-04-03 10:22:02 +00:00
|
|
|
char *file;
|
|
|
|
FILE *log;
|
|
|
|
{
|
|
|
|
int fd, flushed = 0;
|
|
|
|
|
|
|
|
if ((fd = open(file, O_RDWR)) == -1) {
|
2002-03-19 11:48:16 +00:00
|
|
|
(void) fprintf(stderr, "%s: open: %s\n",
|
|
|
|
file, STRERROR(errno));
|
2001-07-28 12:08:15 +00:00
|
|
|
exit(1);
|
1997-04-03 10:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCIPFFB, &flushed) == 0) {
|
|
|
|
printf("%d bytes flushed from log buffer\n",
|
|
|
|
flushed);
|
|
|
|
fflush(stdout);
|
|
|
|
} else
|
|
|
|
perror("SIOCIPFFB");
|
|
|
|
(void) close(fd);
|
|
|
|
|
|
|
|
if (flushed) {
|
|
|
|
if (opts & OPT_SYSLOG)
|
|
|
|
syslog(LOG_INFO, "%d bytes flushed from log\n",
|
|
|
|
flushed);
|
1997-11-16 04:52:19 +00:00
|
|
|
else if (log != stdout)
|
1997-04-03 10:22:02 +00:00
|
|
|
fprintf(log, "%d bytes flushed from log\n", flushed);
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
1997-04-03 10:22:02 +00:00
|
|
|
|
1998-03-21 10:04:55 +00:00
|
|
|
static void logopts(turnon, options)
|
|
|
|
int turnon;
|
|
|
|
char *options;
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
for (s = options; *s; s++)
|
|
|
|
{
|
|
|
|
switch (*s)
|
|
|
|
{
|
|
|
|
case 'N' :
|
|
|
|
flags |= OPT_NAT;
|
|
|
|
break;
|
|
|
|
case 'S' :
|
|
|
|
flags |= OPT_STATE;
|
|
|
|
break;
|
|
|
|
case 'I' :
|
|
|
|
flags |= OPT_FILTER;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
fprintf(stderr, "Unknown log option %c\n", *s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (turnon)
|
|
|
|
opts |= flags;
|
|
|
|
else
|
|
|
|
opts &= ~(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
int main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
1998-06-20 18:43:25 +00:00
|
|
|
int fdt[3], devices = 0, make_daemon = 0;
|
2002-03-19 11:48:16 +00:00
|
|
|
char buf[IPLLOGSIZE], *iplfile[3], *s;
|
2002-06-07 09:01:41 +00:00
|
|
|
int fd[3], doread, n, i;
|
1997-02-09 22:50:16 +00:00
|
|
|
extern char *optarg;
|
2002-06-07 09:01:41 +00:00
|
|
|
extern int optind;
|
|
|
|
int regular[3], c;
|
|
|
|
FILE *log = stdout;
|
|
|
|
struct stat sb;
|
|
|
|
size_t nr, tr;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
fd[0] = fd[1] = fd[2] = -1;
|
1998-03-21 10:04:55 +00:00
|
|
|
fdt[0] = fdt[1] = fdt[2] = -1;
|
|
|
|
iplfile[0] = IPL_NAME;
|
|
|
|
iplfile[1] = IPNAT_NAME;
|
|
|
|
iplfile[2] = IPSTATE_NAME;
|
1997-11-16 04:52:19 +00:00
|
|
|
|
2001-02-04 14:26:56 +00:00
|
|
|
while ((c = getopt(argc, argv, "?abDf:FhnN:o:O:pP:sS:tvxX")) != -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
1997-04-03 10:22:02 +00:00
|
|
|
case 'a' :
|
2000-02-10 03:17:51 +00:00
|
|
|
opts |= OPT_LOGALL;
|
|
|
|
fdt[0] = IPL_LOGIPF;
|
|
|
|
fdt[1] = IPL_LOGNAT;
|
|
|
|
fdt[2] = IPL_LOGSTATE;
|
1997-04-03 10:22:02 +00:00
|
|
|
break;
|
2001-02-04 14:26:56 +00:00
|
|
|
case 'b' :
|
|
|
|
opts |= OPT_LOGBODY;
|
|
|
|
break;
|
1998-06-20 18:43:25 +00:00
|
|
|
case 'D' :
|
|
|
|
make_daemon = 1;
|
|
|
|
break;
|
1998-03-21 10:04:55 +00:00
|
|
|
case 'f' : case 'I' :
|
|
|
|
opts |= OPT_FILTER;
|
|
|
|
fdt[0] = IPL_LOGIPF;
|
|
|
|
iplfile[0] = optarg;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 'F' :
|
1998-03-21 10:04:55 +00:00
|
|
|
flushlogs(iplfile[0], log);
|
|
|
|
flushlogs(iplfile[1], log);
|
|
|
|
flushlogs(iplfile[2], log);
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
1997-04-03 10:22:02 +00:00
|
|
|
case 'n' :
|
1997-02-09 22:50:16 +00:00
|
|
|
opts |= OPT_RESOLVE;
|
|
|
|
break;
|
1997-04-03 10:22:02 +00:00
|
|
|
case 'N' :
|
|
|
|
opts |= OPT_NAT;
|
1998-03-21 10:04:55 +00:00
|
|
|
fdt[1] = IPL_LOGNAT;
|
|
|
|
iplfile[1] = optarg;
|
|
|
|
break;
|
|
|
|
case 'o' : case 'O' :
|
|
|
|
logopts(c == 'o', optarg);
|
|
|
|
fdt[0] = fdt[1] = fdt[2] = -1;
|
|
|
|
if (opts & OPT_FILTER)
|
|
|
|
fdt[0] = IPL_LOGIPF;
|
|
|
|
if (opts & OPT_NAT)
|
|
|
|
fdt[1] = IPL_LOGNAT;
|
|
|
|
if (opts & OPT_STATE)
|
|
|
|
fdt[2] = IPL_LOGSTATE;
|
|
|
|
break;
|
|
|
|
case 'p' :
|
|
|
|
opts |= OPT_PORTNUM;
|
1997-04-03 10:22:02 +00:00
|
|
|
break;
|
2000-02-10 03:17:51 +00:00
|
|
|
case 'P' :
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 's' :
|
2002-03-19 11:48:16 +00:00
|
|
|
s = strrchr(argv[0], '/');
|
|
|
|
if (s == NULL)
|
|
|
|
s = argv[0];
|
|
|
|
else
|
|
|
|
s++;
|
|
|
|
openlog(s, LOG_NDELAY|LOG_PID, LOGFAC);
|
1997-02-09 22:50:16 +00:00
|
|
|
opts |= OPT_SYSLOG;
|
2001-07-28 12:08:15 +00:00
|
|
|
log = NULL;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
1997-04-03 10:22:02 +00:00
|
|
|
case 'S' :
|
|
|
|
opts |= OPT_STATE;
|
1998-03-21 10:04:55 +00:00
|
|
|
fdt[2] = IPL_LOGSTATE;
|
|
|
|
iplfile[2] = optarg;
|
1997-04-03 10:22:02 +00:00
|
|
|
break;
|
|
|
|
case 't' :
|
|
|
|
opts |= OPT_TAIL;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'v' :
|
|
|
|
opts |= OPT_VERBOSE;
|
|
|
|
break;
|
|
|
|
case 'x' :
|
|
|
|
opts |= OPT_HEXBODY;
|
|
|
|
break;
|
|
|
|
case 'X' :
|
|
|
|
opts |= OPT_HEXHDR;
|
|
|
|
break;
|
1997-04-03 10:22:02 +00:00
|
|
|
default :
|
|
|
|
case 'h' :
|
|
|
|
case '?' :
|
2002-03-19 11:48:16 +00:00
|
|
|
usage(argv[0]);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
init_tabs();
|
|
|
|
|
1998-03-21 10:04:55 +00:00
|
|
|
/*
|
|
|
|
* Default action is to only open the filter log file.
|
|
|
|
*/
|
|
|
|
if ((fdt[0] == -1) && (fdt[1] == -1) && (fdt[2] == -1))
|
|
|
|
fdt[0] = IPL_LOGIPF;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (fdt[i] == -1)
|
|
|
|
continue;
|
|
|
|
if (!strcmp(iplfile[i], "-"))
|
|
|
|
fd[i] = 0;
|
|
|
|
else {
|
|
|
|
if ((fd[i] = open(iplfile[i], O_RDONLY)) == -1) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"%s: open: %s\n", iplfile[i],
|
|
|
|
STRERROR(errno));
|
2001-07-28 12:08:15 +00:00
|
|
|
exit(1);
|
|
|
|
/* NOTREACHED */
|
1998-03-21 10:04:55 +00:00
|
|
|
}
|
|
|
|
if (fstat(fd[i], &sb) == -1) {
|
2002-03-19 11:48:16 +00:00
|
|
|
(void) fprintf(stderr, "%d: fstat: %s\n",
|
|
|
|
fd[i], STRERROR(errno));
|
2001-07-28 12:08:15 +00:00
|
|
|
exit(1);
|
|
|
|
/* NOTREACHED */
|
1998-03-21 10:04:55 +00:00
|
|
|
}
|
|
|
|
if (!(regular[i] = !S_ISCHR(sb.st_mode)))
|
|
|
|
devices++;
|
1997-04-03 10:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (!(opts & OPT_SYSLOG)) {
|
1998-06-20 18:43:25 +00:00
|
|
|
logfile = argv[optind];
|
|
|
|
log = logfile ? fopen(logfile, "a") : stdout;
|
1997-11-16 04:52:19 +00:00
|
|
|
if (log == NULL) {
|
2002-03-19 11:48:16 +00:00
|
|
|
(void) fprintf(stderr, "%s: fopen: %s\n",
|
|
|
|
argv[optind], STRERROR(errno));
|
2001-07-28 12:08:15 +00:00
|
|
|
exit(1);
|
|
|
|
/* NOTREACHED */
|
1997-11-16 04:52:19 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
setvbuf(log, NULL, _IONBF, 0);
|
2000-02-10 03:17:51 +00:00
|
|
|
} else
|
|
|
|
log = NULL;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
if (make_daemon && ((log != stdout) || (opts & OPT_SYSLOG))) {
|
2001-07-28 12:08:15 +00:00
|
|
|
#if BSD
|
|
|
|
daemon(0, !(opts & OPT_SYSLOG));
|
|
|
|
#else
|
|
|
|
int pid;
|
|
|
|
if ((pid = fork()) > 0)
|
1998-06-20 18:43:25 +00:00
|
|
|
exit(0);
|
2001-07-28 12:08:15 +00:00
|
|
|
if (pid < 0) {
|
2002-03-19 11:48:16 +00:00
|
|
|
(void) fprintf(stderr, "%s: fork() failed: %s\n",
|
|
|
|
argv[0], STRERROR(errno));
|
2001-07-28 12:08:15 +00:00
|
|
|
exit(1);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
setsid();
|
|
|
|
if ((opts & OPT_SYSLOG))
|
|
|
|
close(2);
|
|
|
|
#endif /* !BSD */
|
1998-06-20 18:43:25 +00:00
|
|
|
close(0);
|
|
|
|
close(1);
|
2001-07-28 12:08:15 +00:00
|
|
|
}
|
|
|
|
write_pid(pidfile);
|
1998-06-20 18:43:25 +00:00
|
|
|
|
|
|
|
signal(SIGHUP, handlehup);
|
|
|
|
|
1997-04-03 10:22:02 +00:00
|
|
|
for (doread = 1; doread; ) {
|
|
|
|
nr = 0;
|
|
|
|
|
1998-03-21 10:04:55 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
1997-04-03 10:22:02 +00:00
|
|
|
tr = 0;
|
1998-03-21 10:04:55 +00:00
|
|
|
if (fdt[i] == -1)
|
|
|
|
continue;
|
|
|
|
if (!regular[i]) {
|
1997-04-03 10:22:02 +00:00
|
|
|
if (ioctl(fd[i], FIONREAD, &tr) == -1) {
|
2001-07-28 12:08:15 +00:00
|
|
|
if (opts & OPT_SYSLOG)
|
2002-03-19 11:48:16 +00:00
|
|
|
syslog(LOG_CRIT,
|
|
|
|
"ioctl(FIONREAD): %m");
|
2001-07-28 12:08:15 +00:00
|
|
|
else
|
|
|
|
perror("ioctl(FIONREAD)");
|
|
|
|
exit(1);
|
|
|
|
/* NOTREACHED */
|
1997-04-03 10:22:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
1997-11-16 04:52:19 +00:00
|
|
|
tr = (lseek(fd[i], 0, SEEK_CUR) < sb.st_size);
|
1997-04-03 10:22:02 +00:00
|
|
|
if (!tr && !(opts & OPT_TAIL))
|
|
|
|
doread = 0;
|
|
|
|
}
|
1998-03-21 10:04:55 +00:00
|
|
|
if (!tr)
|
1997-04-03 10:22:02 +00:00
|
|
|
continue;
|
|
|
|
nr += tr;
|
|
|
|
|
2000-02-10 03:17:51 +00:00
|
|
|
tr = read_log(fd[i], &n, buf, sizeof(buf));
|
1998-06-20 18:43:25 +00:00
|
|
|
if (donehup) {
|
|
|
|
donehup = 0;
|
|
|
|
if (newlog) {
|
|
|
|
fclose(log);
|
|
|
|
log = newlog;
|
|
|
|
newlog = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-03 10:22:02 +00:00
|
|
|
switch (tr)
|
|
|
|
{
|
|
|
|
case -1 :
|
|
|
|
if (opts & OPT_SYSLOG)
|
2001-07-28 12:08:15 +00:00
|
|
|
syslog(LOG_CRIT, "read: %m\n");
|
1997-04-03 10:22:02 +00:00
|
|
|
else
|
|
|
|
perror("read");
|
|
|
|
doread = 0;
|
|
|
|
break;
|
|
|
|
case 1 :
|
|
|
|
if (opts & OPT_SYSLOG)
|
2001-07-28 12:08:15 +00:00
|
|
|
syslog(LOG_CRIT, "aborting logging\n");
|
1997-04-03 10:22:02 +00:00
|
|
|
else
|
|
|
|
fprintf(log, "aborting logging\n");
|
|
|
|
doread = 0;
|
|
|
|
break;
|
|
|
|
case 2 :
|
|
|
|
break;
|
|
|
|
case 0 :
|
|
|
|
if (n > 0) {
|
1997-11-16 04:52:19 +00:00
|
|
|
print_log(fdt[i], log, buf, n);
|
1997-04-03 10:22:02 +00:00
|
|
|
if (!(opts & OPT_SYSLOG))
|
|
|
|
fflush(log);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
1998-03-21 10:04:55 +00:00
|
|
|
if (!nr && ((opts & OPT_TAIL) || devices))
|
1997-04-03 10:22:02 +00:00
|
|
|
sleep(1);
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
exit(0);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|