xref: /onnv-gate/usr/src/cmd/ipf/tools/ipmon.c (revision 7704:c2487b19c177)
10Sstevel@tonic-gate /*
27176Syx160601  * Copyright (C) 2001-2008 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
67176Syx160601  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
70Sstevel@tonic-gate  * Use is subject to license terms.
80Sstevel@tonic-gate  */
90Sstevel@tonic-gate 
100Sstevel@tonic-gate 
110Sstevel@tonic-gate #ifndef SOLARIS
120Sstevel@tonic-gate #define SOLARIS (defined(__SVR4) || defined(__svr4__)) && defined(sun)
130Sstevel@tonic-gate #endif
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #include <sys/types.h>
160Sstevel@tonic-gate #include <sys/stat.h>
170Sstevel@tonic-gate #include <sys/param.h>
180Sstevel@tonic-gate #include <sys/file.h>
190Sstevel@tonic-gate #include <sys/time.h>
200Sstevel@tonic-gate #define _KERNEL
210Sstevel@tonic-gate #include <sys/uio.h>
220Sstevel@tonic-gate #undef _KERNEL
230Sstevel@tonic-gate #include <sys/socket.h>
240Sstevel@tonic-gate #include <sys/ioctl.h>
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <unistd.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <fcntl.h>
300Sstevel@tonic-gate #include <errno.h>
312393Syz155240 #include <time.h>
320Sstevel@tonic-gate #if !defined(__SVR4) && !defined(__svr4__)
330Sstevel@tonic-gate # if (__FreeBSD_version >= 300000)
340Sstevel@tonic-gate #  include <sys/dirent.h>
350Sstevel@tonic-gate # else
360Sstevel@tonic-gate #  include <sys/dir.h>
370Sstevel@tonic-gate # endif
380Sstevel@tonic-gate #else
390Sstevel@tonic-gate # include <sys/filio.h>
400Sstevel@tonic-gate # include <sys/byteorder.h>
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate #if !defined(__hpux) && (!defined(__SVR4) && !defined(__GNUC__))
430Sstevel@tonic-gate # include <strings.h>
440Sstevel@tonic-gate #endif
450Sstevel@tonic-gate #include <signal.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <stddef.h>
480Sstevel@tonic-gate #include <netinet/in.h>
490Sstevel@tonic-gate #include <netinet/in_systm.h>
500Sstevel@tonic-gate #include <net/if.h>
510Sstevel@tonic-gate #include <netinet/ip.h>
522393Syz155240 #if !defined(__hpux) && !defined(linux)
530Sstevel@tonic-gate # include <netinet/tcp_fsm.h>
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate #include <netdb.h>
560Sstevel@tonic-gate #include <arpa/inet.h>
570Sstevel@tonic-gate #include <arpa/nameser.h>
580Sstevel@tonic-gate #ifdef	__hpux
590Sstevel@tonic-gate # undef	NOERROR
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate #include <resolv.h>
620Sstevel@tonic-gate 
632393Syz155240 #if !defined(linux)
642393Syz155240 # include <sys/protosw.h>
652393Syz155240 # include <netinet/ip_var.h>
662393Syz155240 #endif
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <netinet/tcp.h>
690Sstevel@tonic-gate #include <netinet/ip_icmp.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <ctype.h>
720Sstevel@tonic-gate #include <syslog.h>
730Sstevel@tonic-gate 
742393Syz155240 #include "netinet/ip_compat.h"
750Sstevel@tonic-gate #include <netinet/tcpip.h>
760Sstevel@tonic-gate #include "netinet/ip_fil.h"
770Sstevel@tonic-gate #include "netinet/ip_nat.h"
780Sstevel@tonic-gate #include "netinet/ip_state.h"
790Sstevel@tonic-gate #include "netinet/ip_proxy.h"
800Sstevel@tonic-gate #include "ipmon.h"
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #if !defined(lint)
830Sstevel@tonic-gate static const char sccsid[] = "@(#)ipmon.c	1.21 6/5/96 (C)1993-2000 Darren Reed";
842393Syz155240 static const char rcsid[] = "@(#)$Id: ipmon.c,v 1.33.2.10 2005/06/18 02:41:35 darrenr Exp $";
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #if	defined(sun) && !defined(SOLARIS2)
890Sstevel@tonic-gate #define	STRERROR(x)	sys_errlist[x]
900Sstevel@tonic-gate extern	char	*sys_errlist[];
910Sstevel@tonic-gate #else
920Sstevel@tonic-gate #define	STRERROR(x)	strerror(x)
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 
960Sstevel@tonic-gate struct	flags {
970Sstevel@tonic-gate 	int	value;
980Sstevel@tonic-gate 	char	flag;
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate typedef	struct	icmp_subtype {
1030Sstevel@tonic-gate 	int	ist_val;
1040Sstevel@tonic-gate 	char	*ist_name;
1050Sstevel@tonic-gate } icmp_subtype_t;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate typedef	struct	icmp_type {
1080Sstevel@tonic-gate 	int	it_val;
1090Sstevel@tonic-gate 	struct	icmp_subtype *it_subtable;
1100Sstevel@tonic-gate 	size_t	it_stsize;
1110Sstevel@tonic-gate 	char	*it_name;
1120Sstevel@tonic-gate } icmp_type_t;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #define	IST_SZ(x)	(sizeof(x)/sizeof(icmp_subtype_t))
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate struct	flags	tcpfl[] = {
1190Sstevel@tonic-gate 	{ TH_ACK, 'A' },
1200Sstevel@tonic-gate 	{ TH_RST, 'R' },
1210Sstevel@tonic-gate 	{ TH_SYN, 'S' },
1220Sstevel@tonic-gate 	{ TH_FIN, 'F' },
1230Sstevel@tonic-gate 	{ TH_URG, 'U' },
1240Sstevel@tonic-gate 	{ TH_PUSH,'P' },
1250Sstevel@tonic-gate 	{ TH_ECN, 'E' },
1260Sstevel@tonic-gate 	{ TH_CWR, 'C' },
1270Sstevel@tonic-gate 	{ 0, '\0' }
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate 
1302085Sdr146992 #if defined(__hpux) || (SOLARIS && (SOLARIS2 < 10))
1310Sstevel@tonic-gate static	char	*pidfile = "/etc/ipf/ipmon.pid";
1320Sstevel@tonic-gate #else
1332085Sdr146992 # if (BSD >= 199306) || SOLARIS
1340Sstevel@tonic-gate static	char	*pidfile = "/var/run/ipmon.pid";
1350Sstevel@tonic-gate # else
1360Sstevel@tonic-gate static	char	*pidfile = "/etc/ipmon.pid";
1370Sstevel@tonic-gate # endif
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static	char	line[2048];
1410Sstevel@tonic-gate static	int	opts = 0;
1420Sstevel@tonic-gate static	char	*logfile = NULL;
1430Sstevel@tonic-gate static	FILE	*binarylog = NULL;
1440Sstevel@tonic-gate static	char	*binarylogfile = NULL;
1450Sstevel@tonic-gate static	int	donehup = 0;
1460Sstevel@tonic-gate static	void	usage __P((char *));
1470Sstevel@tonic-gate static	void	handlehup __P((int));
1480Sstevel@tonic-gate static	void	flushlogs __P((char *, FILE *));
1490Sstevel@tonic-gate static	void	print_log __P((int, FILE *, char *, int));
1500Sstevel@tonic-gate static	void	print_ipflog __P((FILE *, char *, int));
1510Sstevel@tonic-gate static	void	print_natlog __P((FILE *, char *, int));
1520Sstevel@tonic-gate static	void	print_statelog __P((FILE *, char *, int));
1530Sstevel@tonic-gate static	int	read_log __P((int, int *, char *, int));
1540Sstevel@tonic-gate static	void	write_pid __P((char *));
1550Sstevel@tonic-gate static	char	*icmpname __P((u_int, u_int));
1560Sstevel@tonic-gate static	char	*icmpname6 __P((u_int, u_int));
1570Sstevel@tonic-gate static	icmp_type_t *find_icmptype __P((int, icmp_type_t *, size_t));
1580Sstevel@tonic-gate static	icmp_subtype_t *find_icmpsubtype __P((int, icmp_subtype_t *, size_t));
1590Sstevel@tonic-gate #ifdef __hpux
1600Sstevel@tonic-gate static	struct	tm	*get_tm __P((u_32_t));
1610Sstevel@tonic-gate #else
1620Sstevel@tonic-gate static	struct	tm	*get_tm __P((time_t));
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate char	*hostname __P((int, int, u_32_t *));
1660Sstevel@tonic-gate char	*portname __P((int, char *, u_int));
1670Sstevel@tonic-gate int	main __P((int, char *[]));
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static	void	logopts __P((int, char *));
1700Sstevel@tonic-gate static	void	init_tabs __P((void));
1710Sstevel@tonic-gate static	char	*getproto __P((u_int));
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static	char	**protocols = NULL;
1740Sstevel@tonic-gate static	char	**udp_ports = NULL;
1750Sstevel@tonic-gate static	char	**tcp_ports = NULL;
1760Sstevel@tonic-gate static	char	*conf_file = NULL;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate #define	OPT_SYSLOG	0x001
1800Sstevel@tonic-gate #define	OPT_RESOLVE	0x002
1810Sstevel@tonic-gate #define	OPT_HEXBODY	0x004
1820Sstevel@tonic-gate #define	OPT_VERBOSE	0x008
1830Sstevel@tonic-gate #define	OPT_HEXHDR	0x010
1840Sstevel@tonic-gate #define	OPT_TAIL	0x020
1850Sstevel@tonic-gate #define	OPT_NAT		0x080
1860Sstevel@tonic-gate #define	OPT_STATE	0x100
1870Sstevel@tonic-gate #define	OPT_FILTER	0x200
1880Sstevel@tonic-gate #define	OPT_PORTNUM	0x400
1890Sstevel@tonic-gate #define	OPT_LOGALL	(OPT_NAT|OPT_STATE|OPT_FILTER)
1900Sstevel@tonic-gate #define	OPT_LOGBODY	0x800
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate #define	HOSTNAME_V4(a,b)	hostname((a), 4, (u_32_t *)&(b))
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate #ifndef	LOGFAC
1950Sstevel@tonic-gate #define	LOGFAC	LOG_LOCAL0
1960Sstevel@tonic-gate #endif
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate static icmp_subtype_t icmpunreachnames[] = {
2000Sstevel@tonic-gate 	{ ICMP_UNREACH_NET,		"net" },
2010Sstevel@tonic-gate 	{ ICMP_UNREACH_HOST,		"host" },
2020Sstevel@tonic-gate 	{ ICMP_UNREACH_PROTOCOL,	"protocol" },
2030Sstevel@tonic-gate 	{ ICMP_UNREACH_PORT,		"port" },
2040Sstevel@tonic-gate 	{ ICMP_UNREACH_NEEDFRAG,	"needfrag" },
2050Sstevel@tonic-gate 	{ ICMP_UNREACH_SRCFAIL,		"srcfail" },
2060Sstevel@tonic-gate 	{ ICMP_UNREACH_NET_UNKNOWN,	"net_unknown" },
2070Sstevel@tonic-gate 	{ ICMP_UNREACH_HOST_UNKNOWN,	"host_unknown" },
2080Sstevel@tonic-gate 	{ ICMP_UNREACH_NET,		"isolated" },
2090Sstevel@tonic-gate 	{ ICMP_UNREACH_NET_PROHIB,	"net_prohib" },
2100Sstevel@tonic-gate 	{ ICMP_UNREACH_NET_PROHIB,	"host_prohib" },
2110Sstevel@tonic-gate 	{ ICMP_UNREACH_TOSNET,		"tosnet" },
2120Sstevel@tonic-gate 	{ ICMP_UNREACH_TOSHOST,		"toshost" },
2130Sstevel@tonic-gate 	{ ICMP_UNREACH_ADMIN_PROHIBIT,	"admin_prohibit" },
2140Sstevel@tonic-gate 	{ -2,				NULL }
2150Sstevel@tonic-gate };
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static icmp_subtype_t redirectnames[] = {
2180Sstevel@tonic-gate 	{ ICMP_REDIRECT_NET,		"net" },
2190Sstevel@tonic-gate 	{ ICMP_REDIRECT_HOST,		"host" },
2200Sstevel@tonic-gate 	{ ICMP_REDIRECT_TOSNET,		"tosnet" },
2210Sstevel@tonic-gate 	{ ICMP_REDIRECT_TOSHOST,	"toshost" },
2220Sstevel@tonic-gate 	{ -2,				NULL }
2230Sstevel@tonic-gate };
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate static icmp_subtype_t timxceednames[] = {
2260Sstevel@tonic-gate 	{ ICMP_TIMXCEED_INTRANS,	"transit" },
2270Sstevel@tonic-gate 	{ ICMP_TIMXCEED_REASS,		"reassem" },
2280Sstevel@tonic-gate 	{ -2,				NULL }
2290Sstevel@tonic-gate };
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static icmp_subtype_t paramnames[] = {
2320Sstevel@tonic-gate 	{ ICMP_PARAMPROB_ERRATPTR,	"errata_pointer" },
2330Sstevel@tonic-gate 	{ ICMP_PARAMPROB_OPTABSENT,	"optmissing" },
2340Sstevel@tonic-gate 	{ ICMP_PARAMPROB_LENGTH,	"length" },
2350Sstevel@tonic-gate 	{ -2,				NULL }
2360Sstevel@tonic-gate };
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate static icmp_type_t icmptypes[] = {
2390Sstevel@tonic-gate 	{ ICMP_ECHOREPLY,	NULL,	0,		"echoreply" },
2400Sstevel@tonic-gate 	{ -1,			NULL,	0,		NULL },
2410Sstevel@tonic-gate 	{ -1,			NULL,	0,		NULL },
2420Sstevel@tonic-gate 	{ ICMP_UNREACH,		icmpunreachnames,
2430Sstevel@tonic-gate 				IST_SZ(icmpunreachnames),"unreach" },
2440Sstevel@tonic-gate 	{ ICMP_SOURCEQUENCH,	NULL,	0,		"sourcequench" },
2450Sstevel@tonic-gate 	{ ICMP_REDIRECT,	redirectnames,
2460Sstevel@tonic-gate 				IST_SZ(redirectnames),	"redirect" },
2470Sstevel@tonic-gate 	{ -1,			NULL,	0,		NULL },
2480Sstevel@tonic-gate 	{ -1,			NULL,	0,		NULL },
2490Sstevel@tonic-gate 	{ ICMP_ECHO,		NULL,	0,		"echo" },
2500Sstevel@tonic-gate 	{ ICMP_ROUTERADVERT,	NULL,	0,		"routeradvert" },
2510Sstevel@tonic-gate 	{ ICMP_ROUTERSOLICIT,	NULL,	0,		"routersolicit" },
2520Sstevel@tonic-gate 	{ ICMP_TIMXCEED,	timxceednames,
2530Sstevel@tonic-gate 				IST_SZ(timxceednames),	"timxceed" },
2540Sstevel@tonic-gate 	{ ICMP_PARAMPROB,	paramnames,
2550Sstevel@tonic-gate 				IST_SZ(paramnames),	"paramprob" },
2560Sstevel@tonic-gate 	{ ICMP_TSTAMP,		NULL,	0,		"timestamp" },
2570Sstevel@tonic-gate 	{ ICMP_TSTAMPREPLY,	NULL,	0,		"timestampreply" },
2580Sstevel@tonic-gate 	{ ICMP_IREQ,		NULL,	0,		"inforeq" },
2590Sstevel@tonic-gate 	{ ICMP_IREQREPLY,	NULL,	0,		"inforeply" },
2600Sstevel@tonic-gate 	{ ICMP_MASKREQ,		NULL,	0,		"maskreq" },
2610Sstevel@tonic-gate 	{ ICMP_MASKREPLY,	NULL,	0,		"maskreply" },
2620Sstevel@tonic-gate 	{ -2,			NULL,	0,		NULL }
2630Sstevel@tonic-gate };
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static icmp_subtype_t icmpredirect6[] = {
2660Sstevel@tonic-gate 	{ ICMP6_DST_UNREACH_NOROUTE,		"noroute" },
2670Sstevel@tonic-gate 	{ ICMP6_DST_UNREACH_ADMIN,		"admin" },
2680Sstevel@tonic-gate 	{ ICMP6_DST_UNREACH_NOTNEIGHBOR,	"neighbour" },
2690Sstevel@tonic-gate 	{ ICMP6_DST_UNREACH_ADDR,		"address" },
2700Sstevel@tonic-gate 	{ ICMP6_DST_UNREACH_NOPORT,		"noport" },
2710Sstevel@tonic-gate 	{ -2,					NULL }
2720Sstevel@tonic-gate };
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate static icmp_subtype_t icmptimexceed6[] = {
2750Sstevel@tonic-gate 	{ ICMP6_TIME_EXCEED_TRANSIT,		"intransit" },
2760Sstevel@tonic-gate 	{ ICMP6_TIME_EXCEED_REASSEMBLY,		"reassem" },
2770Sstevel@tonic-gate 	{ -2,					NULL }
2780Sstevel@tonic-gate };
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate static icmp_subtype_t icmpparamprob6[] = {
2810Sstevel@tonic-gate 	{ ICMP6_PARAMPROB_HEADER,		"header" },
2820Sstevel@tonic-gate 	{ ICMP6_PARAMPROB_NEXTHEADER,		"nextheader" },
2830Sstevel@tonic-gate 	{ ICMP6_PARAMPROB_OPTION,		"option" },
2840Sstevel@tonic-gate 	{ -2,					NULL }
2850Sstevel@tonic-gate };
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate static icmp_subtype_t icmpquerysubject6[] = {
2880Sstevel@tonic-gate 	{ ICMP6_NI_SUBJ_IPV6,			"ipv6" },
2890Sstevel@tonic-gate 	{ ICMP6_NI_SUBJ_FQDN,			"fqdn" },
2900Sstevel@tonic-gate 	{ ICMP6_NI_SUBJ_IPV4,			"ipv4" },
2910Sstevel@tonic-gate 	{ -2,					NULL },
2920Sstevel@tonic-gate };
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate static icmp_subtype_t icmpnodeinfo6[] = {
2950Sstevel@tonic-gate 	{ ICMP6_NI_SUCCESS,			"success" },
2960Sstevel@tonic-gate 	{ ICMP6_NI_REFUSED,			"refused" },
2970Sstevel@tonic-gate 	{ ICMP6_NI_UNKNOWN,			"unknown" },
2980Sstevel@tonic-gate 	{ -2,					NULL }
2990Sstevel@tonic-gate };
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static icmp_subtype_t icmprenumber6[] = {
3020Sstevel@tonic-gate 	{ ICMP6_ROUTER_RENUMBERING_COMMAND,		"command" },
3030Sstevel@tonic-gate 	{ ICMP6_ROUTER_RENUMBERING_RESULT,		"result" },
3040Sstevel@tonic-gate 	{ ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET,	"seqnum_reset" },
3050Sstevel@tonic-gate 	{ -2,						NULL }
3060Sstevel@tonic-gate };
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate static icmp_type_t icmptypes6[] = {
3090Sstevel@tonic-gate 	{ 0,			NULL,	0,		NULL },
3100Sstevel@tonic-gate 	{ ICMP6_DST_UNREACH,	icmpredirect6,
3110Sstevel@tonic-gate 			IST_SZ(icmpredirect6),		"unreach" },
3120Sstevel@tonic-gate 	{ ICMP6_PACKET_TOO_BIG,	NULL,	0,		"toobig" },
3130Sstevel@tonic-gate 	{ ICMP6_TIME_EXCEEDED,	icmptimexceed6,
3140Sstevel@tonic-gate 			IST_SZ(icmptimexceed6),		"timxceed" },
3150Sstevel@tonic-gate 	{ ICMP6_PARAM_PROB,	icmpparamprob6,
3160Sstevel@tonic-gate 			IST_SZ(icmpparamprob6),		"paramprob" },
3170Sstevel@tonic-gate 	{ ICMP6_ECHO_REQUEST,	NULL,	0,		"echo" },
3180Sstevel@tonic-gate 	{ ICMP6_ECHO_REPLY,	NULL,	0,		"echoreply" },
3190Sstevel@tonic-gate 	{ ICMP6_MEMBERSHIP_QUERY, icmpquerysubject6,
3200Sstevel@tonic-gate 			IST_SZ(icmpquerysubject6),	"groupmemberquery" },
3210Sstevel@tonic-gate 	{ ICMP6_MEMBERSHIP_REPORT,NULL,	0,		"groupmemberreport" },
3220Sstevel@tonic-gate 	{ ICMP6_MEMBERSHIP_REDUCTION,NULL,	0,	"groupmemberterm" },
3230Sstevel@tonic-gate 	{ ND_ROUTER_SOLICIT,	NULL,	0,		"routersolicit" },
3240Sstevel@tonic-gate 	{ ND_ROUTER_ADVERT,	NULL,	0,		"routeradvert" },
3250Sstevel@tonic-gate 	{ ND_NEIGHBOR_SOLICIT,	NULL,	0,		"neighborsolicit" },
3260Sstevel@tonic-gate 	{ ND_NEIGHBOR_ADVERT,	NULL,	0,		"neighboradvert" },
3270Sstevel@tonic-gate 	{ ND_REDIRECT,		NULL,	0,		"redirect" },
3280Sstevel@tonic-gate 	{ ICMP6_ROUTER_RENUMBERING,	icmprenumber6,
3290Sstevel@tonic-gate 			IST_SZ(icmprenumber6),		"routerrenumber" },
3300Sstevel@tonic-gate 	{ ICMP6_WRUREQUEST,	NULL,	0,		"whoareyourequest" },
3310Sstevel@tonic-gate 	{ ICMP6_WRUREPLY,	NULL,	0,		"whoareyoureply" },
3320Sstevel@tonic-gate 	{ ICMP6_FQDN_QUERY,	NULL,	0,		"fqdnquery" },
3330Sstevel@tonic-gate 	{ ICMP6_FQDN_REPLY,	NULL,	0,		"fqdnreply" },
3340Sstevel@tonic-gate 	{ ICMP6_NI_QUERY,	icmpnodeinfo6,
3350Sstevel@tonic-gate 			IST_SZ(icmpnodeinfo6),		"nodeinforequest" },
3360Sstevel@tonic-gate 	{ ICMP6_NI_REPLY,	NULL,	0,		"nodeinforeply" },
3370Sstevel@tonic-gate 	{ MLD6_MTRACE_RESP,	NULL,	0,		"mtraceresponse" },
3380Sstevel@tonic-gate 	{ MLD6_MTRACE,		NULL,	0,		"mtracerequest" },
3390Sstevel@tonic-gate 	{ -2,			NULL,	0,		NULL }
3400Sstevel@tonic-gate };
3410Sstevel@tonic-gate 
find_icmpsubtype(type,table,tablesz)3420Sstevel@tonic-gate static icmp_subtype_t *find_icmpsubtype(type, table, tablesz)
3430Sstevel@tonic-gate int type;
3440Sstevel@tonic-gate icmp_subtype_t *table;
3450Sstevel@tonic-gate size_t tablesz;
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	icmp_subtype_t *ist;
3480Sstevel@tonic-gate 	int i;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (tablesz < 2)
3510Sstevel@tonic-gate 		return NULL;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if ((type < 0) || (type > table[tablesz - 2].ist_val))
3540Sstevel@tonic-gate 		return NULL;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	i = type;
3570Sstevel@tonic-gate 	if (table[type].ist_val == type)
3580Sstevel@tonic-gate 		return table + type;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	for (i = 0, ist = table; ist->ist_val != -2; i++, ist++)
3610Sstevel@tonic-gate 		if (ist->ist_val == type)
3620Sstevel@tonic-gate 			return ist;
3630Sstevel@tonic-gate 	return NULL;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 
find_icmptype(type,table,tablesz)3670Sstevel@tonic-gate static icmp_type_t *find_icmptype(type, table, tablesz)
3680Sstevel@tonic-gate int type;
3690Sstevel@tonic-gate icmp_type_t *table;
3700Sstevel@tonic-gate size_t tablesz;
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	icmp_type_t *it;
3730Sstevel@tonic-gate 	int i;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	if (tablesz < 2)
3760Sstevel@tonic-gate 		return NULL;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if ((type < 0) || (type > table[tablesz - 2].it_val))
3790Sstevel@tonic-gate 		return NULL;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	i = type;
3820Sstevel@tonic-gate 	if (table[type].it_val == type)
3830Sstevel@tonic-gate 		return table + type;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	for (i = 0, it = table; it->it_val != -2; i++, it++)
3860Sstevel@tonic-gate 		if (it->it_val == type)
3870Sstevel@tonic-gate 			return it;
3880Sstevel@tonic-gate 	return NULL;
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 
handlehup(sig)3920Sstevel@tonic-gate static void handlehup(sig)
3930Sstevel@tonic-gate int sig;
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	signal(SIGHUP, handlehup);
3960Sstevel@tonic-gate 	donehup = 1;
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 
init_tabs()4000Sstevel@tonic-gate static void init_tabs()
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	struct	protoent	*p;
4030Sstevel@tonic-gate 	struct	servent	*s;
4040Sstevel@tonic-gate 	char	*name, **tab;
4050Sstevel@tonic-gate 	int	port, i;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (protocols != NULL) {
4080Sstevel@tonic-gate 		for (i = 0; i < 256; i++)
4090Sstevel@tonic-gate 			if (protocols[i] != NULL) {
4100Sstevel@tonic-gate 				free(protocols[i]);
4110Sstevel@tonic-gate 				protocols[i] = NULL;
4120Sstevel@tonic-gate 			}
4130Sstevel@tonic-gate 		free(protocols);
4140Sstevel@tonic-gate 		protocols = NULL;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	protocols = (char **)malloc(256 * sizeof(*protocols));
4170Sstevel@tonic-gate 	if (protocols != NULL) {
4180Sstevel@tonic-gate 		bzero((char *)protocols, 256 * sizeof(*protocols));
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		setprotoent(1);
4210Sstevel@tonic-gate 		while ((p = getprotoent()) != NULL)
4220Sstevel@tonic-gate 			if (p->p_proto >= 0 && p->p_proto <= 255 &&
4230Sstevel@tonic-gate 			    p->p_name != NULL && protocols[p->p_proto] == NULL)
4240Sstevel@tonic-gate 				protocols[p->p_proto] = strdup(p->p_name);
4250Sstevel@tonic-gate 		endprotoent();
4262393Syz155240 #if defined(_AIX51)
4272393Syz155240 		if (protocols[0])
4282393Syz155240 			free(protocols[0]);
4292393Syz155240 		if (protocols[252])
4302393Syz155240 			free(protocols[252]);
4312393Syz155240 		protocols[0] = "ip";
4322393Syz155240 		protocols[252] = NULL;
4332393Syz155240 #endif
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (udp_ports != NULL) {
4370Sstevel@tonic-gate 		for (i = 0; i < 65536; i++)
4380Sstevel@tonic-gate 			if (udp_ports[i] != NULL) {
4390Sstevel@tonic-gate 				free(udp_ports[i]);
4400Sstevel@tonic-gate 				udp_ports[i] = NULL;
4410Sstevel@tonic-gate 			}
4420Sstevel@tonic-gate 		free(udp_ports);
4430Sstevel@tonic-gate 		udp_ports = NULL;
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	udp_ports = (char **)malloc(65536 * sizeof(*udp_ports));
4460Sstevel@tonic-gate 	if (udp_ports != NULL)
4470Sstevel@tonic-gate 		bzero((char *)udp_ports, 65536 * sizeof(*udp_ports));
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if (tcp_ports != NULL) {
4500Sstevel@tonic-gate 		for (i = 0; i < 65536; i++)
4510Sstevel@tonic-gate 			if (tcp_ports[i] != NULL) {
4520Sstevel@tonic-gate 				free(tcp_ports[i]);
4530Sstevel@tonic-gate 				tcp_ports[i] = NULL;
4540Sstevel@tonic-gate 			}
4550Sstevel@tonic-gate 		free(tcp_ports);
4560Sstevel@tonic-gate 		tcp_ports = NULL;
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 	tcp_ports = (char **)malloc(65536 * sizeof(*tcp_ports));
4590Sstevel@tonic-gate 	if (tcp_ports != NULL)
4600Sstevel@tonic-gate 		bzero((char *)tcp_ports, 65536 * sizeof(*tcp_ports));
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	setservent(1);
4630Sstevel@tonic-gate 	while ((s = getservent()) != NULL) {
4640Sstevel@tonic-gate 		if (s->s_proto == NULL)
4650Sstevel@tonic-gate 			continue;
4660Sstevel@tonic-gate 		else if (!strcmp(s->s_proto, "tcp")) {
4670Sstevel@tonic-gate 			port = ntohs(s->s_port);
4680Sstevel@tonic-gate 			name = s->s_name;
4690Sstevel@tonic-gate 			tab = tcp_ports;
4700Sstevel@tonic-gate 		} else if (!strcmp(s->s_proto, "udp")) {
4710Sstevel@tonic-gate 			port = ntohs(s->s_port);
4720Sstevel@tonic-gate 			name = s->s_name;
4730Sstevel@tonic-gate 			tab = udp_ports;
4740Sstevel@tonic-gate 		} else
4750Sstevel@tonic-gate 			continue;
4760Sstevel@tonic-gate 		if ((port < 0 || port > 65535) || (name == NULL))
4770Sstevel@tonic-gate 			continue;
4780Sstevel@tonic-gate 		if (tab != NULL)
4790Sstevel@tonic-gate 			tab[port] = strdup(name);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 	endservent();
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 
getproto(p)4850Sstevel@tonic-gate static char *getproto(p)
4860Sstevel@tonic-gate u_int p;
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	static char pnum[4];
4890Sstevel@tonic-gate 	char *s;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	p &= 0xff;
4920Sstevel@tonic-gate 	s = protocols ? protocols[p] : NULL;
4930Sstevel@tonic-gate 	if (s == NULL) {
4940Sstevel@tonic-gate 		sprintf(pnum, "%u", p);
4950Sstevel@tonic-gate 		s = pnum;
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 	return s;
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 
read_log(fd,lenp,buf,bufsize)5010Sstevel@tonic-gate static int read_log(fd, lenp, buf, bufsize)
5020Sstevel@tonic-gate int fd, bufsize, *lenp;
5030Sstevel@tonic-gate char *buf;
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	int	nr;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	nr = read(fd, buf, bufsize);
5080Sstevel@tonic-gate 	if (!nr)
5090Sstevel@tonic-gate 		return 2;
5100Sstevel@tonic-gate 	if ((nr < 0) && (errno != EINTR))
5110Sstevel@tonic-gate 		return -1;
5120Sstevel@tonic-gate 	*lenp = nr;
5130Sstevel@tonic-gate 	return 0;
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 
hostname(res,v,ip)5170Sstevel@tonic-gate char	*hostname(res, v, ip)
5180Sstevel@tonic-gate int	res, v;
5190Sstevel@tonic-gate u_32_t	*ip;
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate # define MAX_INETA	16
5220Sstevel@tonic-gate 	static char hname[MAXHOSTNAMELEN + MAX_INETA + 3];
5230Sstevel@tonic-gate #ifdef	USE_INET6
5240Sstevel@tonic-gate 	static char hostbuf[MAXHOSTNAMELEN+1];
5250Sstevel@tonic-gate #endif
5260Sstevel@tonic-gate 	struct hostent *hp;
5270Sstevel@tonic-gate 	struct in_addr ipa;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (v == 4) {
5300Sstevel@tonic-gate 		ipa.s_addr = *ip;
5310Sstevel@tonic-gate 		if (!res)
5320Sstevel@tonic-gate 			return inet_ntoa(ipa);
5330Sstevel@tonic-gate 		hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
5340Sstevel@tonic-gate 		if (!hp)
5350Sstevel@tonic-gate 			return inet_ntoa(ipa);
5360Sstevel@tonic-gate 		sprintf(hname, "%.*s[%s]", MAXHOSTNAMELEN, hp->h_name,
5370Sstevel@tonic-gate 			inet_ntoa(ipa));
5380Sstevel@tonic-gate 		return hname;
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate #ifdef	USE_INET6
5410Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
5420Sstevel@tonic-gate 	hostbuf[MAXHOSTNAMELEN] = '\0';
5430Sstevel@tonic-gate 	return hostbuf;
5440Sstevel@tonic-gate #else
5450Sstevel@tonic-gate 	return "IPv6";
5460Sstevel@tonic-gate #endif
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 
portname(res,proto,port)5500Sstevel@tonic-gate char	*portname(res, proto, port)
5510Sstevel@tonic-gate int	res;
5520Sstevel@tonic-gate char	*proto;
5530Sstevel@tonic-gate u_int	port;
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	static	char	pname[8];
5560Sstevel@tonic-gate 	char	*s;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	port = ntohs(port);
5590Sstevel@tonic-gate 	port &= 0xffff;
5600Sstevel@tonic-gate 	(void) sprintf(pname, "%u", port);
5610Sstevel@tonic-gate 	if (!res || (opts & OPT_PORTNUM))
5620Sstevel@tonic-gate 		return pname;
5630Sstevel@tonic-gate 	s = NULL;
5640Sstevel@tonic-gate 	if (!strcmp(proto, "tcp"))
5650Sstevel@tonic-gate 		s = tcp_ports[port];
5660Sstevel@tonic-gate 	else if (!strcmp(proto, "udp"))
5670Sstevel@tonic-gate 		s = udp_ports[port];
5680Sstevel@tonic-gate 	if (s == NULL)
5690Sstevel@tonic-gate 		s = pname;
5700Sstevel@tonic-gate 	return s;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 
icmpname(type,code)5740Sstevel@tonic-gate static	char	*icmpname(type, code)
5750Sstevel@tonic-gate u_int	type;
5760Sstevel@tonic-gate u_int	code;
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	static char name[80];
5790Sstevel@tonic-gate 	icmp_subtype_t *ist;
5800Sstevel@tonic-gate 	icmp_type_t *it;
5810Sstevel@tonic-gate 	char *s;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	s = NULL;
5840Sstevel@tonic-gate 	it = find_icmptype(type, icmptypes, sizeof(icmptypes) / sizeof(*it));
5850Sstevel@tonic-gate 	if (it != NULL)
5860Sstevel@tonic-gate 		s = it->it_name;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if (s == NULL)
5890Sstevel@tonic-gate 		sprintf(name, "icmptype(%d)/", type);
5900Sstevel@tonic-gate 	else
5910Sstevel@tonic-gate 		sprintf(name, "%s/", s);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	ist = NULL;
5940Sstevel@tonic-gate 	if (it != NULL && it->it_subtable != NULL)
5950Sstevel@tonic-gate 		ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (ist != NULL && ist->ist_name != NULL)
5980Sstevel@tonic-gate 		strcat(name, ist->ist_name);
5990Sstevel@tonic-gate 	else
6000Sstevel@tonic-gate 		sprintf(name + strlen(name), "%d", code);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	return name;
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
icmpname6(type,code)6050Sstevel@tonic-gate static	char	*icmpname6(type, code)
6060Sstevel@tonic-gate u_int	type;
6070Sstevel@tonic-gate u_int	code;
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	static char name[80];
6100Sstevel@tonic-gate 	icmp_subtype_t *ist;
6110Sstevel@tonic-gate 	icmp_type_t *it;
6120Sstevel@tonic-gate 	char *s;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	s = NULL;
6150Sstevel@tonic-gate 	it = find_icmptype(type, icmptypes6, sizeof(icmptypes6) / sizeof(*it));
6160Sstevel@tonic-gate 	if (it != NULL)
6170Sstevel@tonic-gate 		s = it->it_name;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	if (s == NULL)
6200Sstevel@tonic-gate 		sprintf(name, "icmpv6type(%d)/", type);
6210Sstevel@tonic-gate 	else
6220Sstevel@tonic-gate 		sprintf(name, "%s/", s);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	ist = NULL;
6250Sstevel@tonic-gate 	if (it != NULL && it->it_subtable != NULL)
6260Sstevel@tonic-gate 		ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (ist != NULL && ist->ist_name != NULL)
6290Sstevel@tonic-gate 		strcat(name, ist->ist_name);
6300Sstevel@tonic-gate 	else
6310Sstevel@tonic-gate 		sprintf(name + strlen(name), "%d", code);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	return name;
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 
dumphex(log,dopts,buf,len)6370Sstevel@tonic-gate void	dumphex(log, dopts, buf, len)
6380Sstevel@tonic-gate FILE	*log;
6390Sstevel@tonic-gate int	dopts;
6400Sstevel@tonic-gate char	*buf;
6410Sstevel@tonic-gate int	len;
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 	char	hline[80];
6440Sstevel@tonic-gate 	int	i, j, k;
6450Sstevel@tonic-gate 	u_char	*s = (u_char *)buf, *t = (u_char *)hline;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (buf == NULL || len == 0)
6480Sstevel@tonic-gate 		return;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	*hline = '\0';
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	for (i = len, j = 0; i; i--, j++, s++) {
6530Sstevel@tonic-gate 		if (j && !(j & 0xf)) {
6540Sstevel@tonic-gate 			*t++ = '\n';
6550Sstevel@tonic-gate 			*t = '\0';
6560Sstevel@tonic-gate 			if (!(dopts & OPT_SYSLOG))
6570Sstevel@tonic-gate 				fputs(hline, log);
6580Sstevel@tonic-gate 			else
6590Sstevel@tonic-gate 				syslog(LOG_INFO, "%s", hline);
6600Sstevel@tonic-gate 			t = (u_char *)hline;
6610Sstevel@tonic-gate 			*t = '\0';
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 		sprintf((char *)t, "%02x", *s & 0xff);
6640Sstevel@tonic-gate 		t += 2;
6650Sstevel@tonic-gate 		if (!((j + 1) & 0xf)) {
6660Sstevel@tonic-gate 			s -= 15;
6670Sstevel@tonic-gate 			sprintf((char *)t, "        ");
6680Sstevel@tonic-gate 			t += 8;
6690Sstevel@tonic-gate 			for (k = 16; k; k--, s++)
6702393Syz155240 				*t++ = (ISPRINT(*s) ? *s : '.');
6710Sstevel@tonic-gate 			s--;
6720Sstevel@tonic-gate 		}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		if ((j + 1) & 0xf)
6750Sstevel@tonic-gate 			*t++ = ' ';;
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	if (j & 0xf) {
6790Sstevel@tonic-gate 		for (k = 16 - (j & 0xf); k; k--) {
6800Sstevel@tonic-gate 			*t++ = ' ';
6810Sstevel@tonic-gate 			*t++ = ' ';
6820Sstevel@tonic-gate 			*t++ = ' ';
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 		sprintf((char *)t, "       ");
6850Sstevel@tonic-gate 		t += 7;
6860Sstevel@tonic-gate 		s -= j & 0xf;
6870Sstevel@tonic-gate 		for (k = j & 0xf; k; k--, s++)
6882393Syz155240 			*t++ = (ISPRINT(*s) ? *s : '.');
6890Sstevel@tonic-gate 		*t++ = '\n';
6900Sstevel@tonic-gate 		*t = '\0';
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 	if (!(dopts & OPT_SYSLOG)) {
6930Sstevel@tonic-gate 		fputs(hline, log);
6940Sstevel@tonic-gate 		fflush(log);
6950Sstevel@tonic-gate 	} else
6960Sstevel@tonic-gate 		syslog(LOG_INFO, "%s", hline);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 
get_tm(sec)7000Sstevel@tonic-gate static	struct	tm	*get_tm(sec)
7010Sstevel@tonic-gate #ifdef __hpux
7020Sstevel@tonic-gate u_32_t	sec;
7030Sstevel@tonic-gate #else
7040Sstevel@tonic-gate time_t	sec;
7050Sstevel@tonic-gate #endif
7060Sstevel@tonic-gate {
7070Sstevel@tonic-gate 	struct tm *tm;
7080Sstevel@tonic-gate 	time_t t;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	t = sec;
7110Sstevel@tonic-gate 	tm = localtime(&t);
7120Sstevel@tonic-gate 	return tm;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
print_natlog(log,buf,blen)7150Sstevel@tonic-gate static	void	print_natlog(log, buf, blen)
7160Sstevel@tonic-gate FILE	*log;
7170Sstevel@tonic-gate char	*buf;
7180Sstevel@tonic-gate int	blen;
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	struct	natlog	*nl;
7210Sstevel@tonic-gate 	iplog_t	*ipl = (iplog_t *)buf;
7220Sstevel@tonic-gate 	char	*t = line;
7230Sstevel@tonic-gate 	struct	tm	*tm;
7240Sstevel@tonic-gate 	int	res, i, len;
7250Sstevel@tonic-gate 	char	*proto;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	nl = (struct natlog *)((char *)ipl + sizeof(*ipl));
7280Sstevel@tonic-gate 	res = (opts & OPT_RESOLVE) ? 1 : 0;
7290Sstevel@tonic-gate 	tm = get_tm(ipl->ipl_sec);
7300Sstevel@tonic-gate 	len = sizeof(line);
7310Sstevel@tonic-gate 	if (!(opts & OPT_SYSLOG)) {
7320Sstevel@tonic-gate 		(void) strftime(t, len, "%d/%m/%Y ", tm);
7330Sstevel@tonic-gate 		i = strlen(t);
7340Sstevel@tonic-gate 		len -= i;
7350Sstevel@tonic-gate 		t += i;
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 	(void) strftime(t, len, "%T", tm);
7380Sstevel@tonic-gate 	t += strlen(t);
7397176Syx160601 	(void) sprintf(t, ".%-.6ld @%hd ", ipl->ipl_usec, nl->nlg_rule + 1);
7400Sstevel@tonic-gate 	t += strlen(t);
7410Sstevel@tonic-gate 
7427176Syx160601 	if (nl->nlg_type == NL_NEWMAP)
7430Sstevel@tonic-gate 		strcpy(t, "NAT:MAP ");
7447176Syx160601 	else if (nl->nlg_type == NL_NEWRDR)
7450Sstevel@tonic-gate 		strcpy(t, "NAT:RDR ");
7467176Syx160601 	else if (nl->nlg_type == NL_FLUSH)
7470Sstevel@tonic-gate 		strcpy(t, "NAT:FLUSH ");
7487176Syx160601 	else if (nl->nlg_type == NL_EXPIRE)
7490Sstevel@tonic-gate 		strcpy(t, "NAT:EXPIRE ");
7507176Syx160601 	else if (nl->nlg_type == NL_NEWBIMAP)
7510Sstevel@tonic-gate 		strcpy(t, "NAT:BIMAP ");
7527176Syx160601 	else if (nl->nlg_type == NL_NEWBLOCK)
7530Sstevel@tonic-gate 		strcpy(t, "NAT:MAPBLOCK ");
7547176Syx160601 	else if (nl->nlg_type == NL_CLONE)
7550Sstevel@tonic-gate 		strcpy(t, "NAT:CLONE ");
7560Sstevel@tonic-gate 	else
7577176Syx160601 		sprintf(t, "Type: %d ", nl->nlg_type);
7580Sstevel@tonic-gate 	t += strlen(t);
7590Sstevel@tonic-gate 
7607176Syx160601 	proto = getproto(nl->nlg_p);
7610Sstevel@tonic-gate 
7627176Syx160601 	(void) sprintf(t, "%s,%s <- -> ", hostname(res, nl->nlg_v,
7637176Syx160601 		(u_32_t *)&nl->nlg_inip),
7647176Syx160601 		portname(res, proto, (u_int)nl->nlg_inport));
7650Sstevel@tonic-gate 	t += strlen(t);
7667176Syx160601 	(void) sprintf(t, "%s,%s ", hostname(res, nl->nlg_v,
7677176Syx160601 		(u_32_t *)&nl->nlg_outip),
7687176Syx160601 		portname(res, proto, (u_int)nl->nlg_outport));
7690Sstevel@tonic-gate 	t += strlen(t);
7707176Syx160601 	(void) sprintf(t, "[%s,%s]", hostname(res, nl->nlg_v,
7717176Syx160601 		(u_32_t *)&nl->nlg_origip),
7727176Syx160601 		portname(res, proto, (u_int)nl->nlg_origport));
7730Sstevel@tonic-gate 	t += strlen(t);
7747176Syx160601 	if (nl->nlg_type == NL_EXPIRE) {
7750Sstevel@tonic-gate #ifdef	USE_QUAD_T
7760Sstevel@tonic-gate 		(void) sprintf(t, " Pkts %qd/%qd Bytes %qd/%qd",
7777176Syx160601 				(long long)nl->nlg_pkts[0],
7787176Syx160601 				(long long)nl->nlg_pkts[1],
7797176Syx160601 				(long long)nl->nlg_bytes[0],
7807176Syx160601 				(long long)nl->nlg_bytes[1]);
7810Sstevel@tonic-gate #else
7822393Syz155240 		(void) sprintf(t, " Pkts %ld/%ld Bytes %ld/%ld",
7837176Syx160601 				nl->nlg_pkts[0], nl->nlg_pkts[1],
7847176Syx160601 				nl->nlg_bytes[0], nl->nlg_bytes[1]);
7850Sstevel@tonic-gate #endif
7860Sstevel@tonic-gate 		t += strlen(t);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	*t++ = '\n';
7900Sstevel@tonic-gate 	*t++ = '\0';
7910Sstevel@tonic-gate 	if (opts & OPT_SYSLOG)
7920Sstevel@tonic-gate 		syslog(LOG_INFO, "%s", line);
7930Sstevel@tonic-gate 	else
7940Sstevel@tonic-gate 		(void) fprintf(log, "%s", line);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 
print_statelog(log,buf,blen)7980Sstevel@tonic-gate static	void	print_statelog(log, buf, blen)
7990Sstevel@tonic-gate FILE	*log;
8000Sstevel@tonic-gate char	*buf;
8010Sstevel@tonic-gate int	blen;
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 	struct	ipslog *sl;
8040Sstevel@tonic-gate 	iplog_t	*ipl = (iplog_t *)buf;
8050Sstevel@tonic-gate 	char	*t = line, *proto;
8060Sstevel@tonic-gate 	struct	tm	*tm;
8070Sstevel@tonic-gate 	int	res, i, len;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	sl = (struct ipslog *)((char *)ipl + sizeof(*ipl));
8100Sstevel@tonic-gate 	res = (opts & OPT_RESOLVE) ? 1 : 0;
8110Sstevel@tonic-gate 	tm = get_tm(ipl->ipl_sec);
8120Sstevel@tonic-gate 	len = sizeof(line);
8130Sstevel@tonic-gate 	if (!(opts & OPT_SYSLOG)) {
8140Sstevel@tonic-gate 		(void) strftime(t, len, "%d/%m/%Y ", tm);
8150Sstevel@tonic-gate 		i = strlen(t);
8160Sstevel@tonic-gate 		len -= i;
8170Sstevel@tonic-gate 		t += i;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	(void) strftime(t, len, "%T", tm);
8200Sstevel@tonic-gate 	t += strlen(t);
8210Sstevel@tonic-gate 	(void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
8220Sstevel@tonic-gate 	t += strlen(t);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if (sl->isl_type == ISL_NEW)
8250Sstevel@tonic-gate 		strcpy(t, "STATE:NEW ");
8260Sstevel@tonic-gate 	else if (sl->isl_type == ISL_CLONE)
8270Sstevel@tonic-gate 		strcpy(t, "STATE:CLONED ");
8280Sstevel@tonic-gate 	else if (sl->isl_type == ISL_EXPIRE) {
8290Sstevel@tonic-gate 		if ((sl->isl_p == IPPROTO_TCP) &&
8300Sstevel@tonic-gate 		    (sl->isl_state[0] > IPF_TCPS_ESTABLISHED ||
8310Sstevel@tonic-gate 		     sl->isl_state[1] > IPF_TCPS_ESTABLISHED))
8320Sstevel@tonic-gate 			strcpy(t, "STATE:CLOSE ");
8330Sstevel@tonic-gate 		else
8340Sstevel@tonic-gate 			strcpy(t, "STATE:EXPIRE ");
8350Sstevel@tonic-gate 	} else if (sl->isl_type == ISL_FLUSH)
8360Sstevel@tonic-gate 		strcpy(t, "STATE:FLUSH ");
8370Sstevel@tonic-gate 	else if (sl->isl_type == ISL_INTERMEDIATE)
8380Sstevel@tonic-gate 		strcpy(t, "STATE:INTERMEDIATE ");
8390Sstevel@tonic-gate 	else if (sl->isl_type == ISL_REMOVE)
8400Sstevel@tonic-gate 		strcpy(t, "STATE:REMOVE ");
8410Sstevel@tonic-gate 	else if (sl->isl_type == ISL_KILLED)
8420Sstevel@tonic-gate 		strcpy(t, "STATE:KILLED ");
8430Sstevel@tonic-gate 	else
8440Sstevel@tonic-gate 		sprintf(t, "Type: %d ", sl->isl_type);
8450Sstevel@tonic-gate 	t += strlen(t);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	proto = getproto(sl->isl_p);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	if (sl->isl_p == IPPROTO_TCP || sl->isl_p == IPPROTO_UDP) {
8500Sstevel@tonic-gate 		(void) sprintf(t, "%s,%s -> ",
8510Sstevel@tonic-gate 			hostname(res, sl->isl_v, (u_32_t *)&sl->isl_src),
8520Sstevel@tonic-gate 			portname(res, proto, (u_int)sl->isl_sport));
8530Sstevel@tonic-gate 		t += strlen(t);
8540Sstevel@tonic-gate 		(void) sprintf(t, "%s,%s PR %s",
8550Sstevel@tonic-gate 			hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
8560Sstevel@tonic-gate 			portname(res, proto, (u_int)sl->isl_dport), proto);
8570Sstevel@tonic-gate 	} else if (sl->isl_p == IPPROTO_ICMP) {
8580Sstevel@tonic-gate 		(void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
8590Sstevel@tonic-gate 						     (u_32_t *)&sl->isl_src));
8600Sstevel@tonic-gate 		t += strlen(t);
8610Sstevel@tonic-gate 		(void) sprintf(t, "%s PR icmp %d",
8620Sstevel@tonic-gate 			hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
8630Sstevel@tonic-gate 			sl->isl_itype);
8640Sstevel@tonic-gate 	} else if (sl->isl_p == IPPROTO_ICMPV6) {
8650Sstevel@tonic-gate 		(void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
8660Sstevel@tonic-gate 						     (u_32_t *)&sl->isl_src));
8670Sstevel@tonic-gate 		t += strlen(t);
8680Sstevel@tonic-gate 		(void) sprintf(t, "%s PR icmpv6 %d",
8690Sstevel@tonic-gate 			hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
8700Sstevel@tonic-gate 			sl->isl_itype);
8712393Syz155240 	} else {
8722393Syz155240 		(void) sprintf(t, "%s -> ",
8732393Syz155240 			hostname(res, sl->isl_v, (u_32_t *)&sl->isl_src));
8742393Syz155240 		t += strlen(t);
8752393Syz155240 		(void) sprintf(t, "%s PR %s",
8762393Syz155240 			hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
8772393Syz155240 			proto);
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 	t += strlen(t);
8800Sstevel@tonic-gate 	if (sl->isl_tag != FR_NOLOGTAG) {
8810Sstevel@tonic-gate 		(void) sprintf(t, " tag %u", sl->isl_tag);
8820Sstevel@tonic-gate 		t += strlen(t);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 	if (sl->isl_type != ISL_NEW) {
8850Sstevel@tonic-gate 		sprintf(t,
8860Sstevel@tonic-gate #ifdef	USE_QUAD_T
8872393Syz155240 #ifdef	PRId64
8882393Syz155240 			" Forward: Pkts in %" PRId64 " Bytes in %" PRId64
8892393Syz155240 			" Pkts out %" PRId64 " Bytes out %" PRId64
8902393Syz155240 			" Backward: Pkts in %" PRId64 " Bytes in %" PRId64
8912393Syz155240 			" Pkts out %" PRId64 " Bytes out %" PRId64,
8922393Syz155240 #else
8930Sstevel@tonic-gate 			" Forward: Pkts in %qd Bytes in %qd Pkts out %qd Bytes out %qd Backward: Pkts in %qd Bytes in %qd Pkts out %qd Bytes out %qd",
8942393Syz155240 #endif /* PRId64 */
8950Sstevel@tonic-gate #else
8960Sstevel@tonic-gate 			" Forward: Pkts in %ld Bytes in %ld Pkts out %ld Bytes out %ld Backward: Pkts in %ld Bytes in %ld Pkts out %ld Bytes out %ld",
8970Sstevel@tonic-gate #endif
8980Sstevel@tonic-gate 			sl->isl_pkts[0], sl->isl_bytes[0],
8990Sstevel@tonic-gate 			sl->isl_pkts[1], sl->isl_bytes[1],
9000Sstevel@tonic-gate 			sl->isl_pkts[2], sl->isl_bytes[2],
9010Sstevel@tonic-gate 			sl->isl_pkts[3], sl->isl_bytes[3]);
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 		t += strlen(t);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	*t++ = '\n';
9070Sstevel@tonic-gate 	*t++ = '\0';
9080Sstevel@tonic-gate 	if (opts & OPT_SYSLOG)
9090Sstevel@tonic-gate 		syslog(LOG_INFO, "%s", line);
9100Sstevel@tonic-gate 	else
9110Sstevel@tonic-gate 		(void) fprintf(log, "%s", line);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 
print_log(logtype,log,buf,blen)9150Sstevel@tonic-gate static	void	print_log(logtype, log, buf, blen)
9160Sstevel@tonic-gate FILE	*log;
9170Sstevel@tonic-gate char	*buf;
9180Sstevel@tonic-gate int	logtype, blen;
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	iplog_t	*ipl;
9210Sstevel@tonic-gate 	char *bp = NULL, *bpo = NULL;
9220Sstevel@tonic-gate 	int psize;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	while (blen > 0) {
9250Sstevel@tonic-gate 		ipl = (iplog_t *)buf;
9260Sstevel@tonic-gate 		if ((u_long)ipl & (sizeof(long)-1)) {
9270Sstevel@tonic-gate 			if (bp)
9280Sstevel@tonic-gate 				bpo = bp;
9290Sstevel@tonic-gate 			bp = (char *)malloc(blen);
9301448Sschuster 			if (bp == NULL) {
9311448Sschuster 				perror("malloc");
9321448Sschuster 				exit(1);
9331448Sschuster 			}
9340Sstevel@tonic-gate 			bcopy((char *)ipl, bp, blen);
9350Sstevel@tonic-gate 			if (bpo) {
9360Sstevel@tonic-gate 				free(bpo);
9370Sstevel@tonic-gate 				bpo = NULL;
9380Sstevel@tonic-gate 			}
9390Sstevel@tonic-gate 			buf = bp;
9400Sstevel@tonic-gate 			continue;
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 		psize = ipl->ipl_dsize;
9440Sstevel@tonic-gate 		if (psize > blen)
9450Sstevel@tonic-gate 			break;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 		if (binarylog) {
9480Sstevel@tonic-gate 			fwrite(buf, psize, 1, binarylog);
9490Sstevel@tonic-gate 			fflush(binarylog);
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		if (logtype == IPL_LOGIPF) {
9532393Syz155240 			if (ipl->ipl_magic == IPL_MAGIC)
9542393Syz155240 				print_ipflog(log, buf, psize);
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		} else if (logtype == IPL_LOGNAT) {
9572393Syz155240 			if (ipl->ipl_magic == IPL_MAGIC_NAT)
9582393Syz155240 				print_natlog(log, buf, psize);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 		} else if (logtype == IPL_LOGSTATE) {
9612393Syz155240 			if (ipl->ipl_magic == IPL_MAGIC_STATE)
9622393Syz155240 				print_statelog(log, buf, psize);
9630Sstevel@tonic-gate 		}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		blen -= psize;
9660Sstevel@tonic-gate 		buf += psize;
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 	if (bp)
9690Sstevel@tonic-gate 		free(bp);
9700Sstevel@tonic-gate 	return;
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 
print_ipflog(log,buf,blen)9740Sstevel@tonic-gate static	void	print_ipflog(log, buf, blen)
9750Sstevel@tonic-gate FILE	*log;
9760Sstevel@tonic-gate char	*buf;
9770Sstevel@tonic-gate int	blen;
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate 	tcphdr_t	*tp;
9800Sstevel@tonic-gate 	struct	icmp	*ic;
9810Sstevel@tonic-gate 	struct	icmp	*icmp;
9820Sstevel@tonic-gate 	struct	tm	*tm;
9830Sstevel@tonic-gate 	char	*t, *proto;
9842393Syz155240 	int	i, v, lvl, res, len, off, plen, ipoff, defaction;
9850Sstevel@tonic-gate 	ip_t	*ipc, *ip;
9862393Syz155240 	u_32_t	*s, *d;
9870Sstevel@tonic-gate 	u_short	hl, p;
9880Sstevel@tonic-gate 	ipflog_t *ipf;
9890Sstevel@tonic-gate 	iplog_t	*ipl;
9900Sstevel@tonic-gate #ifdef	USE_INET6
9910Sstevel@tonic-gate 	ip6_t *ip6;
9920Sstevel@tonic-gate #endif
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	ipl = (iplog_t *)buf;
9950Sstevel@tonic-gate 	ipf = (ipflog_t *)((char *)buf + sizeof(*ipl));
9960Sstevel@tonic-gate 	ip = (ip_t *)((char *)ipf + sizeof(*ipf));
9970Sstevel@tonic-gate 	v = IP_V(ip);
9980Sstevel@tonic-gate 	res = (opts & OPT_RESOLVE) ? 1 : 0;
9990Sstevel@tonic-gate 	t = line;
10000Sstevel@tonic-gate 	*t = '\0';
10010Sstevel@tonic-gate 	tm = get_tm(ipl->ipl_sec);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	len = sizeof(line);
10040Sstevel@tonic-gate 	if (!(opts & OPT_SYSLOG)) {
10050Sstevel@tonic-gate 		(void) strftime(t, len, "%d/%m/%Y ", tm);
10060Sstevel@tonic-gate 		i = strlen(t);
10070Sstevel@tonic-gate 		len -= i;
10080Sstevel@tonic-gate 		t += i;
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 	(void) strftime(t, len, "%T", tm);
10110Sstevel@tonic-gate 	t += strlen(t);
10120Sstevel@tonic-gate 	(void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
10130Sstevel@tonic-gate 	t += strlen(t);
10140Sstevel@tonic-gate 	if (ipl->ipl_count > 1) {
10150Sstevel@tonic-gate 		(void) sprintf(t, "%dx ", ipl->ipl_count);
10160Sstevel@tonic-gate 		t += strlen(t);
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate #if (defined(MENTAT) || \
10190Sstevel@tonic-gate 	(defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
10202393Syz155240 	(defined(__FreeBSD__) && (__FreeBSD_version >= 501113)) || \
10210Sstevel@tonic-gate 	(defined(OpenBSD) && (OpenBSD >= 199603))) || defined(linux)
10220Sstevel@tonic-gate 	{
10230Sstevel@tonic-gate 	char	ifname[sizeof(ipf->fl_ifname) + 1];
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	strncpy(ifname, ipf->fl_ifname, sizeof(ipf->fl_ifname));
10260Sstevel@tonic-gate 	ifname[sizeof(ipf->fl_ifname)] = '\0';
10270Sstevel@tonic-gate 	(void) sprintf(t, "%s", ifname);
10280Sstevel@tonic-gate 	t += strlen(t);
10292393Syz155240 # if defined(MENTAT) || defined(linux)
10302393Syz155240 	if (ISALPHA(*(t - 1))) {
10310Sstevel@tonic-gate 		sprintf(t, "%d", ipf->fl_unit);
10320Sstevel@tonic-gate 		t += strlen(t);
10330Sstevel@tonic-gate 	}
10340Sstevel@tonic-gate # endif
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate #else
10370Sstevel@tonic-gate 	for (len = 0; len < 3; len++)
10380Sstevel@tonic-gate 		if (ipf->fl_ifname[len] == '\0')
10390Sstevel@tonic-gate 			break;
10400Sstevel@tonic-gate 	if (ipf->fl_ifname[len])
10410Sstevel@tonic-gate 		len++;
10420Sstevel@tonic-gate 	(void) sprintf(t, "%*.*s%u", len, len, ipf->fl_ifname, ipf->fl_unit);
10430Sstevel@tonic-gate 	t += strlen(t);
10440Sstevel@tonic-gate #endif
10452393Syz155240 #if defined(__sgi) || defined(_AIX51) || defined(__powerpc__) || \
10462393Syz155240     defined(__arm__)
10472393Syz155240 	if ((ipf->fl_group[0] == 255) && (ipf->fl_group[1] == '\0'))
10482393Syz155240 #else
10490Sstevel@tonic-gate 	if ((ipf->fl_group[0] == -1) && (ipf->fl_group[1] == '\0'))
10502393Syz155240 #endif
10510Sstevel@tonic-gate 		strcat(t, " @-1:");
10520Sstevel@tonic-gate 	else if (ipf->fl_group[0] == '\0')
10530Sstevel@tonic-gate 		(void) strcpy(t, " @0:");
10540Sstevel@tonic-gate 	else
10550Sstevel@tonic-gate 		(void) sprintf(t, " @%s:", ipf->fl_group);
10560Sstevel@tonic-gate 	t += strlen(t);
10570Sstevel@tonic-gate 	if (ipf->fl_rule == 0xffffffff)
10580Sstevel@tonic-gate 		strcat(t, "-1 ");
10590Sstevel@tonic-gate 	else
10600Sstevel@tonic-gate 		(void) sprintf(t, "%u ", ipf->fl_rule + 1);
10610Sstevel@tonic-gate 	t += strlen(t);
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	lvl = LOG_NOTICE;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate  	if (ipf->fl_lflags & FI_SHORT) {
10660Sstevel@tonic-gate 		*t++ = 'S';
10670Sstevel@tonic-gate 		lvl = LOG_ERR;
10680Sstevel@tonic-gate 	}
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (FR_ISPASS(ipf->fl_flags)) {
10710Sstevel@tonic-gate 		if (ipf->fl_flags & FR_LOGP)
10720Sstevel@tonic-gate 			*t++ = 'p';
10730Sstevel@tonic-gate 		else
10740Sstevel@tonic-gate 			*t++ = 'P';
10750Sstevel@tonic-gate 	} else if (FR_ISBLOCK(ipf->fl_flags)) {
10760Sstevel@tonic-gate 		if (ipf->fl_flags & FR_LOGB)
10770Sstevel@tonic-gate 			*t++ = 'b';
10780Sstevel@tonic-gate 		else
10790Sstevel@tonic-gate 			*t++ = 'B';
10800Sstevel@tonic-gate 		lvl = LOG_WARNING;
10810Sstevel@tonic-gate 	} else if ((ipf->fl_flags & FR_LOGMASK) == FR_LOG) {
10820Sstevel@tonic-gate 		*t++ = 'L';
10830Sstevel@tonic-gate 		lvl = LOG_INFO;
10840Sstevel@tonic-gate 	} else if (ipf->fl_flags & FF_LOGNOMATCH) {
10850Sstevel@tonic-gate 		*t++ = 'n';
10860Sstevel@tonic-gate 	} else {
10870Sstevel@tonic-gate 		*t++ = '?';
10880Sstevel@tonic-gate 		lvl = LOG_EMERG;
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 	if (ipf->fl_loglevel != 0xffff)
10910Sstevel@tonic-gate 		lvl = ipf->fl_loglevel;
10920Sstevel@tonic-gate 	*t++ = ' ';
10930Sstevel@tonic-gate 	*t = '\0';
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	if (v == 6) {
10960Sstevel@tonic-gate #ifdef	USE_INET6
10970Sstevel@tonic-gate 		off = 0;
10980Sstevel@tonic-gate 		ipoff = 0;
10990Sstevel@tonic-gate 		hl = sizeof(ip6_t);
11000Sstevel@tonic-gate 		ip6 = (ip6_t *)ip;
11010Sstevel@tonic-gate 		p = (u_short)ip6->ip6_nxt;
11020Sstevel@tonic-gate 		s = (u_32_t *)&ip6->ip6_src;
11030Sstevel@tonic-gate 		d = (u_32_t *)&ip6->ip6_dst;
11042393Syz155240 		plen = hl + ntohs(ip6->ip6_plen);
11050Sstevel@tonic-gate #else
11060Sstevel@tonic-gate 		sprintf(t, "ipv6");
11070Sstevel@tonic-gate 		goto printipflog;
11080Sstevel@tonic-gate #endif
11090Sstevel@tonic-gate 	} else if (v == 4) {
11100Sstevel@tonic-gate 		hl = IP_HL(ip) << 2;
11110Sstevel@tonic-gate 		ipoff = ip->ip_off;
11120Sstevel@tonic-gate 		off = ipoff & IP_OFFMASK;
11130Sstevel@tonic-gate 		p = (u_short)ip->ip_p;
11140Sstevel@tonic-gate 		s = (u_32_t *)&ip->ip_src;
11150Sstevel@tonic-gate 		d = (u_32_t *)&ip->ip_dst;
11160Sstevel@tonic-gate 		plen = ip->ip_len;
11170Sstevel@tonic-gate 	} else {
11180Sstevel@tonic-gate 		goto printipflog;
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 	proto = getproto(p);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	if ((p == IPPROTO_TCP || p == IPPROTO_UDP) && !off) {
11230Sstevel@tonic-gate 		tp = (tcphdr_t *)((char *)ip + hl);
11240Sstevel@tonic-gate 		if (!(ipf->fl_lflags & FI_SHORT)) {
11250Sstevel@tonic-gate 			(void) sprintf(t, "%s,%s -> ", hostname(res, v, s),
11260Sstevel@tonic-gate 				portname(res, proto, (u_int)tp->th_sport));
11270Sstevel@tonic-gate 			t += strlen(t);
11280Sstevel@tonic-gate 			(void) sprintf(t, "%s,%s PR %s len %hu %hu",
11290Sstevel@tonic-gate 				hostname(res, v, d),
11300Sstevel@tonic-gate 				portname(res, proto, (u_int)tp->th_dport),
11310Sstevel@tonic-gate 				proto, hl, plen);
11320Sstevel@tonic-gate 			t += strlen(t);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 			if (p == IPPROTO_TCP) {
11350Sstevel@tonic-gate 				*t++ = ' ';
11360Sstevel@tonic-gate 				*t++ = '-';
11370Sstevel@tonic-gate 				for (i = 0; tcpfl[i].value; i++)
11380Sstevel@tonic-gate 					if (tp->th_flags & tcpfl[i].value)
11390Sstevel@tonic-gate 						*t++ = tcpfl[i].flag;
11400Sstevel@tonic-gate 				if (opts & OPT_VERBOSE) {
11410Sstevel@tonic-gate 					(void) sprintf(t, " %lu %lu %hu",
11420Sstevel@tonic-gate 						(u_long)(ntohl(tp->th_seq)),
11430Sstevel@tonic-gate 						(u_long)(ntohl(tp->th_ack)),
11440Sstevel@tonic-gate 						ntohs(tp->th_win));
11450Sstevel@tonic-gate 					t += strlen(t);
11460Sstevel@tonic-gate 				}
11470Sstevel@tonic-gate 			}
11480Sstevel@tonic-gate 			*t = '\0';
11490Sstevel@tonic-gate 		} else {
11500Sstevel@tonic-gate 			(void) sprintf(t, "%s -> ", hostname(res, v, s));
11510Sstevel@tonic-gate 			t += strlen(t);
11520Sstevel@tonic-gate 			(void) sprintf(t, "%s PR %s len %hu %hu",
11530Sstevel@tonic-gate 				hostname(res, v, d), proto, hl, plen);
11540Sstevel@tonic-gate 		}
11550Sstevel@tonic-gate 	} else if ((p == IPPROTO_ICMPV6) && !off && (v == 6)) {
11560Sstevel@tonic-gate 		ic = (struct icmp *)((char *)ip + hl);
11570Sstevel@tonic-gate 		(void) sprintf(t, "%s -> ", hostname(res, v, s));
11580Sstevel@tonic-gate 		t += strlen(t);
11590Sstevel@tonic-gate 		(void) sprintf(t, "%s PR icmpv6 len %hu %hu icmpv6 %s",
11600Sstevel@tonic-gate 			hostname(res, v, d), hl, plen,
11610Sstevel@tonic-gate 			icmpname6(ic->icmp_type, ic->icmp_code));
11620Sstevel@tonic-gate 	} else if ((p == IPPROTO_ICMP) && !off && (v == 4)) {
11630Sstevel@tonic-gate 		ic = (struct icmp *)((char *)ip + hl);
11640Sstevel@tonic-gate 		(void) sprintf(t, "%s -> ", hostname(res, v, s));
11650Sstevel@tonic-gate 		t += strlen(t);
11660Sstevel@tonic-gate 		(void) sprintf(t, "%s PR icmp len %hu %hu icmp %s",
11670Sstevel@tonic-gate 			hostname(res, v, d), hl, plen,
11680Sstevel@tonic-gate 			icmpname(ic->icmp_type, ic->icmp_code));
11690Sstevel@tonic-gate 		if (ic->icmp_type == ICMP_UNREACH ||
11700Sstevel@tonic-gate 		    ic->icmp_type == ICMP_SOURCEQUENCH ||
11710Sstevel@tonic-gate 		    ic->icmp_type == ICMP_PARAMPROB ||
11720Sstevel@tonic-gate 		    ic->icmp_type == ICMP_REDIRECT ||
11730Sstevel@tonic-gate 		    ic->icmp_type == ICMP_TIMXCEED) {
11740Sstevel@tonic-gate 			ipc = &ic->icmp_ip;
11750Sstevel@tonic-gate 			i = ntohs(ipc->ip_len);
11760Sstevel@tonic-gate 			/*
11770Sstevel@tonic-gate 			 * XXX - try to guess endian of ip_len in ICMP
11780Sstevel@tonic-gate 			 * returned data.
11790Sstevel@tonic-gate 			 */
11800Sstevel@tonic-gate 			if (i > 1500)
11810Sstevel@tonic-gate 				i = ipc->ip_len;
11820Sstevel@tonic-gate 			ipoff = ntohs(ipc->ip_off);
11830Sstevel@tonic-gate 			proto = getproto(ipc->ip_p);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 			if (!(ipoff & IP_OFFMASK) &&
11860Sstevel@tonic-gate 			    ((ipc->ip_p == IPPROTO_TCP) ||
11870Sstevel@tonic-gate 			     (ipc->ip_p == IPPROTO_UDP))) {
11880Sstevel@tonic-gate 				tp = (tcphdr_t *)((char *)ipc + hl);
11890Sstevel@tonic-gate 				t += strlen(t);
11900Sstevel@tonic-gate 				(void) sprintf(t, " for %s,%s -",
11910Sstevel@tonic-gate 					HOSTNAME_V4(res, ipc->ip_src),
11920Sstevel@tonic-gate 					portname(res, proto,
11930Sstevel@tonic-gate 						 (u_int)tp->th_sport));
11940Sstevel@tonic-gate 				t += strlen(t);
11950Sstevel@tonic-gate 				(void) sprintf(t, " %s,%s PR %s len %hu %hu",
11960Sstevel@tonic-gate 					HOSTNAME_V4(res, ipc->ip_dst),
11970Sstevel@tonic-gate 					portname(res, proto,
11980Sstevel@tonic-gate 						 (u_int)tp->th_dport),
11990Sstevel@tonic-gate 					proto, IP_HL(ipc) << 2, i);
12000Sstevel@tonic-gate 			} else if (!(ipoff & IP_OFFMASK) &&
12010Sstevel@tonic-gate 				   (ipc->ip_p == IPPROTO_ICMP)) {
12020Sstevel@tonic-gate 				icmp = (icmphdr_t *)((char *)ipc + hl);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 				t += strlen(t);
12050Sstevel@tonic-gate 				(void) sprintf(t, " for %s -",
12060Sstevel@tonic-gate 					HOSTNAME_V4(res, ipc->ip_src));
12070Sstevel@tonic-gate 				t += strlen(t);
12080Sstevel@tonic-gate 				(void) sprintf(t,
12090Sstevel@tonic-gate 					" %s PR icmp len %hu %hu icmp %d/%d",
12100Sstevel@tonic-gate 					HOSTNAME_V4(res, ipc->ip_dst),
12110Sstevel@tonic-gate 					IP_HL(ipc) << 2, i,
12120Sstevel@tonic-gate 					icmp->icmp_type, icmp->icmp_code);
12130Sstevel@tonic-gate 			} else {
12140Sstevel@tonic-gate 				t += strlen(t);
12150Sstevel@tonic-gate 				(void) sprintf(t, " for %s -",
12160Sstevel@tonic-gate 						HOSTNAME_V4(res, ipc->ip_src));
12170Sstevel@tonic-gate 				t += strlen(t);
12180Sstevel@tonic-gate 				(void) sprintf(t, " %s PR %s len %hu (%hu)",
12190Sstevel@tonic-gate 					HOSTNAME_V4(res, ipc->ip_dst), proto,
12200Sstevel@tonic-gate 					IP_HL(ipc) << 2, i);
12210Sstevel@tonic-gate 				t += strlen(t);
12220Sstevel@tonic-gate 				if (ipoff & IP_OFFMASK) {
12232393Syz155240 					(void) sprintf(t,
12242393Syz155240 						"(frag %d:%hu@%hu%s%s)",
12252393Syz155240 						ntohs(ipc->ip_id),
12262393Syz155240 						i - (IP_HL(ipc) << 2),
12272393Syz155240 						(ipoff & IP_OFFMASK) << 3,
12280Sstevel@tonic-gate 						ipoff & IP_MF ? "+" : "",
12292393Syz155240 						ipoff & IP_DF ? "-" : "");
12300Sstevel@tonic-gate 				}
12310Sstevel@tonic-gate 			}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 		}
12340Sstevel@tonic-gate 	} else {
12350Sstevel@tonic-gate 		(void) sprintf(t, "%s -> ", hostname(res, v, s));
12360Sstevel@tonic-gate 		t += strlen(t);
12370Sstevel@tonic-gate 		(void) sprintf(t, "%s PR %s len %hu (%hu)",
12380Sstevel@tonic-gate 			hostname(res, v, d), proto, hl, plen);
12390Sstevel@tonic-gate 		t += strlen(t);
12400Sstevel@tonic-gate 		if (off & IP_OFFMASK)
12412393Syz155240 			(void) sprintf(t, " (frag %d:%hu@%hu%s%s)",
12422393Syz155240 				ntohs(ip->ip_id),
12432393Syz155240 				plen - hl, (off & IP_OFFMASK) << 3,
12440Sstevel@tonic-gate 				ipoff & IP_MF ? "+" : "",
12452393Syz155240 				ipoff & IP_DF ? "-" : "");
12460Sstevel@tonic-gate 	}
12470Sstevel@tonic-gate 	t += strlen(t);
12480Sstevel@tonic-gate 
12492393Syz155240 printipflog:
12500Sstevel@tonic-gate 	if (ipf->fl_flags & FR_KEEPSTATE) {
12510Sstevel@tonic-gate 		(void) strcpy(t, " K-S");
12520Sstevel@tonic-gate 		t += strlen(t);
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	if (ipf->fl_flags & FR_KEEPFRAG) {
12560Sstevel@tonic-gate 		(void) strcpy(t, " K-F");
12570Sstevel@tonic-gate 		t += strlen(t);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	if (ipf->fl_dir == 0)
12610Sstevel@tonic-gate 		strcpy(t, " IN");
12620Sstevel@tonic-gate 	else if (ipf->fl_dir == 1)
12630Sstevel@tonic-gate 		strcpy(t, " OUT");
12640Sstevel@tonic-gate 	t += strlen(t);
12652393Syz155240 	if (ipf->fl_logtag != 0) {
12662393Syz155240 		sprintf(t, " log-tag %d", ipf->fl_logtag);
12672393Syz155240 		t += strlen(t);
12682393Syz155240 	}
12692393Syz155240 	if (ipf->fl_nattag.ipt_num[0] != 0) {
12702393Syz155240 		strcpy(t, " nat-tag ");
12712393Syz155240 		t += strlen(t);
12722393Syz155240 		strncpy(t, ipf->fl_nattag.ipt_tag, sizeof(ipf->fl_nattag));
12730Sstevel@tonic-gate 		t += strlen(t);
12740Sstevel@tonic-gate 	}
12752393Syz155240 	if ((ipf->fl_lflags & FI_LOWTTL) != 0) {
12762393Syz155240 			strcpy(t, " low-ttl");
12772393Syz155240 			t += 8;
12782393Syz155240 	}
12792393Syz155240 	if ((ipf->fl_lflags & FI_OOW) != 0) {
1280*7704SAlexandr.Nedvedicky@Sun.COM 		if (ipf->fl_lflags & FI_NEG_OOW) {
1281*7704SAlexandr.Nedvedicky@Sun.COM 			strcpy(t, " NEG_OOW");
1282*7704SAlexandr.Nedvedicky@Sun.COM 			t += sizeof (" NEG_OOW") - 1;
1283*7704SAlexandr.Nedvedicky@Sun.COM 		} else {
12842393Syz155240 			strcpy(t, " OOW");
1285*7704SAlexandr.Nedvedicky@Sun.COM 			t += sizeof (" OOW") - 1;
1286*7704SAlexandr.Nedvedicky@Sun.COM 		}
12872393Syz155240 	}
12882393Syz155240 	if ((ipf->fl_lflags & FI_BAD) != 0) {
12892393Syz155240 			strcpy(t, " bad");
12902393Syz155240 			t += 4;
12912393Syz155240 	}
12922393Syz155240 	if ((ipf->fl_lflags & FI_NATED) != 0) {
12932393Syz155240 			strcpy(t, " NAT");
12942393Syz155240 			t += 4;
12952393Syz155240 	}
12962393Syz155240 	if ((ipf->fl_lflags & FI_BADNAT) != 0) {
12972393Syz155240 			strcpy(t, " bad-NAT");
12982393Syz155240 			t += 8;
12992393Syz155240 	}
13002393Syz155240 	if ((ipf->fl_lflags & FI_BADSRC) != 0) {
13012393Syz155240 			strcpy(t, " bad-src");
13022393Syz155240 			t += 8;
13032393Syz155240 	}
13042393Syz155240 	if ((ipf->fl_lflags & FI_MULTICAST) != 0) {
13052393Syz155240 			strcpy(t, " multicast");
13062393Syz155240 			t += 10;
13072393Syz155240 	}
13082393Syz155240 	if ((ipf->fl_lflags & FI_BROADCAST) != 0) {
13092393Syz155240 			strcpy(t, " broadcast");
13102393Syz155240 			t += 10;
13112393Syz155240 	}
13122393Syz155240 	if ((ipf->fl_lflags & (FI_MULTICAST|FI_BROADCAST|FI_MBCAST)) ==
13132393Syz155240 	    FI_MBCAST) {
13142393Syz155240 			strcpy(t, " mbcast");
13152393Syz155240 			t += 7;
13162393Syz155240 	}
13170Sstevel@tonic-gate 	*t++ = '\n';
13180Sstevel@tonic-gate 	*t++ = '\0';
13192393Syz155240 	defaction = 0;
13202393Syz155240 	if (conf_file != NULL)
13212393Syz155240 		defaction = check_action(buf, line, opts, lvl);
13222393Syz155240 	if (defaction == 0) {
13232393Syz155240 		if (opts & OPT_SYSLOG)
13242393Syz155240 			syslog(lvl, "%s", line);
13252393Syz155240 		else
13262393Syz155240 			(void) fprintf(log, "%s", line);
13272393Syz155240 		if (opts & OPT_HEXHDR)
13282393Syz155240 			dumphex(log, opts, buf,
13292393Syz155240 				sizeof(iplog_t) + sizeof(*ipf));
13302393Syz155240 		if (opts & OPT_HEXBODY)
13312393Syz155240 			dumphex(log, opts, (char *)ip,
13322393Syz155240 				ipf->fl_plen + ipf->fl_hlen);
13332393Syz155240 		else if ((opts & OPT_LOGBODY) && (ipf->fl_flags & FR_LOGBODY))
13342393Syz155240 			dumphex(log, opts, (char *)ip + ipf->fl_hlen,
13352393Syz155240 				ipf->fl_plen);
13362393Syz155240 	}
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 
usage(prog)13400Sstevel@tonic-gate static void usage(prog)
13410Sstevel@tonic-gate char *prog;
13420Sstevel@tonic-gate {
13430Sstevel@tonic-gate 	fprintf(stderr, "%s: [-abDFhnpstvxX] %s %s %s %s %s %s\n",
13440Sstevel@tonic-gate 		prog, "[-N device]", "[ [-o [NSI]] [-O [NSI]]",
13450Sstevel@tonic-gate 		"[-P pidfile]", "[-S device]", "[-f device]",
13460Sstevel@tonic-gate 		"filename");
13470Sstevel@tonic-gate 	exit(1);
13480Sstevel@tonic-gate }
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 
write_pid(file)13510Sstevel@tonic-gate static void write_pid(file)
13520Sstevel@tonic-gate char *file;
13530Sstevel@tonic-gate {
13540Sstevel@tonic-gate 	FILE *fp = NULL;
13550Sstevel@tonic-gate 	int fd;
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0644)) >= 0) {
13580Sstevel@tonic-gate 		fp = fdopen(fd, "w");
13590Sstevel@tonic-gate 		if (fp == NULL) {
13600Sstevel@tonic-gate 			close(fd);
13610Sstevel@tonic-gate 			fprintf(stderr,
13620Sstevel@tonic-gate 				"unable to open/create pid file: %s\n", file);
13630Sstevel@tonic-gate 			return;
13640Sstevel@tonic-gate 		}
13650Sstevel@tonic-gate 		fprintf(fp, "%d", getpid());
13660Sstevel@tonic-gate 		fclose(fp);
13670Sstevel@tonic-gate 	}
13680Sstevel@tonic-gate }
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 
flushlogs(file,log)13710Sstevel@tonic-gate static void flushlogs(file, log)
13720Sstevel@tonic-gate char *file;
13730Sstevel@tonic-gate FILE *log;
13740Sstevel@tonic-gate {
13750Sstevel@tonic-gate 	int	fd, flushed = 0;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	if ((fd = open(file, O_RDWR)) == -1) {
13780Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: open: %s\n",
13790Sstevel@tonic-gate 			       file, STRERROR(errno));
13800Sstevel@tonic-gate 		exit(1);
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	if (ioctl(fd, SIOCIPFFB, &flushed) == 0) {
13840Sstevel@tonic-gate 		printf("%d bytes flushed from log buffer\n",
13850Sstevel@tonic-gate 			flushed);
13860Sstevel@tonic-gate 		fflush(stdout);
13870Sstevel@tonic-gate 	} else
13880Sstevel@tonic-gate 		perror("SIOCIPFFB");
13890Sstevel@tonic-gate 	(void) close(fd);
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if (flushed) {
13920Sstevel@tonic-gate 		if (opts & OPT_SYSLOG)
13930Sstevel@tonic-gate 			syslog(LOG_INFO, "%d bytes flushed from log\n",
13940Sstevel@tonic-gate 				flushed);
13950Sstevel@tonic-gate 		else if (log != stdout)
13960Sstevel@tonic-gate 			fprintf(log, "%d bytes flushed from log\n", flushed);
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate }
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 
logopts(turnon,options)14010Sstevel@tonic-gate static void logopts(turnon, options)
14020Sstevel@tonic-gate int turnon;
14030Sstevel@tonic-gate char *options;
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate 	int flags = 0;
14060Sstevel@tonic-gate 	char *s;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	for (s = options; *s; s++)
14090Sstevel@tonic-gate 	{
14100Sstevel@tonic-gate 		switch (*s)
14110Sstevel@tonic-gate 		{
14120Sstevel@tonic-gate 		case 'N' :
14130Sstevel@tonic-gate 			flags |= OPT_NAT;
14140Sstevel@tonic-gate 			break;
14150Sstevel@tonic-gate 		case 'S' :
14160Sstevel@tonic-gate 			flags |= OPT_STATE;
14170Sstevel@tonic-gate 			break;
14180Sstevel@tonic-gate 		case 'I' :
14190Sstevel@tonic-gate 			flags |= OPT_FILTER;
14200Sstevel@tonic-gate 			break;
14210Sstevel@tonic-gate 		default :
14220Sstevel@tonic-gate 			fprintf(stderr, "Unknown log option %c\n", *s);
14230Sstevel@tonic-gate 			exit(1);
14240Sstevel@tonic-gate 		}
14250Sstevel@tonic-gate 	}
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	if (turnon)
14280Sstevel@tonic-gate 		opts |= flags;
14290Sstevel@tonic-gate 	else
14300Sstevel@tonic-gate 		opts &= ~(flags);
14310Sstevel@tonic-gate }
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 
main(argc,argv)14340Sstevel@tonic-gate int main(argc, argv)
14350Sstevel@tonic-gate int argc;
14360Sstevel@tonic-gate char *argv[];
14370Sstevel@tonic-gate {
14380Sstevel@tonic-gate 	struct	stat	sb;
14390Sstevel@tonic-gate 	FILE	*log = stdout;
14402393Syz155240 	FILE	*fp;
14410Sstevel@tonic-gate 	int	fd[3], doread, n, i;
14420Sstevel@tonic-gate 	int	tr, nr, regular[3], c;
14430Sstevel@tonic-gate 	int	fdt[3], devices = 0, make_daemon = 0;
14440Sstevel@tonic-gate 	char	buf[DEFAULT_IPFLOGSIZE], *iplfile[3], *s;
14450Sstevel@tonic-gate 	extern	int	optind;
14460Sstevel@tonic-gate 	extern	char	*optarg;
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	fd[0] = fd[1] = fd[2] = -1;
14490Sstevel@tonic-gate 	fdt[0] = fdt[1] = fdt[2] = -1;
14500Sstevel@tonic-gate 	iplfile[0] = IPL_NAME;
14510Sstevel@tonic-gate 	iplfile[1] = IPNAT_NAME;
14520Sstevel@tonic-gate 	iplfile[2] = IPSTATE_NAME;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "?abB:C:Df:FhnN:o:O:pP:sS:tvxX")) != -1)
14550Sstevel@tonic-gate 		switch (c)
14560Sstevel@tonic-gate 		{
14570Sstevel@tonic-gate 		case 'a' :
14580Sstevel@tonic-gate 			opts |= OPT_LOGALL;
14590Sstevel@tonic-gate 			fdt[0] = IPL_LOGIPF;
14600Sstevel@tonic-gate 			fdt[1] = IPL_LOGNAT;
14610Sstevel@tonic-gate 			fdt[2] = IPL_LOGSTATE;
14620Sstevel@tonic-gate 			break;
14630Sstevel@tonic-gate 		case 'b' :
14640Sstevel@tonic-gate 			opts |= OPT_LOGBODY;
14650Sstevel@tonic-gate 			break;
14660Sstevel@tonic-gate 		case 'B' :
14670Sstevel@tonic-gate 			binarylogfile = optarg;
14680Sstevel@tonic-gate 			binarylog = fopen(optarg, "a");
14690Sstevel@tonic-gate 			break;
14700Sstevel@tonic-gate 		case 'C' :
14710Sstevel@tonic-gate 			conf_file = optarg;
14720Sstevel@tonic-gate 			break;
14730Sstevel@tonic-gate 		case 'D' :
14740Sstevel@tonic-gate 			make_daemon = 1;
14750Sstevel@tonic-gate 			break;
14760Sstevel@tonic-gate 		case 'f' : case 'I' :
14770Sstevel@tonic-gate 			opts |= OPT_FILTER;
14780Sstevel@tonic-gate 			fdt[0] = IPL_LOGIPF;
14790Sstevel@tonic-gate 			iplfile[0] = optarg;
14800Sstevel@tonic-gate 			break;
14810Sstevel@tonic-gate 		case 'F' :
14820Sstevel@tonic-gate 			flushlogs(iplfile[0], log);
14830Sstevel@tonic-gate 			flushlogs(iplfile[1], log);
14840Sstevel@tonic-gate 			flushlogs(iplfile[2], log);
14850Sstevel@tonic-gate 			break;
14860Sstevel@tonic-gate 		case 'n' :
14870Sstevel@tonic-gate 			opts |= OPT_RESOLVE;
14880Sstevel@tonic-gate 			break;
14890Sstevel@tonic-gate 		case 'N' :
14900Sstevel@tonic-gate 			opts |= OPT_NAT;
14910Sstevel@tonic-gate 			fdt[1] = IPL_LOGNAT;
14920Sstevel@tonic-gate 			iplfile[1] = optarg;
14930Sstevel@tonic-gate 			break;
14940Sstevel@tonic-gate 		case 'o' : case 'O' :
14950Sstevel@tonic-gate 			logopts(c == 'o', optarg);
14960Sstevel@tonic-gate 			fdt[0] = fdt[1] = fdt[2] = -1;
14970Sstevel@tonic-gate 			if (opts & OPT_FILTER)
14980Sstevel@tonic-gate 				fdt[0] = IPL_LOGIPF;
14990Sstevel@tonic-gate 			if (opts & OPT_NAT)
15000Sstevel@tonic-gate 				fdt[1] = IPL_LOGNAT;
15010Sstevel@tonic-gate 			if (opts & OPT_STATE)
15020Sstevel@tonic-gate 				fdt[2] = IPL_LOGSTATE;
15030Sstevel@tonic-gate 			break;
15040Sstevel@tonic-gate 		case 'p' :
15050Sstevel@tonic-gate 			opts |= OPT_PORTNUM;
15060Sstevel@tonic-gate 			break;
15070Sstevel@tonic-gate 		case 'P' :
15080Sstevel@tonic-gate 			pidfile = optarg;
15090Sstevel@tonic-gate 			break;
15100Sstevel@tonic-gate 		case 's' :
15110Sstevel@tonic-gate 			s = strrchr(argv[0], '/');
15120Sstevel@tonic-gate 			if (s == NULL)
15130Sstevel@tonic-gate 				s = argv[0];
15140Sstevel@tonic-gate 			else
15150Sstevel@tonic-gate 				s++;
15160Sstevel@tonic-gate 			openlog(s, LOG_NDELAY|LOG_PID, LOGFAC);
15170Sstevel@tonic-gate 			s = NULL;
15180Sstevel@tonic-gate 			opts |= OPT_SYSLOG;
15190Sstevel@tonic-gate 			log = NULL;
15200Sstevel@tonic-gate 			break;
15210Sstevel@tonic-gate 		case 'S' :
15220Sstevel@tonic-gate 			opts |= OPT_STATE;
15230Sstevel@tonic-gate 			fdt[2] = IPL_LOGSTATE;
15240Sstevel@tonic-gate 			iplfile[2] = optarg;
15250Sstevel@tonic-gate 			break;
15260Sstevel@tonic-gate 		case 't' :
15270Sstevel@tonic-gate 			opts |= OPT_TAIL;
15280Sstevel@tonic-gate 			break;
15290Sstevel@tonic-gate 		case 'v' :
15300Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
15310Sstevel@tonic-gate 			break;
15320Sstevel@tonic-gate 		case 'x' :
15330Sstevel@tonic-gate 			opts |= OPT_HEXBODY;
15340Sstevel@tonic-gate 			break;
15350Sstevel@tonic-gate 		case 'X' :
15360Sstevel@tonic-gate 			opts |= OPT_HEXHDR;
15370Sstevel@tonic-gate 			break;
15380Sstevel@tonic-gate 		default :
15390Sstevel@tonic-gate 		case 'h' :
15400Sstevel@tonic-gate 		case '?' :
15410Sstevel@tonic-gate 			usage(argv[0]);
15420Sstevel@tonic-gate 		}
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	init_tabs();
15450Sstevel@tonic-gate 	if (conf_file)
15460Sstevel@tonic-gate 		if (load_config(conf_file) == -1)
15470Sstevel@tonic-gate 			exit(1);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	/*
15500Sstevel@tonic-gate 	 * Default action is to only open the filter log file.
15510Sstevel@tonic-gate 	 */
15520Sstevel@tonic-gate 	if ((fdt[0] == -1) && (fdt[1] == -1) && (fdt[2] == -1))
15530Sstevel@tonic-gate 		fdt[0] = IPL_LOGIPF;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
15560Sstevel@tonic-gate 		if (fdt[i] == -1)
15570Sstevel@tonic-gate 			continue;
15580Sstevel@tonic-gate 		if (!strcmp(iplfile[i], "-"))
15590Sstevel@tonic-gate 			fd[i] = 0;
15600Sstevel@tonic-gate 		else {
15610Sstevel@tonic-gate 			if ((fd[i] = open(iplfile[i], O_RDONLY)) == -1) {
15620Sstevel@tonic-gate 				(void) fprintf(stderr,
15630Sstevel@tonic-gate 					       "%s: open: %s\n", iplfile[i],
15640Sstevel@tonic-gate 					       STRERROR(errno));
15650Sstevel@tonic-gate 				exit(1);
15660Sstevel@tonic-gate 				/* NOTREACHED */
15670Sstevel@tonic-gate 			}
15680Sstevel@tonic-gate 			if (fstat(fd[i], &sb) == -1) {
15690Sstevel@tonic-gate 				(void) fprintf(stderr, "%d: fstat: %s\n",
15700Sstevel@tonic-gate 					       fd[i], STRERROR(errno));
15710Sstevel@tonic-gate 				exit(1);
15720Sstevel@tonic-gate 				/* NOTREACHED */
15730Sstevel@tonic-gate 			}
15740Sstevel@tonic-gate 			if (!(regular[i] = !S_ISCHR(sb.st_mode)))
15750Sstevel@tonic-gate 				devices++;
15760Sstevel@tonic-gate 		}
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	if (!(opts & OPT_SYSLOG)) {
15800Sstevel@tonic-gate 		logfile = argv[optind];
15810Sstevel@tonic-gate 		log = logfile ? fopen(logfile, "a") : stdout;
15820Sstevel@tonic-gate 		if (log == NULL) {
15830Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: fopen: %s\n",
15840Sstevel@tonic-gate 				       argv[optind], STRERROR(errno));
15850Sstevel@tonic-gate 			exit(1);
15860Sstevel@tonic-gate 			/* NOTREACHED */
15870Sstevel@tonic-gate 		}
15880Sstevel@tonic-gate 		setvbuf(log, NULL, _IONBF, 0);
15890Sstevel@tonic-gate 	} else
15900Sstevel@tonic-gate 		log = NULL;
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	if (make_daemon && ((log != stdout) || (opts & OPT_SYSLOG))) {
15930Sstevel@tonic-gate #if BSD >= 199306
15940Sstevel@tonic-gate 		daemon(0, !(opts & OPT_SYSLOG));
15950Sstevel@tonic-gate #else
15960Sstevel@tonic-gate 		int pid;
15970Sstevel@tonic-gate 		if ((pid = fork()) > 0)
15980Sstevel@tonic-gate 			exit(0);
15990Sstevel@tonic-gate 		if (pid < 0) {
16000Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: fork() failed: %s\n",
16010Sstevel@tonic-gate 				       argv[0], STRERROR(errno));
16020Sstevel@tonic-gate 			exit(1);
16030Sstevel@tonic-gate 			/* NOTREACHED */
16040Sstevel@tonic-gate 		}
16050Sstevel@tonic-gate 		setsid();
16060Sstevel@tonic-gate 		if ((opts & OPT_SYSLOG))
16070Sstevel@tonic-gate 			close(2);
16080Sstevel@tonic-gate #endif /* !BSD */
16090Sstevel@tonic-gate 		close(0);
16100Sstevel@tonic-gate 		close(1);
16110Sstevel@tonic-gate 	}
16120Sstevel@tonic-gate 	write_pid(pidfile);
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	signal(SIGHUP, handlehup);
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	for (doread = 1; doread; ) {
16170Sstevel@tonic-gate 		nr = 0;
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
16200Sstevel@tonic-gate 			tr = 0;
16210Sstevel@tonic-gate 			if (fdt[i] == -1)
16220Sstevel@tonic-gate 				continue;
16230Sstevel@tonic-gate 			if (!regular[i]) {
16240Sstevel@tonic-gate 				if (ioctl(fd[i], FIONREAD, &tr) == -1) {
16250Sstevel@tonic-gate 					if (opts & OPT_SYSLOG)
16260Sstevel@tonic-gate 						syslog(LOG_CRIT,
16270Sstevel@tonic-gate 						       "ioctl(FIONREAD): %m");
16280Sstevel@tonic-gate 					else
16290Sstevel@tonic-gate 						perror("ioctl(FIONREAD)");
16300Sstevel@tonic-gate 					exit(1);
16310Sstevel@tonic-gate 					/* NOTREACHED */
16320Sstevel@tonic-gate 				}
16330Sstevel@tonic-gate 			} else {
16340Sstevel@tonic-gate 				tr = (lseek(fd[i], 0, SEEK_CUR) < sb.st_size);
16350Sstevel@tonic-gate 				if (!tr && !(opts & OPT_TAIL))
16360Sstevel@tonic-gate 					doread = 0;
16370Sstevel@tonic-gate 			}
16380Sstevel@tonic-gate 			if (!tr)
16390Sstevel@tonic-gate 				continue;
16400Sstevel@tonic-gate 			nr += tr;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 			tr = read_log(fd[i], &n, buf, sizeof(buf));
16430Sstevel@tonic-gate 			if (donehup) {
16442393Syz155240 				if (logfile && (fp = fopen(logfile, "a"))) {
16450Sstevel@tonic-gate 					fclose(log);
16462393Syz155240 					log = fp;
16470Sstevel@tonic-gate 				}
16482393Syz155240 				if (binarylogfile && (fp = fopen(binarylogfile, "a"))) {
16490Sstevel@tonic-gate 					fclose(binarylog);
16502393Syz155240 					binarylog = fp;
16510Sstevel@tonic-gate 				}
16522393Syz155240 				init_tabs();
16532393Syz155240 				if (conf_file != NULL)
16542393Syz155240 					load_config(conf_file);
16552393Syz155240 				donehup = 0;
16560Sstevel@tonic-gate 			}
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 			switch (tr)
16590Sstevel@tonic-gate 			{
16600Sstevel@tonic-gate 			case -1 :
16610Sstevel@tonic-gate 				if (opts & OPT_SYSLOG)
16620Sstevel@tonic-gate 					syslog(LOG_CRIT, "read: %m\n");
16630Sstevel@tonic-gate 				else
16640Sstevel@tonic-gate 					perror("read");
16650Sstevel@tonic-gate 				doread = 0;
16660Sstevel@tonic-gate 				break;
16670Sstevel@tonic-gate 			case 1 :
16680Sstevel@tonic-gate 				if (opts & OPT_SYSLOG)
16690Sstevel@tonic-gate 					syslog(LOG_CRIT, "aborting logging\n");
16700Sstevel@tonic-gate 				else
16710Sstevel@tonic-gate 					fprintf(log, "aborting logging\n");
16720Sstevel@tonic-gate 				doread = 0;
16730Sstevel@tonic-gate 				break;
16740Sstevel@tonic-gate 			case 2 :
16750Sstevel@tonic-gate 				break;
16760Sstevel@tonic-gate 			case 0 :
16770Sstevel@tonic-gate 				if (n > 0) {
16780Sstevel@tonic-gate 					print_log(fdt[i], log, buf, n);
16790Sstevel@tonic-gate 					if (!(opts & OPT_SYSLOG))
16800Sstevel@tonic-gate 						fflush(log);
16810Sstevel@tonic-gate 				}
16820Sstevel@tonic-gate 				break;
16830Sstevel@tonic-gate 			}
16840Sstevel@tonic-gate 		}
16850Sstevel@tonic-gate 		if (!nr && ((opts & OPT_TAIL) || devices))
16860Sstevel@tonic-gate 			sleep(1);
16870Sstevel@tonic-gate 	}
16880Sstevel@tonic-gate 	return(0);
16890Sstevel@tonic-gate 	/* NOTREACHED */
16900Sstevel@tonic-gate }
1691