1 /* $FreeBSD: src/contrib/ipfilter/ipmon.c,v 1.5.2.7 2003/03/01 03:55:51 darrenr Exp $ */
3 * Copyright (C) 1993-2002 by Darren Reed.
5 * See the IPFILTER.LICENCE file for details on licencing.
8 #define SOLARIS (defined(__SVR4) || defined(__svr4__)) && defined(sun)
11 #if defined(__sgi) && (IRIX > 602)
12 # include <sys/ptimers.h>
14 #include <sys/types.h>
16 #include <sys/param.h>
19 #include <sys/socket.h>
20 #include <sys/ioctl.h>
27 #if !defined(__SVR4) && !defined(__svr4__)
28 # if (__FreeBSD_version >= 300000)
29 # include <sys/dirent.h>
34 # include <sys/filio.h>
35 # include <sys/byteorder.h>
37 #if !defined(__SVR4) && !defined(__GNUC__)
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/tcp_fsm.h>
49 #include <arpa/inet.h>
50 #include <arpa/nameser.h>
54 # include <sys/protosw.h>
55 # include <netinet/ip_var.h>
58 #include <netinet/tcp.h>
59 #include <netinet/ip_icmp.h>
64 #include "netinet/ip_compat.h"
65 #include <netinet/tcpip.h>
66 #include "netinet/ip_fil.h"
67 #include "netinet/ip_nat.h"
68 #include "netinet/ip_state.h"
71 static const char sccsid[] = "@(#)ipmon.c 1.21 6/5/96 (C)1993-2000 Darren Reed";
72 static const char rcsid[] = "@(#)$Id: ipmon.c,v 2.12.2.37 2002/12/06 11:40:26 darrenr Exp $";
76 #if defined(sun) && !defined(SOLARIS2)
77 #define STRERROR(x) sys_errlist[x]
78 extern char *sys_errlist[];
80 #define STRERROR(x) strerror(x)
90 typedef struct icmp_subtype {
95 typedef struct icmp_type {
97 struct icmp_subtype *it_subtable;
103 #define IST_SZ(x) (sizeof(x)/sizeof(icmp_subtype_t))
106 struct flags tcpfl[] = {
119 static char *pidfile = "/etc/opt/ipf/ipmon.pid";
122 static char *pidfile = "/var/run/ipmon.pid";
124 static char *pidfile = "/etc/ipmon.pid";
128 static char line[2048];
130 static FILE *newlog = NULL;
131 static char *logfile = NULL;
132 static int donehup = 0;
133 static void usage __P((char *));
134 static void handlehup __P((int));
135 static void flushlogs __P((char *, FILE *));
136 static void print_log __P((int, FILE *, char *, int));
137 static void print_ipflog __P((FILE *, char *, int));
138 static void print_natlog __P((FILE *, char *, int));
139 static void print_statelog __P((FILE *, char *, int));
140 static void dumphex __P((FILE *, u_char *, int));
141 static int read_log __P((int, int *, char *, int));
142 static void write_pid __P((char *));
143 static char *icmpname __P((u_int, u_int));
144 static char *icmpname6 __P((u_int, u_int));
145 static icmp_type_t *find_icmptype __P((int, icmp_type_t *, size_t));
146 static icmp_subtype_t *find_icmpsubtype __P((int, icmp_subtype_t *, size_t));
148 char *hostname __P((int, int, u_32_t *));
149 char *portname __P((int, char *, u_int));
150 int main __P((int, char *[]));
152 static void logopts __P((int, char *));
153 static void init_tabs __P((void));
154 static char *getproto __P((u_int));
156 static char **protocols = NULL;
157 static char **udp_ports = NULL;
158 static char **tcp_ports = NULL;
160 #define OPT_SYSLOG 0x001
161 #define OPT_RESOLVE 0x002
162 #define OPT_HEXBODY 0x004
163 #define OPT_VERBOSE 0x008
164 #define OPT_HEXHDR 0x010
165 #define OPT_TAIL 0x020
166 #define OPT_NAT 0x080
167 #define OPT_STATE 0x100
168 #define OPT_FILTER 0x200
169 #define OPT_PORTNUM 0x400
170 #define OPT_LOGALL (OPT_NAT|OPT_STATE|OPT_FILTER)
171 #define OPT_LOGBODY 0x800
173 #define HOSTNAME_V4(a,b) hostname((a), 4, (u_32_t *)&(b))
176 #define LOGFAC LOG_LOCAL0
180 static icmp_subtype_t icmpunreachnames[] = {
181 { ICMP_UNREACH_NET, "net" },
182 { ICMP_UNREACH_HOST, "host" },
183 { ICMP_UNREACH_PROTOCOL, "protocol" },
184 { ICMP_UNREACH_PORT, "port" },
185 { ICMP_UNREACH_NEEDFRAG, "needfrag" },
186 { ICMP_UNREACH_SRCFAIL, "srcfail" },
187 { ICMP_UNREACH_NET_UNKNOWN, "net_unknown" },
188 { ICMP_UNREACH_HOST_UNKNOWN, "host_unknown" },
189 { ICMP_UNREACH_NET, "isolated" },
190 { ICMP_UNREACH_NET_PROHIB, "net_prohib" },
191 { ICMP_UNREACH_NET_PROHIB, "host_prohib" },
192 { ICMP_UNREACH_TOSNET, "tosnet" },
193 { ICMP_UNREACH_TOSHOST, "toshost" },
194 { ICMP_UNREACH_ADMIN_PROHIBIT, "admin_prohibit" },
198 static icmp_subtype_t redirectnames[] = {
199 { ICMP_REDIRECT_NET, "net" },
200 { ICMP_REDIRECT_HOST, "host" },
201 { ICMP_REDIRECT_TOSNET, "tosnet" },
202 { ICMP_REDIRECT_TOSHOST, "toshost" },
206 static icmp_subtype_t timxceednames[] = {
207 { ICMP_TIMXCEED_INTRANS, "transit" },
208 { ICMP_TIMXCEED_REASS, "reassem" },
212 static icmp_subtype_t paramnames[] = {
213 { ICMP_PARAMPROB_ERRATPTR, "errata_pointer" },
214 { ICMP_PARAMPROB_OPTABSENT, "optmissing" },
215 { ICMP_PARAMPROB_LENGTH, "length" },
219 static icmp_type_t icmptypes[] = {
220 { ICMP_ECHOREPLY, NULL, 0, "echoreply" },
221 { -1, NULL, 0, NULL },
222 { -1, NULL, 0, NULL },
223 { ICMP_UNREACH, icmpunreachnames,
224 IST_SZ(icmpunreachnames),"unreach" },
225 { ICMP_SOURCEQUENCH, NULL, 0, "sourcequench" },
226 { ICMP_REDIRECT, redirectnames,
227 IST_SZ(redirectnames), "redirect" },
228 { -1, NULL, 0, NULL },
229 { -1, NULL, 0, NULL },
230 { ICMP_ECHO, NULL, 0, "echo" },
231 { ICMP_ROUTERADVERT, NULL, 0, "routeradvert" },
232 { ICMP_ROUTERSOLICIT, NULL, 0, "routersolicit" },
233 { ICMP_TIMXCEED, timxceednames,
234 IST_SZ(timxceednames), "timxceed" },
235 { ICMP_PARAMPROB, paramnames,
236 IST_SZ(paramnames), "paramprob" },
237 { ICMP_TSTAMP, NULL, 0, "timestamp" },
238 { ICMP_TSTAMPREPLY, NULL, 0, "timestampreply" },
239 { ICMP_IREQ, NULL, 0, "inforeq" },
240 { ICMP_IREQREPLY, NULL, 0, "inforeply" },
241 { ICMP_MASKREQ, NULL, 0, "maskreq" },
242 { ICMP_MASKREPLY, NULL, 0, "maskreply" },
243 { -2, NULL, 0, NULL }
246 static icmp_subtype_t icmpredirect6[] = {
247 { ICMP6_DST_UNREACH_NOROUTE, "noroute" },
248 { ICMP6_DST_UNREACH_ADMIN, "admin" },
249 { ICMP6_DST_UNREACH_NOTNEIGHBOR, "neighbour" },
250 { ICMP6_DST_UNREACH_ADDR, "address" },
251 { ICMP6_DST_UNREACH_NOPORT, "noport" },
255 static icmp_subtype_t icmptimexceed6[] = {
256 { ICMP6_TIME_EXCEED_TRANSIT, "intransit" },
257 { ICMP6_TIME_EXCEED_REASSEMBLY, "reassem" },
261 static icmp_subtype_t icmpparamprob6[] = {
262 { ICMP6_PARAMPROB_HEADER, "header" },
263 { ICMP6_PARAMPROB_NEXTHEADER, "nextheader" },
264 { ICMP6_PARAMPROB_OPTION, "option" },
268 static icmp_subtype_t icmpquerysubject6[] = {
269 { ICMP6_NI_SUBJ_IPV6, "ipv6" },
270 { ICMP6_NI_SUBJ_FQDN, "fqdn" },
271 { ICMP6_NI_SUBJ_IPV4, "ipv4" },
275 static icmp_subtype_t icmpnodeinfo6[] = {
276 { ICMP6_NI_SUCCESS, "success" },
277 { ICMP6_NI_REFUSED, "refused" },
278 { ICMP6_NI_UNKNOWN, "unknown" },
282 static icmp_subtype_t icmprenumber6[] = {
283 { ICMP6_ROUTER_RENUMBERING_COMMAND, "command" },
284 { ICMP6_ROUTER_RENUMBERING_RESULT, "result" },
285 { ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET, "seqnum_reset" },
289 static icmp_type_t icmptypes6[] = {
290 { 0, NULL, 0, NULL },
291 { ICMP6_DST_UNREACH, icmpredirect6,
292 IST_SZ(icmpredirect6), "unreach" },
293 { ICMP6_PACKET_TOO_BIG, NULL, 0, "toobig" },
294 { ICMP6_TIME_EXCEEDED, icmptimexceed6,
295 IST_SZ(icmptimexceed6), "timxceed" },
296 { ICMP6_PARAM_PROB, icmpparamprob6,
297 IST_SZ(icmpparamprob6), "paramprob" },
298 { ICMP6_ECHO_REQUEST, NULL, 0, "echo" },
299 { ICMP6_ECHO_REPLY, NULL, 0, "echoreply" },
300 { ICMP6_MEMBERSHIP_QUERY, icmpquerysubject6,
301 IST_SZ(icmpquerysubject6), "groupmemberquery" },
302 { ICMP6_MEMBERSHIP_REPORT,NULL, 0, "groupmemberreport" },
303 { ICMP6_MEMBERSHIP_REDUCTION,NULL, 0, "groupmemberterm" },
304 { ND_ROUTER_SOLICIT, NULL, 0, "routersolicit" },
305 { ND_ROUTER_ADVERT, NULL, 0, "routeradvert" },
306 { ND_NEIGHBOR_SOLICIT, NULL, 0, "neighborsolicit" },
307 { ND_NEIGHBOR_ADVERT, NULL, 0, "neighboradvert" },
308 { ND_REDIRECT, NULL, 0, "redirect" },
309 { ICMP6_ROUTER_RENUMBERING, icmprenumber6,
310 IST_SZ(icmprenumber6), "routerrenumber" },
311 { ICMP6_WRUREQUEST, NULL, 0, "whoareyourequest" },
312 { ICMP6_WRUREPLY, NULL, 0, "whoareyoureply" },
313 { ICMP6_FQDN_QUERY, NULL, 0, "fqdnquery" },
314 { ICMP6_FQDN_REPLY, NULL, 0, "fqdnreply" },
315 { ICMP6_NI_QUERY, icmpnodeinfo6,
316 IST_SZ(icmpnodeinfo6), "nodeinforequest" },
317 { ICMP6_NI_REPLY, NULL, 0, "nodeinforeply" },
318 { MLD6_MTRACE_RESP, NULL, 0, "mtraceresponse" },
319 { MLD6_MTRACE, NULL, 0, "mtracerequest" },
320 { -2, NULL, 0, NULL }
323 static icmp_subtype_t *find_icmpsubtype(type, table, tablesz)
325 icmp_subtype_t *table;
334 if ((type < 0) || (type > table[tablesz - 2].ist_val))
338 if (table[type].ist_val == type)
341 for (i = 0, ist = table; ist->ist_val != -2; i++, ist++)
342 if (ist->ist_val == type)
348 static icmp_type_t *find_icmptype(type, table, tablesz)
359 if ((type < 0) || (type > table[tablesz - 2].it_val))
363 if (table[type].it_val == type)
366 for (i = 0, it = table; it->it_val != -2; i++, it++)
367 if (it->it_val == type)
373 static void handlehup(sig)
378 signal(SIGHUP, handlehup);
379 if (logfile && (fp = fopen(logfile, "a")))
386 static void init_tabs()
393 if (protocols != NULL) {
397 protocols = (char **)malloc(256 * sizeof(*protocols));
398 if (protocols != NULL) {
399 bzero((char *)protocols, 256 * sizeof(*protocols));
402 while ((p = getprotoent()) != NULL)
403 if (p->p_proto >= 0 && p->p_proto <= 255 &&
404 p->p_name != NULL && protocols[p->p_proto] == NULL)
405 protocols[p->p_proto] = strdup(p->p_name);
409 if (udp_ports != NULL) {
413 udp_ports = (char **)malloc(65536 * sizeof(*udp_ports));
414 if (udp_ports != NULL)
415 bzero((char *)udp_ports, 65536 * sizeof(*udp_ports));
417 if (tcp_ports != NULL) {
421 tcp_ports = (char **)malloc(65536 * sizeof(*tcp_ports));
422 if (tcp_ports != NULL)
423 bzero((char *)tcp_ports, 65536 * sizeof(*tcp_ports));
426 while ((s = getservent()) != NULL) {
427 if (s->s_proto == NULL)
429 else if (!strcmp(s->s_proto, "tcp")) {
430 port = ntohs(s->s_port);
433 } else if (!strcmp(s->s_proto, "udp")) {
434 port = ntohs(s->s_port);
439 if ((port < 0 || port > 65535) || (name == NULL))
441 tab[port] = strdup(name);
447 static char *getproto(p)
454 s = protocols ? protocols[p] : NULL;
456 sprintf(pnum, "%u", p);
463 static int read_log(fd, lenp, buf, bufsize)
464 int fd, bufsize, *lenp;
469 nr = read(fd, buf, bufsize);
472 if ((nr < 0) && (errno != EINTR))
479 char *hostname(res, v, ip)
483 # define MAX_INETA 16
484 static char hname[MAXHOSTNAMELEN + MAX_INETA + 3];
486 static char hostbuf[MAXHOSTNAMELEN+1];
494 return inet_ntoa(ipa);
495 hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
497 return inet_ntoa(ipa);
498 sprintf(hname, "%.*s[%s]", MAXHOSTNAMELEN, hp->h_name,
503 (void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
504 hostbuf[MAXHOSTNAMELEN] = '\0';
512 char *portname(res, proto, port)
517 static char pname[8];
522 (void) sprintf(pname, "%u", port);
523 if (!res || (opts & OPT_PORTNUM))
526 if (!strcmp(proto, "tcp"))
528 else if (!strcmp(proto, "udp"))
536 static char *icmpname(type, code)
540 static char name[80];
546 it = find_icmptype(type, icmptypes, sizeof(icmptypes) / sizeof(*it));
551 sprintf(name, "icmptype(%d)/", type);
553 sprintf(name, "%s/", s);
556 if (it != NULL && it->it_subtable != NULL)
557 ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
559 if (ist != NULL && ist->ist_name != NULL)
560 strcat(name, ist->ist_name);
562 sprintf(name + strlen(name), "%d", code);
567 static char *icmpname6(type, code)
571 static char name[80];
577 it = find_icmptype(type, icmptypes6, sizeof(icmptypes6) / sizeof(*it));
582 sprintf(name, "icmpv6type(%d)/", type);
584 sprintf(name, "%s/", s);
587 if (it != NULL && it->it_subtable != NULL)
588 ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
590 if (ist != NULL && ist->ist_name != NULL)
591 strcat(name, ist->ist_name);
593 sprintf(name + strlen(name), "%d", code);
599 static void dumphex(log, buf, len)
606 u_char *s = buf, *t = (u_char *)line;
608 if (len == 0 || buf == 0)
612 for (i = len, j = 0; i; i--, j++, s++) {
613 if (j && !(j & 0xf)) {
616 if (!(opts & OPT_SYSLOG))
619 syslog(LOG_INFO, "%s", line);
623 sprintf((char *)t, "%02x", *s & 0xff);
625 if (!((j + 1) & 0xf)) {
627 sprintf((char *)t, " ");
629 for (k = 16; k; k--, s++)
630 *t++ = (isprint(*s) ? *s : '.');
639 for (k = 16 - (j & 0xf); k; k--) {
644 sprintf((char *)t, " ");
647 for (k = j & 0xf; k; k--, s++)
648 *t++ = (isprint(*s) ? *s : '.');
652 if (!(opts & OPT_SYSLOG)) {
656 syslog(LOG_INFO, "%s", line);
659 static void print_natlog(log, buf, blen)
665 iplog_t *ipl = (iplog_t *)buf;
671 nl = (struct natlog *)((char *)ipl + IPLOG_SIZE);
672 res = (opts & OPT_RESOLVE) ? 1 : 0;
673 tm = localtime((time_t *)&ipl->ipl_sec);
675 if (!(opts & OPT_SYSLOG)) {
676 (void) strftime(t, len, "%d/%m/%Y ", tm);
681 (void) strftime(t, len, "%T", tm);
683 (void) sprintf(t, ".%-.6ld @%hd ", ipl->ipl_usec, nl->nl_rule + 1);
686 if (nl->nl_type == NL_NEWMAP)
687 strcpy(t, "NAT:MAP ");
688 else if (nl->nl_type == NL_NEWRDR)
689 strcpy(t, "NAT:RDR ");
690 else if (nl->nl_type == NL_EXPIRE)
691 strcpy(t, "NAT:EXPIRE ");
692 else if (nl->nl_type == NL_FLUSH)
693 strcpy(t, "NAT:FLUSH ");
694 else if (nl->nl_type == NL_NEWBIMAP)
695 strcpy(t, "NAT:BIMAP ");
696 else if (nl->nl_type == NL_NEWBLOCK)
697 strcpy(t, "NAT:MAPBLOCK ");
699 sprintf(t, "Type: %d ", nl->nl_type);
702 proto = getproto(nl->nl_p);
704 (void) sprintf(t, "%s,%s <- -> ", HOSTNAME_V4(res, nl->nl_inip),
705 portname(res, proto, (u_int)nl->nl_inport));
707 (void) sprintf(t, "%s,%s ", HOSTNAME_V4(res, nl->nl_outip),
708 portname(res, proto, (u_int)nl->nl_outport));
710 (void) sprintf(t, "[%s,%s]", HOSTNAME_V4(res, nl->nl_origip),
711 portname(res, proto, (u_int)nl->nl_origport));
713 if (nl->nl_type == NL_EXPIRE) {
715 (void) sprintf(t, " Pkts %qd Bytes %qd",
716 (long long)nl->nl_pkts,
717 (long long)nl->nl_bytes);
719 (void) sprintf(t, " Pkts %ld Bytes %ld",
720 nl->nl_pkts, nl->nl_bytes);
727 if (opts & OPT_SYSLOG)
728 syslog(LOG_INFO, "%s", line);
730 (void) fprintf(log, "%s", line);
734 static void print_statelog(log, buf, blen)
740 iplog_t *ipl = (iplog_t *)buf;
741 char *t = line, *proto;
745 sl = (struct ipslog *)((char *)ipl + IPLOG_SIZE);
746 res = (opts & OPT_RESOLVE) ? 1 : 0;
747 tm = localtime((time_t *)&ipl->ipl_sec);
749 if (!(opts & OPT_SYSLOG)) {
750 (void) strftime(t, len, "%d/%m/%Y ", tm);
755 (void) strftime(t, len, "%T", tm);
757 (void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
760 if (sl->isl_type == ISL_NEW)
761 strcpy(t, "STATE:NEW ");
762 else if (sl->isl_type == ISL_EXPIRE) {
763 if ((sl->isl_p == IPPROTO_TCP) &&
764 (sl->isl_state[0] > TCPS_ESTABLISHED ||
765 sl->isl_state[1] > TCPS_ESTABLISHED))
766 strcpy(t, "STATE:CLOSE ");
768 strcpy(t, "STATE:EXPIRE ");
769 } else if (sl->isl_type == ISL_FLUSH)
770 strcpy(t, "STATE:FLUSH ");
771 else if (sl->isl_type == ISL_REMOVE)
772 strcpy(t, "STATE:REMOVE ");
774 sprintf(t, "Type: %d ", sl->isl_type);
777 proto = getproto(sl->isl_p);
779 if (sl->isl_p == IPPROTO_TCP || sl->isl_p == IPPROTO_UDP) {
780 (void) sprintf(t, "%s,%s -> ",
781 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_src),
782 portname(res, proto, (u_int)sl->isl_sport));
784 (void) sprintf(t, "%s,%s PR %s",
785 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
786 portname(res, proto, (u_int)sl->isl_dport), proto);
787 } else if (sl->isl_p == IPPROTO_ICMP) {
788 (void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
789 (u_32_t *)&sl->isl_src));
791 (void) sprintf(t, "%s PR icmp %d",
792 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
794 } else if (sl->isl_p == IPPROTO_ICMPV6) {
795 (void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
796 (u_32_t *)&sl->isl_src));
798 (void) sprintf(t, "%s PR icmpv6 %d",
799 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
803 if (sl->isl_type != ISL_NEW) {
805 (void) sprintf(t, " Pkts %qd Bytes %qd",
806 (long long)sl->isl_pkts,
807 (long long)sl->isl_bytes);
809 (void) sprintf(t, " Pkts %ld Bytes %ld",
810 sl->isl_pkts, sl->isl_bytes);
817 if (opts & OPT_SYSLOG)
818 syslog(LOG_INFO, "%s", line);
820 (void) fprintf(log, "%s", line);
824 static void print_log(logtype, log, buf, blen)
830 char *bp = NULL, *bpo = NULL;
834 ipl = (iplog_t *)buf;
835 if ((u_long)ipl & (sizeof(long)-1)) {
838 bp = (char *)malloc(blen);
839 bcopy((char *)ipl, bp, blen);
847 if (ipl->ipl_magic != IPL_MAGIC) {
848 /* invalid data or out of sync */
851 psize = ipl->ipl_dsize;
855 print_ipflog(log, buf, psize);
858 print_natlog(log, buf, psize);
861 print_statelog(log, buf, psize);
874 static void print_ipflog(log, buf, blen)
884 int i, v, lvl, res, len, off, plen, ipoff;
894 ipl = (iplog_t *)buf;
895 ipf = (ipflog_t *)((char *)buf + IPLOG_SIZE);
896 ip = (ip_t *)((char *)ipf + sizeof(*ipf));
898 res = (opts & OPT_RESOLVE) ? 1 : 0;
901 tm = localtime((time_t *)&ipl->ipl_sec);
904 ip->ip_len = ntohs(ip->ip_len);
908 if (!(opts & OPT_SYSLOG)) {
909 (void) strftime(t, len, "%d/%m/%Y ", tm);
914 (void) strftime(t, len, "%T", tm);
916 (void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
918 if (ipl->ipl_count > 1) {
919 (void) sprintf(t, "%dx ", ipl->ipl_count);
923 (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
924 (defined(OpenBSD) && (OpenBSD >= 199603))) || defined(linux)
926 char ifname[sizeof(ipf->fl_ifname) + 1];
928 strncpy(ifname, (char *)ipf->fl_ifname, sizeof(ipf->fl_ifname));
929 ifname[sizeof(ipf->fl_ifname)] = '\0';
930 (void) sprintf(t, "%s", ifname);
933 if (isalpha(*(t - 1))) {
934 sprintf(t, "%d", ipf->fl_unit);
940 for (len = 0; len < 3; len++)
941 if (ipf->fl_ifname[len] == '\0')
943 if (ipf->fl_ifname[len])
945 (void) sprintf(t, "%*.*s%u", len, len, ipf->fl_ifname, ipf->fl_unit);
948 if (ipf->fl_group == 0xffffffff)
951 (void) sprintf(t, " @%u:", ipf->fl_group);
953 if (ipf->fl_rule == 0xffffffff)
956 (void) sprintf(t, "%u ", ipf->fl_rule + 1);
959 if (ipf->fl_flags & FF_SHORT) {
962 } else if (ipf->fl_flags & FR_PASS) {
963 if (ipf->fl_flags & FR_LOG)
968 } else if (ipf->fl_flags & FR_BLOCK) {
969 if (ipf->fl_flags & FR_LOG)
974 } else if (ipf->fl_flags & FF_LOGNOMATCH) {
981 if (ipf->fl_loglevel != 0xffff)
982 lvl = ipf->fl_loglevel;
992 p = (u_short)ip6->ip6_nxt;
993 s = (u_32_t *)&ip6->ip6_src;
994 d = (u_32_t *)&ip6->ip6_dst;
995 plen = ntohs(ip6->ip6_plen);
1000 } else if (v == 4) {
1001 hl = (ip->ip_hl << 2);
1003 off = ipoff & IP_OFFMASK;
1004 p = (u_short)ip->ip_p;
1005 s = (u_32_t *)&ip->ip_src;
1006 d = (u_32_t *)&ip->ip_dst;
1011 proto = getproto(p);
1013 if ((p == IPPROTO_TCP || p == IPPROTO_UDP) && !off) {
1014 tp = (tcphdr_t *)((char *)ip + hl);
1015 if (!(ipf->fl_flags & FF_SHORT)) {
1016 (void) sprintf(t, "%s,%s -> ", hostname(res, v, s),
1017 portname(res, proto, (u_int)tp->th_sport));
1019 (void) sprintf(t, "%s,%s PR %s len %hu %hu",
1020 hostname(res, v, d),
1021 portname(res, proto, (u_int)tp->th_dport),
1025 if (p == IPPROTO_TCP) {
1028 for (i = 0; tcpfl[i].value; i++)
1029 if (tp->th_flags & tcpfl[i].value)
1030 *t++ = tcpfl[i].flag;
1031 if (opts & OPT_VERBOSE) {
1032 (void) sprintf(t, " %lu %lu %hu",
1033 (u_long)(ntohl(tp->th_seq)),
1034 (u_long)(ntohl(tp->th_ack)),
1041 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1043 (void) sprintf(t, "%s PR %s len %hu %hu",
1044 hostname(res, v, d), proto, hl, plen);
1046 } else if ((p == IPPROTO_ICMPV6) && !off && (v == 6)) {
1047 ic = (struct icmp *)((char *)ip + hl);
1048 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1050 (void) sprintf(t, "%s PR icmpv6 len %hu %hu icmpv6 %s",
1051 hostname(res, v, d), hl, plen,
1052 icmpname6(ic->icmp_type, ic->icmp_code));
1053 } else if ((p == IPPROTO_ICMP) && !off && (v == 4)) {
1054 ic = (struct icmp *)((char *)ip + hl);
1055 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1057 (void) sprintf(t, "%s PR icmp len %hu %hu icmp %s",
1058 hostname(res, v, d), hl, plen,
1059 icmpname(ic->icmp_type, ic->icmp_code));
1060 if (ic->icmp_type == ICMP_UNREACH ||
1061 ic->icmp_type == ICMP_SOURCEQUENCH ||
1062 ic->icmp_type == ICMP_PARAMPROB ||
1063 ic->icmp_type == ICMP_REDIRECT ||
1064 ic->icmp_type == ICMP_TIMXCEED) {
1066 i = ntohs(ipc->ip_len);
1067 ipoff = ntohs(ipc->ip_off);
1068 proto = getproto(ipc->ip_p);
1070 if (!(ipoff & IP_OFFMASK) &&
1071 ((ipc->ip_p == IPPROTO_TCP) ||
1072 (ipc->ip_p == IPPROTO_UDP))) {
1073 tp = (tcphdr_t *)((char *)ipc + hl);
1075 (void) sprintf(t, " for %s,%s -",
1076 HOSTNAME_V4(res, ipc->ip_src),
1077 portname(res, proto,
1078 (u_int)tp->th_sport));
1080 (void) sprintf(t, " %s,%s PR %s len %hu %hu",
1081 HOSTNAME_V4(res, ipc->ip_dst),
1082 portname(res, proto,
1083 (u_int)tp->th_dport),
1084 proto, ipc->ip_hl << 2, i);
1085 } else if (!(ipoff & IP_OFFMASK) &&
1086 (ipc->ip_p == IPPROTO_ICMP)) {
1087 icmp = (icmphdr_t *)((char *)ipc + hl);
1090 (void) sprintf(t, " for %s -",
1091 HOSTNAME_V4(res, ipc->ip_src));
1094 " %s PR icmp len %hu %hu icmp %d/%d",
1095 HOSTNAME_V4(res, ipc->ip_dst),
1097 icmp->icmp_type, icmp->icmp_code);
1101 (void) sprintf(t, " for %s -",
1102 HOSTNAME_V4(res, ipc->ip_src));
1104 (void) sprintf(t, " %s PR %s len %hu (%hu)",
1105 HOSTNAME_V4(res, ipc->ip_dst), proto,
1106 ipc->ip_hl << 2, i);
1108 if (ipoff & IP_OFFMASK) {
1109 (void) sprintf(t, " frag %s%s%hu@%hu",
1110 ipoff & IP_MF ? "+" : "",
1111 ipoff & IP_DF ? "-" : "",
1112 i - (ipc->ip_hl<<2),
1113 (ipoff & IP_OFFMASK) << 3);
1118 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1120 (void) sprintf(t, "%s PR %s len %hu (%hu)",
1121 hostname(res, v, d), proto, hl, plen);
1123 if (off & IP_OFFMASK)
1124 (void) sprintf(t, " frag %s%s%hu@%hu",
1125 ipoff & IP_MF ? "+" : "",
1126 ipoff & IP_DF ? "-" : "",
1127 plen - hl, (off & IP_OFFMASK) << 3);
1131 if (ipf->fl_flags & FR_KEEPSTATE) {
1132 (void) strcpy(t, " K-S");
1136 if (ipf->fl_flags & FR_KEEPFRAG) {
1137 (void) strcpy(t, " K-F");
1141 if (ipf->fl_dir == 0)
1143 else if (ipf->fl_dir == 1)
1149 if (opts & OPT_SYSLOG)
1150 syslog(lvl, "%s", line);
1152 (void) fprintf(log, "%s", line);
1153 if (opts & OPT_HEXHDR)
1154 dumphex(log, (u_char *)buf, sizeof(iplog_t) + sizeof(*ipf));
1155 if (opts & OPT_HEXBODY)
1156 dumphex(log, (u_char *)ip, ipf->fl_plen + ipf->fl_hlen);
1157 else if ((opts & OPT_LOGBODY) && (ipf->fl_flags & FR_LOGBODY))
1158 dumphex(log, (u_char *)ip + ipf->fl_hlen, ipf->fl_plen);
1162 static void usage(prog)
1165 fprintf(stderr, "%s: [-NFhstvxX] [-f <logfile>]\n", prog);
1170 static void write_pid(file)
1176 if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0644)) >= 0)
1177 fp = fdopen(fd, "w");
1180 fprintf(stderr, "unable to open/create pid file: %s\n", file);
1183 fprintf(fp, "%d", getpid());
1189 static void flushlogs(file, log)
1193 int fd, flushed = 0;
1195 if ((fd = open(file, O_RDWR)) == -1) {
1196 (void) fprintf(stderr, "%s: open: %s\n",
1197 file, STRERROR(errno));
1201 if (ioctl(fd, SIOCIPFFB, &flushed) == 0) {
1202 printf("%d bytes flushed from log buffer\n",
1206 perror("SIOCIPFFB");
1210 if (opts & OPT_SYSLOG)
1211 syslog(LOG_INFO, "%d bytes flushed from log\n",
1213 else if (log != stdout)
1214 fprintf(log, "%d bytes flushed from log\n", flushed);
1219 static void logopts(turnon, options)
1226 for (s = options; *s; s++)
1237 flags |= OPT_FILTER;
1240 fprintf(stderr, "Unknown log option %c\n", *s);
1252 int main(argc, argv)
1256 int fdt[3], devices = 0, make_daemon = 0;
1257 char buf[IPLLOGSIZE], *iplfile[3], *s;
1258 int fd[3], doread, n, i;
1259 extern char *optarg;
1266 fd[0] = fd[1] = fd[2] = -1;
1267 fdt[0] = fdt[1] = fdt[2] = -1;
1268 iplfile[0] = IPL_NAME;
1269 iplfile[1] = IPNAT_NAME;
1270 iplfile[2] = IPSTATE_NAME;
1272 while ((c = getopt(argc, argv, "?abDf:FhnN:o:O:pP:sS:tvxX")) != -1)
1277 fdt[0] = IPL_LOGIPF;
1278 fdt[1] = IPL_LOGNAT;
1279 fdt[2] = IPL_LOGSTATE;
1282 opts |= OPT_LOGBODY;
1287 case 'f' : case 'I' :
1289 fdt[0] = IPL_LOGIPF;
1290 iplfile[0] = optarg;
1293 flushlogs(iplfile[0], log);
1294 flushlogs(iplfile[1], log);
1295 flushlogs(iplfile[2], log);
1298 opts |= OPT_RESOLVE;
1302 fdt[1] = IPL_LOGNAT;
1303 iplfile[1] = optarg;
1305 case 'o' : case 'O' :
1306 logopts(c == 'o', optarg);
1307 fdt[0] = fdt[1] = fdt[2] = -1;
1308 if (opts & OPT_FILTER)
1309 fdt[0] = IPL_LOGIPF;
1311 fdt[1] = IPL_LOGNAT;
1312 if (opts & OPT_STATE)
1313 fdt[2] = IPL_LOGSTATE;
1316 opts |= OPT_PORTNUM;
1322 s = strrchr(argv[0], '/');
1327 openlog(s, LOG_NDELAY|LOG_PID, LOGFAC);
1333 fdt[2] = IPL_LOGSTATE;
1334 iplfile[2] = optarg;
1340 opts |= OPT_VERBOSE;
1343 opts |= OPT_HEXBODY;
1357 * Default action is to only open the filter log file.
1359 if ((fdt[0] == -1) && (fdt[1] == -1) && (fdt[2] == -1))
1360 fdt[0] = IPL_LOGIPF;
1362 for (i = 0; i < 3; i++) {
1365 if (!strcmp(iplfile[i], "-"))
1368 if ((fd[i] = open(iplfile[i], O_RDONLY)) == -1) {
1369 (void) fprintf(stderr,
1370 "%s: open: %s\n", iplfile[i],
1375 if (fstat(fd[i], &sb) == -1) {
1376 (void) fprintf(stderr, "%d: fstat: %s\n",
1377 fd[i], STRERROR(errno));
1381 if (!(regular[i] = !S_ISCHR(sb.st_mode)))
1386 if (!(opts & OPT_SYSLOG)) {
1387 logfile = argv[optind];
1388 log = logfile ? fopen(logfile, "a") : stdout;
1390 (void) fprintf(stderr, "%s: fopen: %s\n",
1391 argv[optind], STRERROR(errno));
1395 setvbuf(log, NULL, _IONBF, 0);
1399 if (make_daemon && ((log != stdout) || (opts & OPT_SYSLOG))) {
1401 daemon(0, !(opts & OPT_SYSLOG));
1404 if ((pid = fork()) > 0)
1407 (void) fprintf(stderr, "%s: fork() failed: %s\n",
1408 argv[0], STRERROR(errno));
1413 if ((opts & OPT_SYSLOG))
1421 signal(SIGHUP, handlehup);
1423 for (doread = 1; doread; ) {
1426 for (i = 0; i < 3; i++) {
1431 if (ioctl(fd[i], FIONREAD, &tr) == -1) {
1432 if (opts & OPT_SYSLOG)
1434 "ioctl(FIONREAD): %m");
1436 perror("ioctl(FIONREAD)");
1441 tr = (lseek(fd[i], 0, SEEK_CUR) < sb.st_size);
1442 if (!tr && !(opts & OPT_TAIL))
1449 tr = read_log(fd[i], &n, buf, sizeof(buf));
1462 if (opts & OPT_SYSLOG)
1463 syslog(LOG_CRIT, "read: %m\n");
1469 if (opts & OPT_SYSLOG)
1470 syslog(LOG_CRIT, "aborting logging\n");
1472 fprintf(log, "aborting logging\n");
1479 print_log(fdt[i], log, buf, n);
1480 if (!(opts & OPT_SYSLOG))
1486 if (!nr && ((opts & OPT_TAIL) || devices))