4 * Copyright (C) 1993-2001, 2003 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
9 #define SOLARIS (defined(__SVR4) || defined(__svr4__)) && defined(sun)
12 #include <sys/types.h>
14 #include <sys/param.h>
20 #include <sys/socket.h>
21 #include <sys/ioctl.h>
29 #if !defined(__SVR4) && !defined(__svr4__)
30 # if (__FreeBSD_version >= 300000)
31 # include <sys/dirent.h>
36 # include <sys/filio.h>
37 # include <sys/byteorder.h>
39 #if !defined(__hpux) && (!defined(__SVR4) && !defined(__GNUC__))
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #if !defined(__hpux) && !defined(linux)
50 # include <netinet/tcp_fsm.h>
53 #include <arpa/inet.h>
54 #include <arpa/nameser.h>
61 # include <sys/protosw.h>
62 # include <netinet/ip_var.h>
65 #include <netinet/tcp.h>
66 #include <netinet/ip_icmp.h>
71 #include "netinet/ip_compat.h"
72 #include <netinet/tcpip.h>
73 #include "netinet/ip_fil.h"
74 #include "netinet/ip_nat.h"
75 #include "netinet/ip_state.h"
76 #include "netinet/ip_proxy.h"
80 static const char sccsid[] = "@(#)ipmon.c 1.21 6/5/96 (C)1993-2000 Darren Reed";
81 static const char rcsid[] = "@(#)$Id: ipmon.c,v 1.33.2.15 2006/03/18 06:59:39 darrenr Exp $";
85 #if defined(sun) && !defined(SOLARIS2)
86 #define STRERROR(x) sys_errlist[x]
87 extern char *sys_errlist[];
89 #define STRERROR(x) strerror(x)
99 typedef struct icmp_subtype {
104 typedef struct icmp_type {
106 struct icmp_subtype *it_subtable;
112 #define IST_SZ(x) (sizeof(x)/sizeof(icmp_subtype_t))
115 struct flags tcpfl[] = {
128 static char *pidfile = "/etc/opt/ipf/ipmon.pid";
131 static char *pidfile = "/var/run/ipmon.pid";
133 static char *pidfile = "/etc/ipmon.pid";
137 static char line[2048];
139 static char *logfile = NULL;
140 static FILE *binarylog = NULL;
141 static char *binarylogfile = NULL;
142 static int donehup = 0;
143 static void usage __P((char *));
144 static void handlehup __P((int));
145 static void flushlogs __P((char *, FILE *));
146 static void print_log __P((int, FILE *, char *, int));
147 static void print_ipflog __P((FILE *, char *, int));
148 static void print_natlog __P((FILE *, char *, int));
149 static void print_statelog __P((FILE *, char *, int));
150 static int read_log __P((int, int *, char *, int));
151 static void write_pid __P((char *));
152 static char *icmpname __P((u_int, u_int));
153 static char *icmpname6 __P((u_int, u_int));
154 static icmp_type_t *find_icmptype __P((int, icmp_type_t *, size_t));
155 static icmp_subtype_t *find_icmpsubtype __P((int, icmp_subtype_t *, size_t));
157 static struct tm *get_tm __P((u_32_t));
159 static struct tm *get_tm __P((time_t));
162 char *hostname __P((int, int, u_32_t *));
163 char *portname __P((int, char *, u_int));
164 int main __P((int, char *[]));
166 static void logopts __P((int, char *));
167 static void init_tabs __P((void));
168 static char *getproto __P((u_int));
170 static char **protocols = NULL;
171 static char **udp_ports = NULL;
172 static char **tcp_ports = NULL;
173 static char *conf_file = NULL;
176 #define OPT_SYSLOG 0x001
177 #define OPT_RESOLVE 0x002
178 #define OPT_HEXBODY 0x004
179 #define OPT_VERBOSE 0x008
180 #define OPT_HEXHDR 0x010
181 #define OPT_TAIL 0x020
182 #define OPT_NAT 0x080
183 #define OPT_STATE 0x100
184 #define OPT_FILTER 0x200
185 #define OPT_PORTNUM 0x400
186 #define OPT_LOGALL (OPT_NAT|OPT_STATE|OPT_FILTER)
187 #define OPT_LOGBODY 0x800
189 #define HOSTNAME_V4(a,b) hostname((a), 4, (u_32_t *)&(b))
192 #define LOGFAC LOG_LOCAL0
197 static icmp_subtype_t icmpunreachnames[] = {
198 { ICMP_UNREACH_NET, "net" },
199 { ICMP_UNREACH_HOST, "host" },
200 { ICMP_UNREACH_PROTOCOL, "protocol" },
201 { ICMP_UNREACH_PORT, "port" },
202 { ICMP_UNREACH_NEEDFRAG, "needfrag" },
203 { ICMP_UNREACH_SRCFAIL, "srcfail" },
204 { ICMP_UNREACH_NET_UNKNOWN, "net_unknown" },
205 { ICMP_UNREACH_HOST_UNKNOWN, "host_unknown" },
206 { ICMP_UNREACH_NET, "isolated" },
207 { ICMP_UNREACH_NET_PROHIB, "net_prohib" },
208 { ICMP_UNREACH_NET_PROHIB, "host_prohib" },
209 { ICMP_UNREACH_TOSNET, "tosnet" },
210 { ICMP_UNREACH_TOSHOST, "toshost" },
211 { ICMP_UNREACH_ADMIN_PROHIBIT, "admin_prohibit" },
215 static icmp_subtype_t redirectnames[] = {
216 { ICMP_REDIRECT_NET, "net" },
217 { ICMP_REDIRECT_HOST, "host" },
218 { ICMP_REDIRECT_TOSNET, "tosnet" },
219 { ICMP_REDIRECT_TOSHOST, "toshost" },
223 static icmp_subtype_t timxceednames[] = {
224 { ICMP_TIMXCEED_INTRANS, "transit" },
225 { ICMP_TIMXCEED_REASS, "reassem" },
229 static icmp_subtype_t paramnames[] = {
230 { ICMP_PARAMPROB_ERRATPTR, "errata_pointer" },
231 { ICMP_PARAMPROB_OPTABSENT, "optmissing" },
232 { ICMP_PARAMPROB_LENGTH, "length" },
236 static icmp_type_t icmptypes[] = {
237 { ICMP_ECHOREPLY, NULL, 0, "echoreply" },
238 { -1, NULL, 0, NULL },
239 { -1, NULL, 0, NULL },
240 { ICMP_UNREACH, icmpunreachnames,
241 IST_SZ(icmpunreachnames),"unreach" },
242 { ICMP_SOURCEQUENCH, NULL, 0, "sourcequench" },
243 { ICMP_REDIRECT, redirectnames,
244 IST_SZ(redirectnames), "redirect" },
245 { -1, NULL, 0, NULL },
246 { -1, NULL, 0, NULL },
247 { ICMP_ECHO, NULL, 0, "echo" },
248 { ICMP_ROUTERADVERT, NULL, 0, "routeradvert" },
249 { ICMP_ROUTERSOLICIT, NULL, 0, "routersolicit" },
250 { ICMP_TIMXCEED, timxceednames,
251 IST_SZ(timxceednames), "timxceed" },
252 { ICMP_PARAMPROB, paramnames,
253 IST_SZ(paramnames), "paramprob" },
254 { ICMP_TSTAMP, NULL, 0, "timestamp" },
255 { ICMP_TSTAMPREPLY, NULL, 0, "timestampreply" },
256 { ICMP_IREQ, NULL, 0, "inforeq" },
257 { ICMP_IREQREPLY, NULL, 0, "inforeply" },
258 { ICMP_MASKREQ, NULL, 0, "maskreq" },
259 { ICMP_MASKREPLY, NULL, 0, "maskreply" },
260 { -2, NULL, 0, NULL }
263 static icmp_subtype_t icmpredirect6[] = {
264 { ICMP6_DST_UNREACH_NOROUTE, "noroute" },
265 { ICMP6_DST_UNREACH_ADMIN, "admin" },
266 { ICMP6_DST_UNREACH_NOTNEIGHBOR, "neighbour" },
267 { ICMP6_DST_UNREACH_ADDR, "address" },
268 { ICMP6_DST_UNREACH_NOPORT, "noport" },
272 static icmp_subtype_t icmptimexceed6[] = {
273 { ICMP6_TIME_EXCEED_TRANSIT, "intransit" },
274 { ICMP6_TIME_EXCEED_REASSEMBLY, "reassem" },
278 static icmp_subtype_t icmpparamprob6[] = {
279 { ICMP6_PARAMPROB_HEADER, "header" },
280 { ICMP6_PARAMPROB_NEXTHEADER, "nextheader" },
281 { ICMP6_PARAMPROB_OPTION, "option" },
285 static icmp_subtype_t icmpquerysubject6[] = {
286 { ICMP6_NI_SUBJ_IPV6, "ipv6" },
287 { ICMP6_NI_SUBJ_FQDN, "fqdn" },
288 { ICMP6_NI_SUBJ_IPV4, "ipv4" },
292 static icmp_subtype_t icmpnodeinfo6[] = {
293 { ICMP6_NI_SUCCESS, "success" },
294 { ICMP6_NI_REFUSED, "refused" },
295 { ICMP6_NI_UNKNOWN, "unknown" },
299 static icmp_subtype_t icmprenumber6[] = {
300 { ICMP6_ROUTER_RENUMBERING_COMMAND, "command" },
301 { ICMP6_ROUTER_RENUMBERING_RESULT, "result" },
302 { ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET, "seqnum_reset" },
306 static icmp_type_t icmptypes6[] = {
307 { 0, NULL, 0, NULL },
308 { ICMP6_DST_UNREACH, icmpredirect6,
309 IST_SZ(icmpredirect6), "unreach" },
310 { ICMP6_PACKET_TOO_BIG, NULL, 0, "toobig" },
311 { ICMP6_TIME_EXCEEDED, icmptimexceed6,
312 IST_SZ(icmptimexceed6), "timxceed" },
313 { ICMP6_PARAM_PROB, icmpparamprob6,
314 IST_SZ(icmpparamprob6), "paramprob" },
315 { ICMP6_ECHO_REQUEST, NULL, 0, "echo" },
316 { ICMP6_ECHO_REPLY, NULL, 0, "echoreply" },
317 { ICMP6_MEMBERSHIP_QUERY, icmpquerysubject6,
318 IST_SZ(icmpquerysubject6), "groupmemberquery" },
319 { ICMP6_MEMBERSHIP_REPORT,NULL, 0, "groupmemberreport" },
320 { ICMP6_MEMBERSHIP_REDUCTION,NULL, 0, "groupmemberterm" },
321 { ND_ROUTER_SOLICIT, NULL, 0, "routersolicit" },
322 { ND_ROUTER_ADVERT, NULL, 0, "routeradvert" },
323 { ND_NEIGHBOR_SOLICIT, NULL, 0, "neighborsolicit" },
324 { ND_NEIGHBOR_ADVERT, NULL, 0, "neighboradvert" },
325 { ND_REDIRECT, NULL, 0, "redirect" },
326 { ICMP6_ROUTER_RENUMBERING, icmprenumber6,
327 IST_SZ(icmprenumber6), "routerrenumber" },
328 { ICMP6_WRUREQUEST, NULL, 0, "whoareyourequest" },
329 { ICMP6_WRUREPLY, NULL, 0, "whoareyoureply" },
330 { ICMP6_FQDN_QUERY, NULL, 0, "fqdnquery" },
331 { ICMP6_FQDN_REPLY, NULL, 0, "fqdnreply" },
332 { ICMP6_NI_QUERY, icmpnodeinfo6,
333 IST_SZ(icmpnodeinfo6), "nodeinforequest" },
334 { ICMP6_NI_REPLY, NULL, 0, "nodeinforeply" },
335 { MLD6_MTRACE_RESP, NULL, 0, "mtraceresponse" },
336 { MLD6_MTRACE, NULL, 0, "mtracerequest" },
337 { -2, NULL, 0, NULL }
340 static icmp_subtype_t *find_icmpsubtype(type, table, tablesz)
342 icmp_subtype_t *table;
351 if ((type < 0) || (type > table[tablesz - 2].ist_val))
355 if (table[type].ist_val == type)
358 for (i = 0, ist = table; ist->ist_val != -2; i++, ist++)
359 if (ist->ist_val == type)
365 static icmp_type_t *find_icmptype(type, table, tablesz)
376 if ((type < 0) || (type > table[tablesz - 2].it_val))
380 if (table[type].it_val == type)
383 for (i = 0, it = table; it->it_val != -2; i++, it++)
384 if (it->it_val == type)
390 static void handlehup(sig)
393 signal(SIGHUP, handlehup);
398 static void init_tabs()
405 if (protocols != NULL) {
406 for (i = 0; i < 256; i++)
407 if (protocols[i] != NULL) {
414 protocols = (char **)malloc(256 * sizeof(*protocols));
415 if (protocols != NULL) {
416 bzero((char *)protocols, 256 * sizeof(*protocols));
419 while ((p = getprotoent()) != NULL)
420 if (p->p_proto >= 0 && p->p_proto <= 255 &&
421 p->p_name != NULL && protocols[p->p_proto] == NULL)
422 protocols[p->p_proto] = strdup(p->p_name);
428 free(protocols[252]);
430 protocols[252] = NULL;
434 if (udp_ports != NULL) {
435 for (i = 0; i < 65536; i++)
436 if (udp_ports[i] != NULL) {
443 udp_ports = (char **)malloc(65536 * sizeof(*udp_ports));
444 if (udp_ports != NULL)
445 bzero((char *)udp_ports, 65536 * sizeof(*udp_ports));
447 if (tcp_ports != NULL) {
448 for (i = 0; i < 65536; i++)
449 if (tcp_ports[i] != NULL) {
456 tcp_ports = (char **)malloc(65536 * sizeof(*tcp_ports));
457 if (tcp_ports != NULL)
458 bzero((char *)tcp_ports, 65536 * sizeof(*tcp_ports));
461 while ((s = getservent()) != NULL) {
462 if (s->s_proto == NULL)
464 else if (!strcmp(s->s_proto, "tcp")) {
465 port = ntohs(s->s_port);
468 } else if (!strcmp(s->s_proto, "udp")) {
469 port = ntohs(s->s_port);
474 if ((port < 0 || port > 65535) || (name == NULL))
477 tab[port] = strdup(name);
483 static char *getproto(p)
490 s = protocols ? protocols[p] : NULL;
492 sprintf(pnum, "%u", p);
499 static int read_log(fd, lenp, buf, bufsize)
500 int fd, bufsize, *lenp;
505 nr = read(fd, buf, bufsize);
508 if ((nr < 0) && (errno != EINTR))
515 char *hostname(res, v, ip)
519 # define MAX_INETA 16
520 static char hname[MAXHOSTNAMELEN + MAX_INETA + 3];
522 static char hostbuf[MAXHOSTNAMELEN+1];
530 return inet_ntoa(ipa);
531 hp = gethostbyaddr((char *)ip, sizeof(*ip), AF_INET);
533 return inet_ntoa(ipa);
534 sprintf(hname, "%.*s[%s]", MAXHOSTNAMELEN, hp->h_name,
539 (void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
540 hostbuf[MAXHOSTNAMELEN] = '\0';
548 char *portname(res, proto, port)
553 static char pname[8];
558 (void) sprintf(pname, "%u", port);
559 if (!res || (opts & OPT_PORTNUM))
562 if (!strcmp(proto, "tcp"))
564 else if (!strcmp(proto, "udp"))
572 static char *icmpname(type, code)
576 static char name[80];
582 it = find_icmptype(type, icmptypes, sizeof(icmptypes) / sizeof(*it));
587 sprintf(name, "icmptype(%d)/", type);
589 sprintf(name, "%s/", s);
592 if (it != NULL && it->it_subtable != NULL)
593 ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
595 if (ist != NULL && ist->ist_name != NULL)
596 strcat(name, ist->ist_name);
598 sprintf(name + strlen(name), "%d", code);
603 static char *icmpname6(type, code)
607 static char name[80];
613 it = find_icmptype(type, icmptypes6, sizeof(icmptypes6) / sizeof(*it));
618 sprintf(name, "icmpv6type(%d)/", type);
620 sprintf(name, "%s/", s);
623 if (it != NULL && it->it_subtable != NULL)
624 ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
626 if (ist != NULL && ist->ist_name != NULL)
627 strcat(name, ist->ist_name);
629 sprintf(name + strlen(name), "%d", code);
635 void dumphex(log, dopts, buf, len)
643 u_char *s = (u_char *)buf, *t = (u_char *)hline;
645 if (buf == NULL || len == 0)
650 for (i = len, j = 0; i; i--, j++, s++) {
651 if (j && !(j & 0xf)) {
654 if ((dopts & OPT_SYSLOG))
655 syslog(LOG_INFO, "%s", hline);
656 else if (log != NULL)
661 sprintf((char *)t, "%02x", *s & 0xff);
663 if (!((j + 1) & 0xf)) {
665 sprintf((char *)t, " ");
667 for (k = 16; k; k--, s++)
668 *t++ = (ISPRINT(*s) ? *s : '.');
677 for (k = 16 - (j & 0xf); k; k--) {
682 sprintf((char *)t, " ");
685 for (k = j & 0xf; k; k--, s++)
686 *t++ = (ISPRINT(*s) ? *s : '.');
690 if ((dopts & OPT_SYSLOG) != 0)
691 syslog(LOG_INFO, "%s", hline);
692 else if (log != NULL) {
699 static struct tm *get_tm(sec)
714 static void print_natlog(log, buf, blen)
720 iplog_t *ipl = (iplog_t *)buf;
726 nl = (struct natlog *)((char *)ipl + sizeof(*ipl));
727 res = (opts & OPT_RESOLVE) ? 1 : 0;
728 tm = get_tm(ipl->ipl_sec);
730 if (!(opts & OPT_SYSLOG)) {
731 (void) strftime(t, len, "%d/%m/%Y ", tm);
736 (void) strftime(t, len, "%T", tm);
738 (void) sprintf(t, ".%-.6ld @%hd ", ipl->ipl_usec, nl->nl_rule + 1);
741 if (nl->nl_type == NL_NEWMAP)
742 strcpy(t, "NAT:MAP ");
743 else if (nl->nl_type == NL_NEWRDR)
744 strcpy(t, "NAT:RDR ");
745 else if (nl->nl_type == NL_FLUSH)
746 strcpy(t, "NAT:FLUSH ");
747 else if (nl->nl_type == NL_EXPIRE)
748 strcpy(t, "NAT:EXPIRE ");
749 else if (nl->nl_type == NL_NEWBIMAP)
750 strcpy(t, "NAT:BIMAP ");
751 else if (nl->nl_type == NL_NEWBLOCK)
752 strcpy(t, "NAT:MAPBLOCK ");
753 else if (nl->nl_type == NL_CLONE)
754 strcpy(t, "NAT:CLONE ");
756 sprintf(t, "Type: %d ", nl->nl_type);
759 proto = getproto(nl->nl_p);
761 (void) sprintf(t, "%s,%s <- -> ", HOSTNAME_V4(res, nl->nl_inip),
762 portname(res, proto, (u_int)nl->nl_inport));
764 (void) sprintf(t, "%s,%s ", HOSTNAME_V4(res, nl->nl_outip),
765 portname(res, proto, (u_int)nl->nl_outport));
767 (void) sprintf(t, "[%s,%s]", HOSTNAME_V4(res, nl->nl_origip),
768 portname(res, proto, (u_int)nl->nl_origport));
770 if (nl->nl_type == NL_EXPIRE) {
772 (void) sprintf(t, " Pkts %qd/%qd Bytes %qd/%qd",
773 (long long)nl->nl_pkts[0],
774 (long long)nl->nl_pkts[1],
775 (long long)nl->nl_bytes[0],
776 (long long)nl->nl_bytes[1]);
778 (void) sprintf(t, " Pkts %ld/%ld Bytes %ld/%ld",
779 nl->nl_pkts[0], nl->nl_pkts[1],
780 nl->nl_bytes[0], nl->nl_bytes[1]);
787 if (opts & OPT_SYSLOG)
788 syslog(LOG_INFO, "%s", line);
789 else if (log != NULL)
790 (void) fprintf(log, "%s", line);
794 static void print_statelog(log, buf, blen)
800 iplog_t *ipl = (iplog_t *)buf;
801 char *t = line, *proto;
805 sl = (struct ipslog *)((char *)ipl + sizeof(*ipl));
806 res = (opts & OPT_RESOLVE) ? 1 : 0;
807 tm = get_tm(ipl->ipl_sec);
809 if (!(opts & OPT_SYSLOG)) {
810 (void) strftime(t, len, "%d/%m/%Y ", tm);
815 (void) strftime(t, len, "%T", tm);
817 (void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
820 if (sl->isl_type == ISL_NEW)
821 strcpy(t, "STATE:NEW ");
822 else if (sl->isl_type == ISL_CLONE)
823 strcpy(t, "STATE:CLONED ");
824 else if (sl->isl_type == ISL_EXPIRE) {
825 if ((sl->isl_p == IPPROTO_TCP) &&
826 (sl->isl_state[0] > IPF_TCPS_ESTABLISHED ||
827 sl->isl_state[1] > IPF_TCPS_ESTABLISHED))
828 strcpy(t, "STATE:CLOSE ");
830 strcpy(t, "STATE:EXPIRE ");
831 } else if (sl->isl_type == ISL_FLUSH)
832 strcpy(t, "STATE:FLUSH ");
833 else if (sl->isl_type == ISL_INTERMEDIATE)
834 strcpy(t, "STATE:INTERMEDIATE ");
835 else if (sl->isl_type == ISL_REMOVE)
836 strcpy(t, "STATE:REMOVE ");
837 else if (sl->isl_type == ISL_KILLED)
838 strcpy(t, "STATE:KILLED ");
840 sprintf(t, "Type: %d ", sl->isl_type);
843 proto = getproto(sl->isl_p);
845 if (sl->isl_p == IPPROTO_TCP || sl->isl_p == IPPROTO_UDP) {
846 (void) sprintf(t, "%s,%s -> ",
847 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_src),
848 portname(res, proto, (u_int)sl->isl_sport));
850 (void) sprintf(t, "%s,%s PR %s",
851 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
852 portname(res, proto, (u_int)sl->isl_dport), proto);
853 } else if (sl->isl_p == IPPROTO_ICMP) {
854 (void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
855 (u_32_t *)&sl->isl_src));
857 (void) sprintf(t, "%s PR icmp %d",
858 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
860 } else if (sl->isl_p == IPPROTO_ICMPV6) {
861 (void) sprintf(t, "%s -> ", hostname(res, sl->isl_v,
862 (u_32_t *)&sl->isl_src));
864 (void) sprintf(t, "%s PR icmpv6 %d",
865 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
868 (void) sprintf(t, "%s -> ",
869 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_src));
871 (void) sprintf(t, "%s PR %s",
872 hostname(res, sl->isl_v, (u_32_t *)&sl->isl_dst),
876 if (sl->isl_tag != FR_NOLOGTAG) {
877 (void) sprintf(t, " tag %u", sl->isl_tag);
880 if (sl->isl_type != ISL_NEW) {
884 " Forward: Pkts in %" PRId64 " Bytes in %" PRId64
885 " Pkts out %" PRId64 " Bytes out %" PRId64
886 " Backward: Pkts in %" PRId64 " Bytes in %" PRId64
887 " Pkts out %" PRId64 " Bytes out %" PRId64,
889 " 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",
892 " 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",
894 sl->isl_pkts[0], sl->isl_bytes[0],
895 sl->isl_pkts[1], sl->isl_bytes[1],
896 sl->isl_pkts[2], sl->isl_bytes[2],
897 sl->isl_pkts[3], sl->isl_bytes[3]);
904 if (opts & OPT_SYSLOG)
905 syslog(LOG_INFO, "%s", line);
906 else if (log != NULL)
907 (void) fprintf(log, "%s", line);
911 static void print_log(logtype, log, buf, blen)
917 char *bp = NULL, *bpo = NULL;
921 ipl = (iplog_t *)buf;
922 if ((u_long)ipl & (sizeof(long)-1)) {
925 bp = (char *)malloc(blen);
926 bcopy((char *)ipl, bp, blen);
935 psize = ipl->ipl_dsize;
940 fwrite(buf, psize, 1, binarylog);
944 if (logtype == IPL_LOGIPF) {
945 if (ipl->ipl_magic == IPL_MAGIC)
946 print_ipflog(log, buf, psize);
948 } else if (logtype == IPL_LOGNAT) {
949 if (ipl->ipl_magic == IPL_MAGIC_NAT)
950 print_natlog(log, buf, psize);
952 } else if (logtype == IPL_LOGSTATE) {
953 if (ipl->ipl_magic == IPL_MAGIC_STATE)
954 print_statelog(log, buf, psize);
966 static void print_ipflog(log, buf, blen)
976 int i, v, lvl, res, len, off, plen, ipoff, defaction;
986 ipl = (iplog_t *)buf;
987 ipf = (ipflog_t *)((char *)buf + sizeof(*ipl));
988 ip = (ip_t *)((char *)ipf + sizeof(*ipf));
990 res = (opts & OPT_RESOLVE) ? 1 : 0;
993 tm = get_tm(ipl->ipl_sec);
996 if (!(opts & OPT_SYSLOG)) {
997 (void) strftime(t, len, "%d/%m/%Y ", tm);
1002 (void) strftime(t, len, "%T", tm);
1004 (void) sprintf(t, ".%-.6ld ", ipl->ipl_usec);
1006 if (ipl->ipl_count > 1) {
1007 (void) sprintf(t, "%dx ", ipl->ipl_count);
1010 #if (defined(MENTAT) || \
1011 (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
1012 (defined(__FreeBSD__) && (__FreeBSD_version >= 501113)) || \
1013 (defined(OpenBSD) && (OpenBSD >= 199603))) || defined(linux)
1015 char ifname[sizeof(ipf->fl_ifname) + 1];
1017 strncpy(ifname, ipf->fl_ifname, sizeof(ipf->fl_ifname));
1018 ifname[sizeof(ipf->fl_ifname)] = '\0';
1019 (void) sprintf(t, "%s", ifname);
1021 # if defined(MENTAT) || defined(linux)
1022 if (ISALPHA(*(t - 1))) {
1023 sprintf(t, "%d", ipf->fl_unit);
1029 for (len = 0; len < 3; len++)
1030 if (ipf->fl_ifname[len] == '\0')
1032 if (ipf->fl_ifname[len])
1034 (void) sprintf(t, "%*.*s%u", len, len, ipf->fl_ifname, ipf->fl_unit);
1037 if ((ipf->fl_group[0] == (char)~0) && (ipf->fl_group[1] == '\0'))
1039 else if (ipf->fl_group[0] == '\0')
1040 (void) strcpy(t, " @0:");
1042 (void) sprintf(t, " @%s:", ipf->fl_group);
1044 if (ipf->fl_rule == 0xffffffff)
1047 (void) sprintf(t, "%u ", ipf->fl_rule + 1);
1052 if (ipf->fl_lflags & FI_SHORT) {
1057 if (FR_ISPASS(ipf->fl_flags)) {
1058 if (ipf->fl_flags & FR_LOGP)
1062 } else if (FR_ISBLOCK(ipf->fl_flags)) {
1063 if (ipf->fl_flags & FR_LOGB)
1068 } else if ((ipf->fl_flags & FR_LOGMASK) == FR_LOG) {
1071 } else if (ipf->fl_flags & FF_LOGNOMATCH) {
1077 if (ipf->fl_loglevel != 0xffff)
1078 lvl = ipf->fl_loglevel;
1088 p = (u_short)ip6->ip6_nxt;
1089 s = (u_32_t *)&ip6->ip6_src;
1090 d = (u_32_t *)&ip6->ip6_dst;
1091 plen = hl + ntohs(ip6->ip6_plen);
1096 } else if (v == 4) {
1097 hl = IP_HL(ip) << 2;
1099 off = ipoff & IP_OFFMASK;
1100 p = (u_short)ip->ip_p;
1101 s = (u_32_t *)&ip->ip_src;
1102 d = (u_32_t *)&ip->ip_dst;
1107 proto = getproto(p);
1109 if ((p == IPPROTO_TCP || p == IPPROTO_UDP) && !off) {
1110 tp = (tcphdr_t *)((char *)ip + hl);
1111 if (!(ipf->fl_lflags & FI_SHORT)) {
1112 (void) sprintf(t, "%s,%s -> ", hostname(res, v, s),
1113 portname(res, proto, (u_int)tp->th_sport));
1115 (void) sprintf(t, "%s,%s PR %s len %hu %hu",
1116 hostname(res, v, d),
1117 portname(res, proto, (u_int)tp->th_dport),
1121 if (p == IPPROTO_TCP) {
1124 for (i = 0; tcpfl[i].value; i++)
1125 if (tp->th_flags & tcpfl[i].value)
1126 *t++ = tcpfl[i].flag;
1127 if (opts & OPT_VERBOSE) {
1128 (void) sprintf(t, " %lu %lu %hu",
1129 (u_long)(ntohl(tp->th_seq)),
1130 (u_long)(ntohl(tp->th_ack)),
1137 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1139 (void) sprintf(t, "%s PR %s len %hu %hu",
1140 hostname(res, v, d), proto, hl, plen);
1142 } else if ((p == IPPROTO_ICMPV6) && !off && (v == 6)) {
1143 ic = (struct icmp *)((char *)ip + hl);
1144 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1146 (void) sprintf(t, "%s PR icmpv6 len %hu %hu icmpv6 %s",
1147 hostname(res, v, d), hl, plen,
1148 icmpname6(ic->icmp_type, ic->icmp_code));
1149 } else if ((p == IPPROTO_ICMP) && !off && (v == 4)) {
1150 ic = (struct icmp *)((char *)ip + hl);
1151 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1153 (void) sprintf(t, "%s PR icmp len %hu %hu icmp %s",
1154 hostname(res, v, d), hl, plen,
1155 icmpname(ic->icmp_type, ic->icmp_code));
1156 if (ic->icmp_type == ICMP_UNREACH ||
1157 ic->icmp_type == ICMP_SOURCEQUENCH ||
1158 ic->icmp_type == ICMP_PARAMPROB ||
1159 ic->icmp_type == ICMP_REDIRECT ||
1160 ic->icmp_type == ICMP_TIMXCEED) {
1162 i = ntohs(ipc->ip_len);
1164 * XXX - try to guess endian of ip_len in ICMP
1169 ipoff = ntohs(ipc->ip_off);
1170 proto = getproto(ipc->ip_p);
1172 if (!(ipoff & IP_OFFMASK) &&
1173 ((ipc->ip_p == IPPROTO_TCP) ||
1174 (ipc->ip_p == IPPROTO_UDP))) {
1175 tp = (tcphdr_t *)((char *)ipc + hl);
1177 (void) sprintf(t, " for %s,%s -",
1178 HOSTNAME_V4(res, ipc->ip_src),
1179 portname(res, proto,
1180 (u_int)tp->th_sport));
1182 (void) sprintf(t, " %s,%s PR %s len %hu %hu",
1183 HOSTNAME_V4(res, ipc->ip_dst),
1184 portname(res, proto,
1185 (u_int)tp->th_dport),
1186 proto, IP_HL(ipc) << 2, i);
1187 } else if (!(ipoff & IP_OFFMASK) &&
1188 (ipc->ip_p == IPPROTO_ICMP)) {
1189 icmp = (icmphdr_t *)((char *)ipc + hl);
1192 (void) sprintf(t, " for %s -",
1193 HOSTNAME_V4(res, ipc->ip_src));
1196 " %s PR icmp len %hu %hu icmp %d/%d",
1197 HOSTNAME_V4(res, ipc->ip_dst),
1199 icmp->icmp_type, icmp->icmp_code);
1202 (void) sprintf(t, " for %s -",
1203 HOSTNAME_V4(res, ipc->ip_src));
1205 (void) sprintf(t, " %s PR %s len %hu (%hu)",
1206 HOSTNAME_V4(res, ipc->ip_dst), proto,
1207 IP_HL(ipc) << 2, i);
1209 if (ipoff & IP_OFFMASK) {
1211 "(frag %d:%hu@%hu%s%s)",
1213 i - (IP_HL(ipc) << 2),
1214 (ipoff & IP_OFFMASK) << 3,
1215 ipoff & IP_MF ? "+" : "",
1216 ipoff & IP_DF ? "-" : "");
1222 (void) sprintf(t, "%s -> ", hostname(res, v, s));
1224 (void) sprintf(t, "%s PR %s len %hu (%hu)",
1225 hostname(res, v, d), proto, hl, plen);
1227 if (off & IP_OFFMASK)
1228 (void) sprintf(t, " (frag %d:%hu@%hu%s%s)",
1230 plen - hl, (off & IP_OFFMASK) << 3,
1231 ipoff & IP_MF ? "+" : "",
1232 ipoff & IP_DF ? "-" : "");
1237 if (ipf->fl_flags & FR_KEEPSTATE) {
1238 (void) strcpy(t, " K-S");
1242 if (ipf->fl_flags & FR_KEEPFRAG) {
1243 (void) strcpy(t, " K-F");
1247 if (ipf->fl_dir == 0)
1249 else if (ipf->fl_dir == 1)
1252 if (ipf->fl_logtag != 0) {
1253 sprintf(t, " log-tag %d", ipf->fl_logtag);
1256 if (ipf->fl_nattag.ipt_num[0] != 0) {
1257 strcpy(t, " nat-tag ");
1259 strncpy(t, ipf->fl_nattag.ipt_tag, sizeof(ipf->fl_nattag));
1262 if ((ipf->fl_lflags & FI_LOWTTL) != 0) {
1263 strcpy(t, " low-ttl");
1266 if ((ipf->fl_lflags & FI_OOW) != 0) {
1270 if ((ipf->fl_lflags & FI_BAD) != 0) {
1274 if ((ipf->fl_lflags & FI_NATED) != 0) {
1278 if ((ipf->fl_lflags & FI_BADNAT) != 0) {
1279 strcpy(t, " bad-NAT");
1282 if ((ipf->fl_lflags & FI_BADSRC) != 0) {
1283 strcpy(t, " bad-src");
1286 if ((ipf->fl_lflags & FI_MULTICAST) != 0) {
1287 strcpy(t, " multicast");
1290 if ((ipf->fl_lflags & FI_BROADCAST) != 0) {
1291 strcpy(t, " broadcast");
1294 if ((ipf->fl_lflags & (FI_MULTICAST|FI_BROADCAST|FI_MBCAST)) ==
1296 strcpy(t, " mbcast");
1302 if (conf_file != NULL)
1303 defaction = check_action(buf, line, opts, lvl);
1304 if (defaction == 0) {
1305 if (opts & OPT_SYSLOG)
1306 syslog(lvl, "%s", line);
1307 else if (log != NULL)
1308 (void) fprintf(log, "%s", line);
1310 if (opts & OPT_HEXHDR)
1311 dumphex(log, opts, buf,
1312 sizeof(iplog_t) + sizeof(*ipf));
1313 if (opts & OPT_HEXBODY)
1314 dumphex(log, opts, (char *)ip,
1315 ipf->fl_plen + ipf->fl_hlen);
1316 else if ((opts & OPT_LOGBODY) && (ipf->fl_flags & FR_LOGBODY))
1317 dumphex(log, opts, (char *)ip + ipf->fl_hlen,
1323 static void usage(prog)
1326 fprintf(stderr, "%s: [-NFhstvxX] [-f <logfile>]\n", prog);
1331 static void write_pid(file)
1337 if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0644)) >= 0) {
1338 fp = fdopen(fd, "w");
1342 "unable to open/create pid file: %s\n", file);
1345 fprintf(fp, "%d", getpid());
1351 static void flushlogs(file, log)
1355 int fd, flushed = 0;
1357 if ((fd = open(file, O_RDWR)) == -1) {
1358 (void) fprintf(stderr, "%s: open: %s\n",
1359 file, STRERROR(errno));
1363 if (ioctl(fd, SIOCIPFFB, &flushed) == 0) {
1364 printf("%d bytes flushed from log buffer\n",
1368 perror("SIOCIPFFB");
1372 if (opts & OPT_SYSLOG) {
1373 syslog(LOG_INFO, "%d bytes flushed from log\n",
1375 } else if ((log != stdout) && (log != NULL)) {
1376 fprintf(log, "%d bytes flushed from log\n", flushed);
1382 static void logopts(turnon, options)
1389 for (s = options; *s; s++)
1400 flags |= OPT_FILTER;
1403 fprintf(stderr, "Unknown log option %c\n", *s);
1415 int main(argc, argv)
1422 int fd[3], doread, n, i;
1423 int tr, nr, regular[3], c;
1424 int fdt[3], devices = 0, make_daemon = 0;
1425 char buf[DEFAULT_IPFLOGSIZE], *iplfile[3], *s;
1427 extern char *optarg;
1429 fd[0] = fd[1] = fd[2] = -1;
1430 fdt[0] = fdt[1] = fdt[2] = -1;
1431 iplfile[0] = IPL_NAME;
1432 iplfile[1] = IPNAT_NAME;
1433 iplfile[2] = IPSTATE_NAME;
1435 while ((c = getopt(argc, argv,
1436 "?abB:C:Df:FhL:nN:o:O:pP:sS:tvxX")) != -1)
1441 fdt[0] = IPL_LOGIPF;
1442 fdt[1] = IPL_LOGNAT;
1443 fdt[2] = IPL_LOGSTATE;
1446 opts |= OPT_LOGBODY;
1449 binarylogfile = optarg;
1450 binarylog = fopen(optarg, "a");
1458 case 'f' : case 'I' :
1460 fdt[0] = IPL_LOGIPF;
1461 iplfile[0] = optarg;
1464 flushlogs(iplfile[0], log);
1465 flushlogs(iplfile[1], log);
1466 flushlogs(iplfile[2], log);
1469 logfac = fac_findname(optarg);
1472 "Unknown syslog facility '%s'\n",
1478 opts |= OPT_RESOLVE;
1482 fdt[1] = IPL_LOGNAT;
1483 iplfile[1] = optarg;
1485 case 'o' : case 'O' :
1486 logopts(c == 'o', optarg);
1487 fdt[0] = fdt[1] = fdt[2] = -1;
1488 if (opts & OPT_FILTER)
1489 fdt[0] = IPL_LOGIPF;
1491 fdt[1] = IPL_LOGNAT;
1492 if (opts & OPT_STATE)
1493 fdt[2] = IPL_LOGSTATE;
1496 opts |= OPT_PORTNUM;
1502 s = strrchr(argv[0], '/');
1507 openlog(s, LOG_NDELAY|LOG_PID, logfac);
1514 fdt[2] = IPL_LOGSTATE;
1515 iplfile[2] = optarg;
1521 opts |= OPT_VERBOSE;
1524 opts |= OPT_HEXBODY;
1537 if (load_config(conf_file) == -1)
1541 * Default action is to only open the filter log file.
1543 if ((fdt[0] == -1) && (fdt[1] == -1) && (fdt[2] == -1))
1544 fdt[0] = IPL_LOGIPF;
1546 for (i = 0; i < 3; i++) {
1549 if (!strcmp(iplfile[i], "-"))
1552 if ((fd[i] = open(iplfile[i], O_RDONLY)) == -1) {
1553 (void) fprintf(stderr,
1554 "%s: open: %s\n", iplfile[i],
1559 if (fstat(fd[i], &sb) == -1) {
1560 (void) fprintf(stderr, "%d: fstat: %s\n",
1561 fd[i], STRERROR(errno));
1565 if (!(regular[i] = !S_ISCHR(sb.st_mode)))
1570 if (!(opts & OPT_SYSLOG)) {
1571 logfile = argv[optind];
1572 log = logfile ? fopen(logfile, "a") : stdout;
1574 (void) fprintf(stderr, "%s: fopen: %s\n",
1575 argv[optind], STRERROR(errno));
1579 setvbuf(log, NULL, _IONBF, 0);
1583 if (make_daemon && ((log != stdout) || (opts & OPT_SYSLOG))) {
1585 daemon(0, !(opts & OPT_SYSLOG));
1588 if ((pid = fork()) > 0)
1591 (void) fprintf(stderr, "%s: fork() failed: %s\n",
1592 argv[0], STRERROR(errno));
1597 if ((opts & OPT_SYSLOG))
1605 signal(SIGHUP, handlehup);
1607 for (doread = 1; doread; ) {
1610 for (i = 0; i < 3; i++) {
1615 if (ioctl(fd[i], FIONREAD, &tr) == -1) {
1616 if (opts & OPT_SYSLOG)
1618 "ioctl(FIONREAD): %m");
1620 perror("ioctl(FIONREAD)");
1625 tr = (lseek(fd[i], 0, SEEK_CUR) < sb.st_size);
1626 if (!tr && !(opts & OPT_TAIL))
1633 tr = read_log(fd[i], &n, buf, sizeof(buf));
1635 if (logfile && (fp = fopen(logfile, "a"))) {
1639 if (binarylogfile &&
1640 (fp = fopen(binarylogfile, "a"))) {
1645 if (conf_file != NULL)
1646 load_config(conf_file);
1653 if (opts & OPT_SYSLOG)
1654 syslog(LOG_CRIT, "read: %m\n");
1660 if (opts & OPT_SYSLOG)
1661 syslog(LOG_CRIT, "aborting logging\n");
1662 else if (log != NULL)
1663 fprintf(log, "aborting logging\n");
1670 print_log(fdt[i], log, buf, n);
1671 if (!(opts & OPT_SYSLOG))
1677 if (!nr && ((opts & OPT_TAIL) || devices))