2 * Copyright (C) 2001-2006 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 #define SOLARIS (defined(__SVR4) || defined(__svr4__)) && defined(sun)
10 #include <sys/types.h>
12 #include <sys/param.h>
18 #include <sys/socket.h>
19 #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(__hpux) && (!defined(__SVR4) && !defined(__GNUC__))
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #if !defined(__hpux) && !defined(linux)
48 # include <netinet/tcp_fsm.h>
51 #include <arpa/inet.h>
52 #include <arpa/nameser.h>
59 # include <sys/protosw.h>
60 # include <netinet/ip_var.h>
63 #include <netinet/tcp.h>
64 #include <netinet/ip_icmp.h>
69 #include "netinet/ip_compat.h"
70 #include <netinet/tcpip.h>
71 #include "netinet/ip_fil.h"
72 #include "netinet/ip_nat.h"
73 #include "netinet/ip_state.h"
74 #include "netinet/ip_proxy.h"
78 static const char sccsid[] = "@(#)ipmon.c 1.21 6/5/96 (C)1993-2000 Darren Reed";
79 static const char rcsid[] = "@(#)$Id: ipmon.c,v 1.33.2.18 2007/05/27 11:12:12 darrenr Exp $";
83 #if defined(sun) && !defined(SOLARIS2)
84 #define STRERROR(x) sys_errlist[x]
85 extern char *sys_errlist[];
87 #define STRERROR(x) strerror(x)
97 typedef struct icmp_subtype {
102 typedef struct icmp_type {
104 struct icmp_subtype *it_subtable;
110 #define IST_SZ(x) (sizeof(x)/sizeof(icmp_subtype_t))
113 struct flags tcpfl[] = {
126 static char *pidfile = "/etc/opt/ipf/ipmon.pid";
129 static char *pidfile = "/var/run/ipmon.pid";
131 static char *pidfile = "/etc/ipmon.pid";
135 static char line[2048];
137 static char *logfile = NULL;
138 static FILE *binarylog = NULL;
139 static char *binarylogfile = NULL;
140 static int donehup = 0;
141 static void usage __P((char *));
142 static void handlehup __P((int));
143 static void flushlogs __P((char *, FILE *));
144 static void print_log __P((int, FILE *, char *, int));
145 static void print_ipflog __P((FILE *, char *, int));
146 static void print_natlog __P((FILE *, char *, int));
147 static void print_statelog __P((FILE *, char *, int));
148 static int read_log __P((int, int *, char *, int));
149 static void write_pid __P((char *));
150 static char *icmpname __P((u_int, u_int));
151 static char *icmpname6 __P((u_int, u_int));
152 static icmp_type_t *find_icmptype __P((int, icmp_type_t *, size_t));
153 static icmp_subtype_t *find_icmpsubtype __P((int, icmp_subtype_t *, size_t));
155 static struct tm *get_tm __P((u_32_t));
157 static struct tm *get_tm __P((time_t));
160 char *hostname __P((int, int, u_32_t *));
161 char *portname __P((int, char *, u_int));
162 int main __P((int, char *[]));
164 static void logopts __P((int, char *));
165 static void init_tabs __P((void));
166 static char *getproto __P((u_int));
168 static char **protocols = NULL;
169 static char **udp_ports = NULL;
170 static char **tcp_ports = NULL;
171 static char *conf_file = NULL;
174 #define OPT_SYSLOG 0x001
175 #define OPT_RESOLVE 0x002
176 #define OPT_HEXBODY 0x004
177 #define OPT_VERBOSE 0x008
178 #define OPT_HEXHDR 0x010
179 #define OPT_TAIL 0x020
180 #define OPT_NAT 0x080
181 #define OPT_STATE 0x100
182 #define OPT_FILTER 0x200
183 #define OPT_PORTNUM 0x400
184 #define OPT_LOGALL (OPT_NAT|OPT_STATE|OPT_FILTER)
185 #define OPT_LOGBODY 0x800
187 #define HOSTNAME_V4(a,b) hostname((a), 4, (u_32_t *)&(b))
190 #define LOGFAC LOG_LOCAL0
195 static icmp_subtype_t icmpunreachnames[] = {
196 { ICMP_UNREACH_NET, "net" },
197 { ICMP_UNREACH_HOST, "host" },
198 { ICMP_UNREACH_PROTOCOL, "protocol" },
199 { ICMP_UNREACH_PORT, "port" },
200 { ICMP_UNREACH_NEEDFRAG, "needfrag" },
201 { ICMP_UNREACH_SRCFAIL, "srcfail" },
202 { ICMP_UNREACH_NET_UNKNOWN, "net_unknown" },
203 { ICMP_UNREACH_HOST_UNKNOWN, "host_unknown" },
204 { ICMP_UNREACH_NET, "isolated" },
205 { ICMP_UNREACH_NET_PROHIB, "net_prohib" },
206 { ICMP_UNREACH_NET_PROHIB, "host_prohib" },
207 { ICMP_UNREACH_TOSNET, "tosnet" },
208 { ICMP_UNREACH_TOSHOST, "toshost" },
209 { ICMP_UNREACH_ADMIN_PROHIBIT, "admin_prohibit" },
213 static icmp_subtype_t redirectnames[] = {
214 { ICMP_REDIRECT_NET, "net" },
215 { ICMP_REDIRECT_HOST, "host" },
216 { ICMP_REDIRECT_TOSNET, "tosnet" },
217 { ICMP_REDIRECT_TOSHOST, "toshost" },
221 static icmp_subtype_t timxceednames[] = {
222 { ICMP_TIMXCEED_INTRANS, "transit" },
223 { ICMP_TIMXCEED_REASS, "reassem" },
227 static icmp_subtype_t paramnames[] = {
228 { ICMP_PARAMPROB_ERRATPTR, "errata_pointer" },
229 { ICMP_PARAMPROB_OPTABSENT, "optmissing" },
230 { ICMP_PARAMPROB_LENGTH, "length" },
234 static icmp_type_t icmptypes[] = {
235 { ICMP_ECHOREPLY, NULL, 0, "echoreply" },
236 { -1, NULL, 0, NULL },
237 { -1, NULL, 0, NULL },
238 { ICMP_UNREACH, icmpunreachnames,
239 IST_SZ(icmpunreachnames),"unreach" },
240 { ICMP_SOURCEQUENCH, NULL, 0, "sourcequench" },
241 { ICMP_REDIRECT, redirectnames,
242 IST_SZ(redirectnames), "redirect" },
243 { -1, NULL, 0, NULL },
244 { -1, NULL, 0, NULL },
245 { ICMP_ECHO, NULL, 0, "echo" },
246 { ICMP_ROUTERADVERT, NULL, 0, "routeradvert" },
247 { ICMP_ROUTERSOLICIT, NULL, 0, "routersolicit" },
248 { ICMP_TIMXCEED, timxceednames,
249 IST_SZ(timxceednames), "timxceed" },
250 { ICMP_PARAMPROB, paramnames,
251 IST_SZ(paramnames), "paramprob" },
252 { ICMP_TSTAMP, NULL, 0, "timestamp" },
253 { ICMP_TSTAMPREPLY, NULL, 0, "timestampreply" },
254 { ICMP_IREQ, NULL, 0, "inforeq" },
255 { ICMP_IREQREPLY, NULL, 0, "inforeply" },
256 { ICMP_MASKREQ, NULL, 0, "maskreq" },
257 { ICMP_MASKREPLY, NULL, 0, "maskreply" },
258 { -2, NULL, 0, NULL }
261 static icmp_subtype_t icmpredirect6[] = {
262 { ICMP6_DST_UNREACH_NOROUTE, "noroute" },
263 { ICMP6_DST_UNREACH_ADMIN, "admin" },
264 { ICMP6_DST_UNREACH_NOTNEIGHBOR, "neighbour" },
265 { ICMP6_DST_UNREACH_ADDR, "address" },
266 { ICMP6_DST_UNREACH_NOPORT, "noport" },
270 static icmp_subtype_t icmptimexceed6[] = {
271 { ICMP6_TIME_EXCEED_TRANSIT, "intransit" },
272 { ICMP6_TIME_EXCEED_REASSEMBLY, "reassem" },
276 static icmp_subtype_t icmpparamprob6[] = {
277 { ICMP6_PARAMPROB_HEADER, "header" },
278 { ICMP6_PARAMPROB_NEXTHEADER, "nextheader" },
279 { ICMP6_PARAMPROB_OPTION, "option" },
283 static icmp_subtype_t icmpquerysubject6[] = {
284 { ICMP6_NI_SUBJ_IPV6, "ipv6" },
285 { ICMP6_NI_SUBJ_FQDN, "fqdn" },
286 { ICMP6_NI_SUBJ_IPV4, "ipv4" },
290 static icmp_subtype_t icmpnodeinfo6[] = {
291 { ICMP6_NI_SUCCESS, "success" },
292 { ICMP6_NI_REFUSED, "refused" },
293 { ICMP6_NI_UNKNOWN, "unknown" },
297 static icmp_subtype_t icmprenumber6[] = {
298 { ICMP6_ROUTER_RENUMBERING_COMMAND, "command" },
299 { ICMP6_ROUTER_RENUMBERING_RESULT, "result" },
300 { ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET, "seqnum_reset" },
304 static icmp_type_t icmptypes6[] = {
305 { 0, NULL, 0, NULL },
306 { ICMP6_DST_UNREACH, icmpredirect6,
307 IST_SZ(icmpredirect6), "unreach" },
308 { ICMP6_PACKET_TOO_BIG, NULL, 0, "toobig" },
309 { ICMP6_TIME_EXCEEDED, icmptimexceed6,
310 IST_SZ(icmptimexceed6), "timxceed" },
311 { ICMP6_PARAM_PROB, icmpparamprob6,
312 IST_SZ(icmpparamprob6), "paramprob" },
313 { ICMP6_ECHO_REQUEST, NULL, 0, "echo" },
314 { ICMP6_ECHO_REPLY, NULL, 0, "echoreply" },
315 { ICMP6_MEMBERSHIP_QUERY, icmpquerysubject6,
316 IST_SZ(icmpquerysubject6), "groupmemberquery" },
317 { ICMP6_MEMBERSHIP_REPORT,NULL, 0, "groupmemberreport" },
318 { ICMP6_MEMBERSHIP_REDUCTION,NULL, 0, "groupmemberterm" },
319 { ND_ROUTER_SOLICIT, NULL, 0, "routersolicit" },
320 { ND_ROUTER_ADVERT, NULL, 0, "routeradvert" },
321 { ND_NEIGHBOR_SOLICIT, NULL, 0, "neighborsolicit" },
322 { ND_NEIGHBOR_ADVERT, NULL, 0, "neighboradvert" },
323 { ND_REDIRECT, NULL, 0, "redirect" },
324 { ICMP6_ROUTER_RENUMBERING, icmprenumber6,
325 IST_SZ(icmprenumber6), "routerrenumber" },
326 { ICMP6_WRUREQUEST, NULL, 0, "whoareyourequest" },
327 { ICMP6_WRUREPLY, NULL, 0, "whoareyoureply" },
328 { ICMP6_FQDN_QUERY, NULL, 0, "fqdnquery" },
329 { ICMP6_FQDN_REPLY, NULL, 0, "fqdnreply" },
330 { ICMP6_NI_QUERY, icmpnodeinfo6,
331 IST_SZ(icmpnodeinfo6), "nodeinforequest" },
332 { ICMP6_NI_REPLY, NULL, 0, "nodeinforeply" },
333 { MLD6_MTRACE_RESP, NULL, 0, "mtraceresponse" },
334 { MLD6_MTRACE, NULL, 0, "mtracerequest" },
335 { -2, NULL, 0, NULL }
338 static icmp_subtype_t *find_icmpsubtype(type, table, tablesz)
340 icmp_subtype_t *table;
349 if ((type < 0) || (type > table[tablesz - 2].ist_val))
353 if (table[type].ist_val == type)
356 for (i = 0, ist = table; ist->ist_val != -2; i++, ist++)
357 if (ist->ist_val == type)
363 static icmp_type_t *find_icmptype(type, table, tablesz)
374 if ((type < 0) || (type > table[tablesz - 2].it_val))
378 if (table[type].it_val == type)
381 for (i = 0, it = table; it->it_val != -2; i++, it++)
382 if (it->it_val == type)
388 static void handlehup(sig)
391 signal(SIGHUP, handlehup);
396 static void init_tabs()
403 if (protocols != NULL) {
404 for (i = 0; i < 256; i++)
405 if (protocols[i] != NULL) {
412 protocols = (char **)malloc(256 * sizeof(*protocols));
413 if (protocols != NULL) {
414 bzero((char *)protocols, 256 * sizeof(*protocols));
417 while ((p = getprotoent()) != NULL)
418 if (p->p_proto >= 0 && p->p_proto <= 255 &&
419 p->p_name != NULL && protocols[p->p_proto] == NULL)
420 protocols[p->p_proto] = strdup(p->p_name);
426 free(protocols[252]);
428 protocols[252] = NULL;
432 if (udp_ports != NULL) {
433 for (i = 0; i < 65536; i++)
434 if (udp_ports[i] != NULL) {
441 udp_ports = (char **)malloc(65536 * sizeof(*udp_ports));
442 if (udp_ports != NULL)
443 bzero((char *)udp_ports, 65536 * sizeof(*udp_ports));
445 if (tcp_ports != NULL) {
446 for (i = 0; i < 65536; i++)
447 if (tcp_ports[i] != NULL) {
454 tcp_ports = (char **)malloc(65536 * sizeof(*tcp_ports));
455 if (tcp_ports != NULL)
456 bzero((char *)tcp_ports, 65536 * sizeof(*tcp_ports));
459 while ((s = getservent()) != NULL) {
460 if (s->s_proto == NULL)
462 else if (!strcmp(s->s_proto, "tcp")) {
463 port = ntohs(s->s_port);
466 } else if (!strcmp(s->s_proto, "udp")) {
467 port = ntohs(s->s_port);
472 if ((port < 0 || port > 65535) || (name == NULL))
475 tab[port] = strdup(name);
481 static char *getproto(p)
488 s = protocols ? protocols[p] : NULL;
490 sprintf(pnum, "%u", p);
497 static int read_log(fd, lenp, buf, bufsize)
498 int fd, bufsize, *lenp;
503 nr = read(fd, buf, bufsize);
506 if ((nr < 0) && (errno != EINTR))
513 char *hostname(res, v, ip)
517 # define MAX_INETA 16
518 static char hname[MAXHOSTNAMELEN + MAX_INETA + 3];
520 static char hostbuf[MAXHOSTNAMELEN+1];
528 return inet_ntoa(ipa);
529 hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
531 return inet_ntoa(ipa);
532 sprintf(hname, "%.*s[%s]", MAXHOSTNAMELEN, hp->h_name,
537 (void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
538 hostbuf[MAXHOSTNAMELEN] = '\0';
546 char *portname(res, proto, port)
551 static char pname[8];
556 (void) sprintf(pname, "%u", port);
557 if (!res || (opts & OPT_PORTNUM))
560 if (!strcmp(proto, "tcp"))
562 else if (!strcmp(proto, "udp"))
570 static char *icmpname(type, code)
574 static char name[80];
580 it = find_icmptype(type, icmptypes, sizeof(icmptypes) / sizeof(*it));
585 sprintf(name, "icmptype(%d)/", type);
587 sprintf(name, "%s/", s);
590 if (it != NULL && it->it_subtable != NULL)
591 ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
593 if (ist != NULL && ist->ist_name != NULL)
594 strcat(name, ist->ist_name);
596 sprintf(name + strlen(name), "%d", code);
601 static char *icmpname6(type, code)
605 static char name[80];
611 it = find_icmptype(type, icmptypes6, sizeof(icmptypes6) / sizeof(*it));
616 sprintf(name, "icmpv6type(%d)/", type);
618 sprintf(name, "%s/", s);
621 if (it != NULL && it->it_subtable != NULL)
622 ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
624 if (ist != NULL && ist->ist_name != NULL)
625 strcat(name, ist->ist_name);
627 sprintf(name + strlen(name), "%d", code);
633 void dumphex(log, dopts, buf, len)
641 u_char *s = (u_char *)buf, *t = (u_char *)hline;
643 if (buf == NULL || len == 0)
648 for (i = len, j = 0; i; i--, j++, s++) {
649 if (j && !(j & 0xf)) {
652 if ((dopts & OPT_SYSLOG))
653 syslog(LOG_INFO, "%s", hline);
654 else if (log != NULL)
659 sprintf((char *)t, "%02x", *s & 0xff);
661 if (!((j + 1) & 0xf)) {
663 sprintf((char *)t, " ");
665 for (k = 16; k; k--, s++)
666 *t++ = (ISPRINT(*s) ? *s : '.');
675 for (k = 16 - (j & 0xf); k; k--) {
680 sprintf((char *)t, " ");
683 for (k = j & 0xf; k; k--, s++)
684 *t++ = (ISPRINT(*s) ? *s : '.');
688 if ((dopts & OPT_SYSLOG) != 0)
689 syslog(LOG_INFO, "%s", hline);
690 else if (log != NULL) {
697 static struct tm *get_tm(sec)
712 static void print_natlog(log, buf, blen)
718 iplog_t *ipl = (iplog_t *)buf;
724 nl = (struct natlog *)((char *)ipl + sizeof(*ipl));
725 res = (opts & OPT_RESOLVE) ? 1 : 0;
726 tm = get_tm(ipl->ipl_sec);
728 if (!(opts & OPT_SYSLOG)) {
729 (void) strftime(t, len, "%d/%m/%Y ", tm);
734 (void) strftime(t, len, "%T", tm);
736 (void) sprintf(t, ".%-.6ld @%hd ", ipl->ipl_usec, nl->nl_rule + 1);
739 if (nl->nl_type == NL_NEWMAP)
740 strcpy(t, "NAT:MAP ");
741 else if (nl->nl_type == NL_NEWRDR)
742 strcpy(t, "NAT:RDR ");
743 else if (nl->nl_type == NL_FLUSH)
744 strcpy(t, "NAT:FLUSH ");
745 else if (nl->nl_type == NL_EXPIRE)
746 strcpy(t, "NAT:EXPIRE ");
747 else if (nl->nl_type == NL_NEWBIMAP)
748 strcpy(t, "NAT:BIMAP ");
749 else if (nl->nl_type == NL_NEWBLOCK)
750 strcpy(t, "NAT:MAPBLOCK ");
751 else if (nl->nl_type == NL_CLONE)
752 strcpy(t, "NAT:CLONE ");
754 sprintf(t, "Type: %d ", nl->nl_type);
757 proto = getproto(nl->nl_p);
759 (void) sprintf(t, "%s,%s <- -> ", HOSTNAME_V4(res, nl->nl_inip),
760 portname(res, proto, (u_int)nl->nl_inport));
762 (void) sprintf(t, "%s,%s ", HOSTNAME_V4(res, nl->nl_outip),
763 portname(res, proto, (u_int)nl->nl_outport));
765 (void) sprintf(t, "[%s,%s]", HOSTNAME_V4(res, nl->nl_origip),
766 portname(res, proto, (u_int)nl->nl_origport));
768 if (nl->nl_type == NL_EXPIRE) {
770 (void) sprintf(t, " Pkts %qd/%qd Bytes %qd/%qd",
771 (long long)nl->nl_pkts[0],
772 (long long)nl->nl_pkts[1],
773 (long long)nl->nl_bytes[0],
774 (long long)nl->nl_bytes[1]);
776 (void) sprintf(t, " Pkts %ld/%ld Bytes %ld/%ld",
777 nl->nl_pkts[0], nl->nl_pkts[1],
778 nl->nl_bytes[0], nl->nl_bytes[1]);
785 if (opts & OPT_SYSLOG)
786 syslog(LOG_INFO, "%s", line);
787 else if (log != NULL)
788 (void) fprintf(log, "%s", line);
792 static void print_statelog(log, buf, blen)
798 iplog_t *ipl = (iplog_t *)buf;
799 char *t = line, *proto;
803 sl = (struct ipslog *)((char *)ipl + sizeof(*ipl));
804 res = (opts & OPT_RESOLVE) ? 1 : 0;
805 tm = get_tm(ipl->ipl_sec);
807 if (!(opts & OPT_SYSLOG)) {
808 (void) strftime(t, len, "%d/%m/%Y ", tm);
813 (void) strftime(t, len, "%T", tm);
815 (void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
818 switch (sl->isl_type)
821 strcpy(t, "STATE:NEW ");
825 strcpy(t, "STATE:CLONED ");
829 if ((sl->isl_p == IPPROTO_TCP) &&
830 (sl->isl_state[0] > IPF_TCPS_ESTABLISHED ||
831 sl->isl_state[1] > IPF_TCPS_ESTABLISHED))
832 strcpy(t, "STATE:CLOSE ");
834 strcpy(t, "STATE:EXPIRE ");
838 strcpy(t, "STATE:FLUSH ");
841 case ISL_INTERMEDIATE :
842 strcpy(t, "STATE:INTERMEDIATE ");
846 strcpy(t, "STATE:REMOVE ");
850 strcpy(t, "STATE:KILLED ");
854 strcpy(t, "STATE:UNLOAD ");
858 sprintf(t, "Type: %d ", sl->isl_type);
863 proto = getproto(sl->isl_p);
865 if (sl->isl_p == IPPROTO_TCP || sl->isl_p == IPPROTO_UDP) {
866 (void) sprintf(t, "%s,%s -> ",
867 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_src),
868 portname(res, proto, (u_int)sl->isl_sport));
870 (void) sprintf(t, "%s,%s PR %s",
871 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
872 portname(res, proto, (u_int)sl->isl_dport), proto);
873 } else if (sl->isl_p == IPPROTO_ICMP) {
874 (void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
875 (u_32_t *)&sl->isl_src));
877 (void) sprintf(t, "%s PR icmp %d",
878 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
880 } else if (sl->isl_p == IPPROTO_ICMPV6) {
881 (void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
882 (u_32_t *)&sl->isl_src));
884 (void) sprintf(t, "%s PR icmpv6 %d",
885 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
888 (void) sprintf(t, "%s -> ",
889 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_src));
891 (void) sprintf(t, "%s PR %s",
892 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
896 if (sl->isl_tag != FR_NOLOGTAG) {
897 (void) sprintf(t, " tag %u", sl->isl_tag);
900 if (sl->isl_type != ISL_NEW) {
904 " Forward: Pkts in %" PRId64 " Bytes in %" PRId64
905 " Pkts out %" PRId64 " Bytes out %" PRId64
906 " Backward: Pkts in %" PRId64 " Bytes in %" PRId64
907 " Pkts out %" PRId64 " Bytes out %" PRId64,
909 " 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",
912 " 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",
914 sl->isl_pkts[0], sl->isl_bytes[0],
915 sl->isl_pkts[1], sl->isl_bytes[1],
916 sl->isl_pkts[2], sl->isl_bytes[2],
917 sl->isl_pkts[3], sl->isl_bytes[3]);
924 if (opts & OPT_SYSLOG)
925 syslog(LOG_INFO, "%s", line);
926 else if (log != NULL)
927 (void) fprintf(log, "%s", line);
931 static void print_log(logtype, log, buf, blen)
937 char *bp = NULL, *bpo = NULL;
941 ipl = (iplog_t *)buf;
942 if ((u_long)ipl & (sizeof(long)-1)) {
945 bp = (char *)malloc(blen);
946 bcopy((char *)ipl, bp, blen);
955 psize = ipl->ipl_dsize;
960 fwrite(buf, psize, 1, binarylog);
964 if (logtype == IPL_LOGIPF) {
965 if (ipl->ipl_magic == IPL_MAGIC)
966 print_ipflog(log, buf, psize);
968 } else if (logtype == IPL_LOGNAT) {
969 if (ipl->ipl_magic == IPL_MAGIC_NAT)
970 print_natlog(log, buf, psize);
972 } else if (logtype == IPL_LOGSTATE) {
973 if (ipl->ipl_magic == IPL_MAGIC_STATE)
974 print_statelog(log, buf, psize);
986 static void print_ipflog(log, buf, blen)
996 int i, v, lvl, res, len, off, plen, ipoff, defaction;
1006 ipl = (iplog_t *)buf;
1007 ipf = (ipflog_t *)((char *)buf + sizeof(*ipl));
1008 ip = (ip_t *)((char *)ipf + sizeof(*ipf));
1010 res = (opts & OPT_RESOLVE) ? 1 : 0;
1013 tm = get_tm(ipl->ipl_sec);
1016 if (!(opts & OPT_SYSLOG)) {
1017 (void) strftime(t, len, "%d/%m/%Y ", tm);
1022 (void) strftime(t, len, "%T", tm);
1024 (void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
1026 if (ipl->ipl_count > 1) {
1027 (void) sprintf(t, "%dx ", ipl->ipl_count);
1030 #if (defined(MENTAT) || \
1031 (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
1032 (defined(__FreeBSD__) && (__FreeBSD_version >= 501113)) || \
1033 (defined(OpenBSD) && (OpenBSD >= 199603))) || defined(linux)
1035 char ifname[sizeof(ipf->fl_ifname) + 1];
1037 strncpy(ifname, ipf->fl_ifname, sizeof(ipf->fl_ifname));
1038 ifname[sizeof(ipf->fl_ifname)] = '\0';
1039 (void) sprintf(t, "%s", ifname);
1041 # if defined(MENTAT) || defined(linux)
1042 if (ISALPHA(*(t - 1))) {
1043 sprintf(t, "%d", ipf->fl_unit);
1049 for (len = 0; len < 3; len++)
1050 if (ipf->fl_ifname[len] == '\0')
1052 if (ipf->fl_ifname[len])
1054 (void) sprintf(t, "%*.*s%u", len, len, ipf->fl_ifname, ipf->fl_unit);
1057 if ((ipf->fl_group[0] == (char)~0) && (ipf->fl_group[1] == '\0'))
1059 else if (ipf->fl_group[0] == '\0')
1060 (void) strcpy(t, " @0:");
1062 (void) sprintf(t, " @%s:", ipf->fl_group);
1064 if (ipf->fl_rule == 0xffffffff)
1067 (void) sprintf(t, "%u ", ipf->fl_rule + 1);
1072 if (ipf->fl_lflags & FI_SHORT) {
1077 if (FR_ISPASS(ipf->fl_flags)) {
1078 if (ipf->fl_flags & FR_LOGP)
1082 } else if (FR_ISBLOCK(ipf->fl_flags)) {
1083 if (ipf->fl_flags & FR_LOGB)
1088 } else if ((ipf->fl_flags & FR_LOGMASK) == FR_LOG) {
1091 } else if (ipf->fl_flags & FF_LOGNOMATCH) {
1097 if (ipf->fl_loglevel != 0xffff)
1098 lvl = ipf->fl_loglevel;
1108 p = (u_short)ip6->ip6_nxt;
1109 s = (u_32_t *)&ip6->ip6_src;
1110 d = (u_32_t *)&ip6->ip6_dst;
1111 plen = hl + ntohs(ip6->ip6_plen);
1116 } else if (v == 4) {
1117 hl = IP_HL(ip) << 2;
1119 off = ipoff & IP_OFFMASK;
1120 p = (u_short)ip->ip_p;
1121 s = (u_32_t *)&ip->ip_src;
1122 d = (u_32_t *)&ip->ip_dst;
1127 proto = getproto(p);
1129 if ((p == IPPROTO_TCP || p == IPPROTO_UDP) && !off) {
1130 tp = (tcphdr_t *)((char *)ip + hl);
1131 if (!(ipf->fl_lflags & FI_SHORT)) {
1132 (void) sprintf(t, "%s,%s -> ", hostname(res, v, s),
1133 portname(res, proto, (u_int)tp->th_sport));
1135 (void) sprintf(t, "%s,%s PR %s len %hu %hu",
1136 hostname(res, v, d),
1137 portname(res, proto, (u_int)tp->th_dport),
1141 if (p == IPPROTO_TCP) {
1144 for (i = 0; tcpfl[i].value; i++)
1145 if (tp->th_flags & tcpfl[i].value)
1146 *t++ = tcpfl[i].flag;
1147 if (opts & OPT_VERBOSE) {
1148 (void) sprintf(t, " %lu %lu %hu",
1149 (u_long)(ntohl(tp->th_seq)),
1150 (u_long)(ntohl(tp->th_ack)),
1157 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1159 (void) sprintf(t, "%s PR %s len %hu %hu",
1160 hostname(res, v, d), proto, hl, plen);
1162 } else if ((p == IPPROTO_ICMPV6) && !off && (v == 6)) {
1163 ic = (struct icmp *)((char *)ip + hl);
1164 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1166 (void) sprintf(t, "%s PR icmpv6 len %hu %hu icmpv6 %s",
1167 hostname(res, v, d), hl, plen,
1168 icmpname6(ic->icmp_type, ic->icmp_code));
1169 } else if ((p == IPPROTO_ICMP) && !off && (v == 4)) {
1170 ic = (struct icmp *)((char *)ip + hl);
1171 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1173 (void) sprintf(t, "%s PR icmp len %hu %hu icmp %s",
1174 hostname(res, v, d), hl, plen,
1175 icmpname(ic->icmp_type, ic->icmp_code));
1176 if (ic->icmp_type == ICMP_UNREACH ||
1177 ic->icmp_type == ICMP_SOURCEQUENCH ||
1178 ic->icmp_type == ICMP_PARAMPROB ||
1179 ic->icmp_type == ICMP_REDIRECT ||
1180 ic->icmp_type == ICMP_TIMXCEED) {
1182 i = ntohs(ipc->ip_len);
1184 * XXX - try to guess endian of ip_len in ICMP
1189 ipoff = ntohs(ipc->ip_off);
1190 proto = getproto(ipc->ip_p);
1192 if (!(ipoff & IP_OFFMASK) &&
1193 ((ipc->ip_p == IPPROTO_TCP) ||
1194 (ipc->ip_p == IPPROTO_UDP))) {
1195 tp = (tcphdr_t *)((char *)ipc + hl);
1197 (void) sprintf(t, " for %s,%s -",
1198 HOSTNAME_V4(res, ipc->ip_src),
1199 portname(res, proto,
1200 (u_int)tp->th_sport));
1202 (void) sprintf(t, " %s,%s PR %s len %hu %hu",
1203 HOSTNAME_V4(res, ipc->ip_dst),
1204 portname(res, proto,
1205 (u_int)tp->th_dport),
1206 proto, IP_HL(ipc) << 2, i);
1207 } else if (!(ipoff & IP_OFFMASK) &&
1208 (ipc->ip_p == IPPROTO_ICMP)) {
1209 icmp = (icmphdr_t *)((char *)ipc + hl);
1212 (void) sprintf(t, " for %s -",
1213 HOSTNAME_V4(res, ipc->ip_src));
1216 " %s PR icmp len %hu %hu icmp %d/%d",
1217 HOSTNAME_V4(res, ipc->ip_dst),
1219 icmp->icmp_type, icmp->icmp_code);
1222 (void) sprintf(t, " for %s -",
1223 HOSTNAME_V4(res, ipc->ip_src));
1225 (void) sprintf(t, " %s PR %s len %hu (%hu)",
1226 HOSTNAME_V4(res, ipc->ip_dst), proto,
1227 IP_HL(ipc) << 2, i);
1229 if (ipoff & IP_OFFMASK) {
1231 "(frag %d:%hu@%hu%s%s)",
1233 i - (IP_HL(ipc) << 2),
1234 (ipoff & IP_OFFMASK) << 3,
1235 ipoff & IP_MF ? "+" : "",
1236 ipoff & IP_DF ? "-" : "");
1242 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1244 (void) sprintf(t, "%s PR %s len %hu (%hu)",
1245 hostname(res, v, d), proto, hl, plen);
1247 if (off & IP_OFFMASK)
1248 (void) sprintf(t, " (frag %d:%hu@%hu%s%s)",
1250 plen - hl, (off & IP_OFFMASK) << 3,
1251 ipoff & IP_MF ? "+" : "",
1252 ipoff & IP_DF ? "-" : "");
1257 if (ipf->fl_flags & FR_KEEPSTATE) {
1258 (void) strcpy(t, " K-S");
1262 if (ipf->fl_flags & FR_KEEPFRAG) {
1263 (void) strcpy(t, " K-F");
1267 if (ipf->fl_dir == 0)
1269 else if (ipf->fl_dir == 1)
1272 if (ipf->fl_logtag != 0) {
1273 sprintf(t, " log-tag %d", ipf->fl_logtag);
1276 if (ipf->fl_nattag.ipt_num[0] != 0) {
1277 strcpy(t, " nat-tag ");
1279 strncpy(t, ipf->fl_nattag.ipt_tag, sizeof(ipf->fl_nattag));
1282 if ((ipf->fl_lflags & FI_LOWTTL) != 0) {
1283 strcpy(t, " low-ttl");
1286 if ((ipf->fl_lflags & FI_OOW) != 0) {
1290 if ((ipf->fl_lflags & FI_BAD) != 0) {
1294 if ((ipf->fl_lflags & FI_NATED) != 0) {
1298 if ((ipf->fl_lflags & FI_BADNAT) != 0) {
1299 strcpy(t, " bad-NAT");
1302 if ((ipf->fl_lflags & FI_BADSRC) != 0) {
1303 strcpy(t, " bad-src");
1306 if ((ipf->fl_lflags & FI_MULTICAST) != 0) {
1307 strcpy(t, " multicast");
1310 if ((ipf->fl_lflags & FI_BROADCAST) != 0) {
1311 strcpy(t, " broadcast");
1314 if ((ipf->fl_lflags & (FI_MULTICAST|FI_BROADCAST|FI_MBCAST)) ==
1316 strcpy(t, " mbcast");
1322 if (conf_file != NULL)
1323 defaction = check_action(buf, line, opts, lvl);
1324 if (defaction == 0) {
1325 if (opts & OPT_SYSLOG)
1326 syslog(lvl, "%s", line);
1327 else if (log != NULL)
1328 (void) fprintf(log, "%s", line);
1330 if (opts & OPT_HEXHDR)
1331 dumphex(log, opts, buf,
1332 sizeof(iplog_t) + sizeof(*ipf));
1333 if (opts & OPT_HEXBODY)
1334 dumphex(log, opts, (char *)ip,
1335 ipf->fl_plen + ipf->fl_hlen);
1336 else if ((opts & OPT_LOGBODY) && (ipf->fl_flags & FR_LOGBODY))
1337 dumphex(log, opts, (char *)ip + ipf->fl_hlen,
1343 static void usage(prog)
1346 fprintf(stderr, "%s: [-NFhstvxX] [-f <logfile>]\n", prog);
1351 static void write_pid(file)
1357 if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0644)) >= 0) {
1358 fp = fdopen(fd, "w");
1362 "unable to open/create pid file: %s\n", file);
1365 fprintf(fp, "%d", getpid());
1371 static void flushlogs(file, log)
1375 int fd, flushed = 0;
1377 if ((fd = open(file, O_RDWR)) == -1) {
1378 (void) fprintf(stderr, "%s: open: %s\n",
1379 file, STRERROR(errno));
1383 if (ioctl(fd, SIOCIPFFB, &flushed) == 0) {
1384 printf("%d bytes flushed from log buffer\n",
1388 perror("SIOCIPFFB");
1392 if (opts & OPT_SYSLOG) {
1393 syslog(LOG_INFO, "%d bytes flushed from log\n",
1395 } else if ((log != stdout) && (log != NULL)) {
1396 fprintf(log, "%d bytes flushed from log\n", flushed);
1402 static void logopts(turnon, options)
1409 for (s = options; *s; s++)
1420 flags |= OPT_FILTER;
1423 fprintf(stderr, "Unknown log option %c\n", *s);
1435 int main(argc, argv)
1442 int fd[3], doread, n, i;
1443 int tr, nr, regular[3], c;
1444 int fdt[3], devices = 0, make_daemon = 0;
1445 char buf[DEFAULT_IPFLOGSIZE], *iplfile[3], *s;
1447 extern char *optarg;
1449 fd[0] = fd[1] = fd[2] = -1;
1450 fdt[0] = fdt[1] = fdt[2] = -1;
1451 iplfile[0] = IPL_NAME;
1452 iplfile[1] = IPNAT_NAME;
1453 iplfile[2] = IPSTATE_NAME;
1455 while ((c = getopt(argc, argv,
1456 "?abB:C:Df:FhL:nN:o:O:pP:sS:tvxX")) != -1)
1461 fdt[0] = IPL_LOGIPF;
1462 fdt[1] = IPL_LOGNAT;
1463 fdt[2] = IPL_LOGSTATE;
1466 opts |= OPT_LOGBODY;
1469 binarylogfile = optarg;
1470 binarylog = fopen(optarg, "a");
1478 case 'f' : case 'I' :
1480 fdt[0] = IPL_LOGIPF;
1481 iplfile[0] = optarg;
1484 flushlogs(iplfile[0], log);
1485 flushlogs(iplfile[1], log);
1486 flushlogs(iplfile[2], log);
1489 logfac = fac_findname(optarg);
1492 "Unknown syslog facility '%s'\n",
1498 opts |= OPT_RESOLVE;
1502 fdt[1] = IPL_LOGNAT;
1503 iplfile[1] = optarg;
1505 case 'o' : case 'O' :
1506 logopts(c == 'o', optarg);
1507 fdt[0] = fdt[1] = fdt[2] = -1;
1508 if (opts & OPT_FILTER)
1509 fdt[0] = IPL_LOGIPF;
1511 fdt[1] = IPL_LOGNAT;
1512 if (opts & OPT_STATE)
1513 fdt[2] = IPL_LOGSTATE;
1516 opts |= OPT_PORTNUM;
1522 s = strrchr(argv[0], '/');
1527 openlog(s, LOG_NDELAY|LOG_PID, logfac);
1534 fdt[2] = IPL_LOGSTATE;
1535 iplfile[2] = optarg;
1541 opts |= OPT_VERBOSE;
1544 opts |= OPT_HEXBODY;
1557 if (load_config(conf_file) == -1)
1561 * Default action is to only open the filter log file.
1563 if ((fdt[0] == -1) && (fdt[1] == -1) && (fdt[2] == -1))
1564 fdt[0] = IPL_LOGIPF;
1566 for (i = 0; i < 3; i++) {
1569 if (!strcmp(iplfile[i], "-"))
1572 if ((fd[i] = open(iplfile[i], O_RDONLY)) == -1) {
1573 (void) fprintf(stderr,
1574 "%s: open: %s\n", iplfile[i],
1579 if (fstat(fd[i], &sb) == -1) {
1580 (void) fprintf(stderr, "%d: fstat: %s\n",
1581 fd[i], STRERROR(errno));
1585 if (!(regular[i] = !S_ISCHR(sb.st_mode)))
1590 if (!(opts & OPT_SYSLOG)) {
1591 logfile = argv[optind];
1592 log = logfile ? fopen(logfile, "a") : stdout;
1594 (void) fprintf(stderr, "%s: fopen: %s\n",
1595 argv[optind], STRERROR(errno));
1599 setvbuf(log, NULL, _IONBF, 0);
1603 if (make_daemon && ((log != stdout) || (opts & OPT_SYSLOG))) {
1605 daemon(0, !(opts & OPT_SYSLOG));
1608 if ((pid = fork()) > 0)
1611 (void) fprintf(stderr, "%s: fork() failed: %s\n",
1612 argv[0], STRERROR(errno));
1617 if ((opts & OPT_SYSLOG))
1625 signal(SIGHUP, handlehup);
1627 for (doread = 1; doread; ) {
1630 for (i = 0; i < 3; i++) {
1635 if (ioctl(fd[i], FIONREAD, &tr) == -1) {
1636 if (opts & OPT_SYSLOG)
1638 "ioctl(FIONREAD): %m");
1640 perror("ioctl(FIONREAD)");
1645 tr = (lseek(fd[i], 0, SEEK_CUR) < sb.st_size);
1646 if (!tr && !(opts & OPT_TAIL))
1654 tr = read_log(fd[i], &n, buf, sizeof(buf));
1656 if (logfile && (fp = fopen(logfile, "a"))) {
1660 if (binarylogfile &&
1661 (fp = fopen(binarylogfile, "a"))) {
1666 if (conf_file != NULL)
1667 load_config(conf_file);
1674 if (opts & OPT_SYSLOG)
1675 syslog(LOG_CRIT, "read: %m\n");
1681 if (opts & OPT_SYSLOG)
1682 syslog(LOG_CRIT, "aborting logging\n");
1683 else if (log != NULL)
1684 fprintf(log, "aborting logging\n");
1691 print_log(fdt[i], log, buf, n);
1692 if (!(opts & OPT_SYSLOG))
1698 if (!nr && ((opts & OPT_TAIL) || devices))