4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
10 #include <sys/ioctl.h>
18 static const char sccsid[] = "@(#)ipmon.c 1.21 6/5/96 (C)1993-2000 Darren Reed";
19 static const char rcsid[] = "@(#)$Id$";
23 #define STRERROR(x) strerror(x)
28 extern ipmon_saver_t executesaver;
29 extern ipmon_saver_t filesaver;
30 extern ipmon_saver_t nothingsaver;
31 extern ipmon_saver_t snmpv1saver;
32 extern ipmon_saver_t snmpv2saver;
33 extern ipmon_saver_t syslogsaver;
41 typedef struct logsource {
49 typedef struct config {
52 logsource_t logsrc[3];
61 typedef struct icmp_subtype {
66 typedef struct icmp_type {
68 struct icmp_subtype *it_subtable;
74 #define IST_SZ(x) (sizeof(x)/sizeof(icmp_subtype_t))
77 struct flags tcpfl[] = {
112 static char *pidfile = "/etc/opt/ipf/ipmon.pid";
114 static char *pidfile = "/var/run/ipmon.pid";
117 static char line[2048];
118 static int donehup = 0;
119 static void usage __P((char *));
120 static void handlehup __P((int));
121 static void flushlogs __P((char *, FILE *));
122 static void print_log __P((config_t *, logsource_t *, char *, int));
123 static void print_ipflog __P((config_t *, char *, int));
124 static void print_natlog __P((config_t *, char *, int));
125 static void print_statelog __P((config_t *, char *, int));
126 static int read_log __P((int, int *, char *, int));
127 static void write_pid __P((char *));
128 static char *icmpname __P((u_int, u_int));
129 static char *icmpname6 __P((u_int, u_int));
130 static icmp_type_t *find_icmptype __P((int, icmp_type_t *, size_t));
131 static icmp_subtype_t *find_icmpsubtype __P((int, icmp_subtype_t *, size_t));
132 static struct tm *get_tm __P((time_t));
134 char *portlocalname __P((int, char *, u_int));
135 int main __P((int, char *[]));
137 static void logopts __P((int, char *));
138 static void init_tabs __P((void));
139 static char *getlocalproto __P((u_int));
140 static void openlogs __P((config_t *conf));
141 static int read_loginfo __P((config_t *conf));
142 static void initconfig __P((config_t *conf));
144 static char **protocols = NULL;
145 static char **udp_ports = NULL;
146 static char **tcp_ports = NULL;
149 #define HOSTNAMEV4(b) hostname(AF_INET, (u_32_t *)&(b))
152 #define LOGFAC LOG_LOCAL0
156 int opts = OPT_NORESOLVE;
160 static icmp_subtype_t icmpunreachnames[] = {
161 { ICMP_UNREACH_NET, "net" },
162 { ICMP_UNREACH_HOST, "host" },
163 { ICMP_UNREACH_PROTOCOL, "protocol" },
164 { ICMP_UNREACH_PORT, "port" },
165 { ICMP_UNREACH_NEEDFRAG, "needfrag" },
166 { ICMP_UNREACH_SRCFAIL, "srcfail" },
167 { ICMP_UNREACH_NET_UNKNOWN, "net_unknown" },
168 { ICMP_UNREACH_HOST_UNKNOWN, "host_unknown" },
169 { ICMP_UNREACH_NET, "isolated" },
170 { ICMP_UNREACH_NET_PROHIB, "net_prohib" },
171 { ICMP_UNREACH_NET_PROHIB, "host_prohib" },
172 { ICMP_UNREACH_TOSNET, "tosnet" },
173 { ICMP_UNREACH_TOSHOST, "toshost" },
174 { ICMP_UNREACH_ADMIN_PROHIBIT, "admin_prohibit" },
178 static icmp_subtype_t redirectnames[] = {
179 { ICMP_REDIRECT_NET, "net" },
180 { ICMP_REDIRECT_HOST, "host" },
181 { ICMP_REDIRECT_TOSNET, "tosnet" },
182 { ICMP_REDIRECT_TOSHOST, "toshost" },
186 static icmp_subtype_t timxceednames[] = {
187 { ICMP_TIMXCEED_INTRANS, "transit" },
188 { ICMP_TIMXCEED_REASS, "reassem" },
192 static icmp_subtype_t paramnames[] = {
193 { ICMP_PARAMPROB_ERRATPTR, "errata_pointer" },
194 { ICMP_PARAMPROB_OPTABSENT, "optmissing" },
195 { ICMP_PARAMPROB_LENGTH, "length" },
199 static icmp_type_t icmptypes4[] = {
200 { ICMP_ECHOREPLY, NULL, 0, "echoreply" },
201 { -1, NULL, 0, NULL },
202 { -1, NULL, 0, NULL },
203 { ICMP_UNREACH, icmpunreachnames,
204 IST_SZ(icmpunreachnames),"unreach" },
205 { ICMP_SOURCEQUENCH, NULL, 0, "sourcequench" },
206 { ICMP_REDIRECT, redirectnames,
207 IST_SZ(redirectnames), "redirect" },
208 { -1, NULL, 0, NULL },
209 { -1, NULL, 0, NULL },
210 { ICMP_ECHO, NULL, 0, "echo" },
211 { ICMP_ROUTERADVERT, NULL, 0, "routeradvert" },
212 { ICMP_ROUTERSOLICIT, NULL, 0, "routersolicit" },
213 { ICMP_TIMXCEED, timxceednames,
214 IST_SZ(timxceednames), "timxceed" },
215 { ICMP_PARAMPROB, paramnames,
216 IST_SZ(paramnames), "paramprob" },
217 { ICMP_TSTAMP, NULL, 0, "timestamp" },
218 { ICMP_TSTAMPREPLY, NULL, 0, "timestampreply" },
219 { ICMP_IREQ, NULL, 0, "inforeq" },
220 { ICMP_IREQREPLY, NULL, 0, "inforeply" },
221 { ICMP_MASKREQ, NULL, 0, "maskreq" },
222 { ICMP_MASKREPLY, NULL, 0, "maskreply" },
223 { -2, NULL, 0, NULL }
226 static icmp_subtype_t icmpredirect6[] = {
227 { ICMP6_DST_UNREACH_NOROUTE, "noroute" },
228 { ICMP6_DST_UNREACH_ADMIN, "admin" },
229 { ICMP6_DST_UNREACH_NOTNEIGHBOR, "neighbour" },
230 { ICMP6_DST_UNREACH_ADDR, "address" },
231 { ICMP6_DST_UNREACH_NOPORT, "noport" },
235 static icmp_subtype_t icmptimexceed6[] = {
236 { ICMP6_TIME_EXCEED_TRANSIT, "intransit" },
237 { ICMP6_TIME_EXCEED_REASSEMBLY, "reassem" },
241 static icmp_subtype_t icmpparamprob6[] = {
242 { ICMP6_PARAMPROB_HEADER, "header" },
243 { ICMP6_PARAMPROB_NEXTHEADER, "nextheader" },
244 { ICMP6_PARAMPROB_OPTION, "option" },
248 static icmp_subtype_t icmpquerysubject6[] = {
249 { ICMP6_NI_SUBJ_IPV6, "ipv6" },
250 { ICMP6_NI_SUBJ_FQDN, "fqdn" },
251 { ICMP6_NI_SUBJ_IPV4, "ipv4" },
255 static icmp_subtype_t icmpnodeinfo6[] = {
256 { ICMP6_NI_SUCCESS, "success" },
257 { ICMP6_NI_REFUSED, "refused" },
258 { ICMP6_NI_UNKNOWN, "unknown" },
262 static icmp_subtype_t icmprenumber6[] = {
263 { ICMP6_ROUTER_RENUMBERING_COMMAND, "command" },
264 { ICMP6_ROUTER_RENUMBERING_RESULT, "result" },
265 { ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET, "seqnum_reset" },
269 static icmp_type_t icmptypes6[] = {
270 { 0, NULL, 0, NULL },
271 { ICMP6_DST_UNREACH, icmpredirect6,
272 IST_SZ(icmpredirect6), "unreach" },
273 { ICMP6_PACKET_TOO_BIG, NULL, 0, "toobig" },
274 { ICMP6_TIME_EXCEEDED, icmptimexceed6,
275 IST_SZ(icmptimexceed6), "timxceed" },
276 { ICMP6_PARAM_PROB, icmpparamprob6,
277 IST_SZ(icmpparamprob6), "paramprob" },
278 { ICMP6_ECHO_REQUEST, NULL, 0, "echo" },
279 { ICMP6_ECHO_REPLY, NULL, 0, "echoreply" },
280 { ICMP6_MEMBERSHIP_QUERY, icmpquerysubject6,
281 IST_SZ(icmpquerysubject6), "groupmemberquery" },
282 { ICMP6_MEMBERSHIP_REPORT,NULL, 0, "groupmemberreport" },
283 { ICMP6_MEMBERSHIP_REDUCTION,NULL, 0, "groupmemberterm" },
284 { ND_ROUTER_SOLICIT, NULL, 0, "routersolicit" },
285 { ND_ROUTER_ADVERT, NULL, 0, "routeradvert" },
286 { ND_NEIGHBOR_SOLICIT, NULL, 0, "neighborsolicit" },
287 { ND_NEIGHBOR_ADVERT, NULL, 0, "neighboradvert" },
288 { ND_REDIRECT, NULL, 0, "redirect" },
289 { ICMP6_ROUTER_RENUMBERING, icmprenumber6,
290 IST_SZ(icmprenumber6), "routerrenumber" },
291 { ICMP6_WRUREQUEST, NULL, 0, "whoareyourequest" },
292 { ICMP6_WRUREPLY, NULL, 0, "whoareyoureply" },
293 { ICMP6_FQDN_QUERY, NULL, 0, "fqdnquery" },
294 { ICMP6_FQDN_REPLY, NULL, 0, "fqdnreply" },
295 { ICMP6_NI_QUERY, icmpnodeinfo6,
296 IST_SZ(icmpnodeinfo6), "nodeinforequest" },
297 { ICMP6_NI_REPLY, NULL, 0, "nodeinforeply" },
298 { MLD6_MTRACE_RESP, NULL, 0, "mtraceresponse" },
299 { MLD6_MTRACE, NULL, 0, "mtracerequest" },
300 { -2, NULL, 0, NULL }
303 static icmp_subtype_t *find_icmpsubtype(type, table, tablesz)
305 icmp_subtype_t *table;
314 if ((type < 0) || (type > table[tablesz - 2].ist_val))
318 if (table[type].ist_val == type)
321 for (i = 0, ist = table; ist->ist_val != -2; i++, ist++)
322 if (ist->ist_val == type)
328 static icmp_type_t *find_icmptype(type, table, tablesz)
339 if ((type < 0) || (type > table[tablesz - 2].it_val))
343 if (table[type].it_val == type)
346 for (i = 0, it = table; it->it_val != -2; i++, it++)
347 if (it->it_val == type)
353 static void handlehup(sig)
356 signal(SIGHUP, handlehup);
361 static void init_tabs()
368 if (protocols != NULL) {
369 for (i = 0; i < 256; i++)
370 if (protocols[i] != NULL) {
377 protocols = (char **)malloc(256 * sizeof(*protocols));
378 if (protocols != NULL) {
379 bzero((char *)protocols, 256 * sizeof(*protocols));
382 while ((p = getprotoent()) != NULL)
383 if (p->p_proto >= 0 && p->p_proto <= 255 &&
384 p->p_name != NULL && protocols[p->p_proto] == NULL)
385 protocols[p->p_proto] = strdup(p->p_name);
389 protocols[0] = strdup("ip");
392 if (udp_ports != NULL) {
393 for (i = 0; i < 65536; i++)
394 if (udp_ports[i] != NULL) {
401 udp_ports = (char **)malloc(65536 * sizeof(*udp_ports));
402 if (udp_ports != NULL)
403 bzero((char *)udp_ports, 65536 * sizeof(*udp_ports));
405 if (tcp_ports != NULL) {
406 for (i = 0; i < 65536; i++)
407 if (tcp_ports[i] != NULL) {
414 tcp_ports = (char **)malloc(65536 * sizeof(*tcp_ports));
415 if (tcp_ports != NULL)
416 bzero((char *)tcp_ports, 65536 * sizeof(*tcp_ports));
419 while ((s = getservent()) != NULL) {
420 if (s->s_proto == NULL)
422 else if (!strcmp(s->s_proto, "tcp")) {
423 port = ntohs(s->s_port);
426 } else if (!strcmp(s->s_proto, "udp")) {
427 port = ntohs(s->s_port);
432 if ((port < 0 || port > 65535) || (name == NULL))
435 tab[port] = strdup(name);
441 static char *getlocalproto(p)
448 s = protocols ? protocols[p] : NULL;
450 sprintf(pnum, "%u", p);
457 static int read_log(fd, lenp, buf, bufsize)
458 int fd, bufsize, *lenp;
463 if (bufsize > IPFILTER_LOGSIZE)
464 bufsize = IPFILTER_LOGSIZE;
466 nr = read(fd, buf, bufsize);
469 if ((nr < 0) && (errno != EINTR))
476 char *portlocalname(res, proto, port)
481 static char pname[8];
486 sprintf(pname, "%u", port);
487 if (!res || (ipmonopts & IPMON_PORTNUM))
490 if (!strcmp(proto, "tcp"))
492 else if (!strcmp(proto, "udp"))
500 static char *icmpname(type, code)
504 static char name[80];
510 it = find_icmptype(type, icmptypes4, sizeof(icmptypes4) / sizeof(*it));
515 sprintf(name, "icmptype(%d)/", type);
517 sprintf(name, "%s/", s);
520 if (it != NULL && it->it_subtable != NULL)
521 ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
523 if (ist != NULL && ist->ist_name != NULL)
524 strcat(name, ist->ist_name);
526 sprintf(name + strlen(name), "%d", code);
531 static char *icmpname6(type, code)
535 static char name[80];
541 it = find_icmptype(type, icmptypes6, sizeof(icmptypes6) / sizeof(*it));
546 sprintf(name, "icmpv6type(%d)/", type);
548 sprintf(name, "%s/", s);
551 if (it != NULL && it->it_subtable != NULL)
552 ist = find_icmpsubtype(code, it->it_subtable, it->it_stsize);
554 if (ist != NULL && ist->ist_name != NULL)
555 strcat(name, ist->ist_name);
557 sprintf(name + strlen(name), "%d", code);
563 void dumphex(log, dopts, buf, len)
571 u_char *s = (u_char *)buf, *t = (u_char *)hline;
573 if (buf == NULL || len == 0)
578 for (i = len, j = 0; i; i--, j++, s++) {
579 if (j && !(j & 0xf)) {
582 if ((dopts & IPMON_SYSLOG))
583 syslog(LOG_INFO, "%s", hline);
584 else if (log != NULL)
589 sprintf((char *)t, "%02x", *s & 0xff);
591 if (!((j + 1) & 0xf)) {
593 sprintf((char *)t, " ");
595 for (k = 16; k; k--, s++)
596 *t++ = (isprint(*s) ? *s : '.');
605 for (k = 16 - (j & 0xf); k; k--) {
610 sprintf((char *)t, " ");
613 for (k = j & 0xf; k; k--, s++)
614 *t++ = (isprint(*s) ? *s : '.');
618 if ((dopts & IPMON_SYSLOG) != 0)
619 syslog(LOG_INFO, "%s", hline);
620 else if (log != NULL) {
627 static struct tm *get_tm(sec)
638 static void print_natlog(conf, buf, blen)
643 static u_32_t seqnum = 0;
644 int res, i, len, family;
654 ipl = (iplog_t *)buf;
655 if (ipl->ipl_seqnum != seqnum) {
656 if ((ipmonopts & IPMON_SYSLOG) != 0) {
658 "missed %u NAT log entries: %u %u",
659 ipl->ipl_seqnum - seqnum, seqnum,
662 (void) fprintf(conf->log,
663 "missed %u NAT log entries: %u %u\n",
664 ipl->ipl_seqnum - seqnum, seqnum,
668 seqnum = ipl->ipl_seqnum + ipl->ipl_count;
670 nl = (struct natlog *)((char *)ipl + sizeof(*ipl));
671 res = (ipmonopts & IPMON_RESOLVE) ? 1 : 0;
672 tm = get_tm(ipl->ipl_sec);
675 if (!(ipmonopts & IPMON_SYSLOG)) {
676 (void) strftime(t, len, "%d/%m/%Y ", tm);
681 (void) strftime(t, len, "%T", tm);
683 sprintf(t, ".%-.6ld @%hd ", (long)ipl->ipl_usec, nl->nl_rule + 1);
686 switch (nl->nl_action)
689 strcpy(t, "NAT:NEW");
693 strcpy(t, "NAT:FLUSH");
697 strcpy(t, "NAT:CLONE");
701 strcpy(t, "NAT:EXPIRE");
705 strcpy(t, "NAT:DESTROY");
709 strcpy(t, "NAT:PURGE");
713 sprintf(t, "NAT:Action(%d)", nl->nl_action);
732 strcpy(t, "-BIMAP ");
737 strcpy(t, "-MAPBLOCK ");
741 case NAT_REWRITE|NAT_MAP :
742 strcpy(t, "-RWR_MAP ");
745 case NAT_REWRITE|NAT_REDIRECT :
746 strcpy(t, "-RWR_RDR ");
749 case NAT_ENCAP|NAT_MAP :
750 strcpy(t, "-ENC_MAP ");
753 case NAT_ENCAP|NAT_REDIRECT :
754 strcpy(t, "-ENC_RDR ");
757 case NAT_DIVERTUDP|NAT_MAP :
758 strcpy(t, "-DIV_MAP ");
761 case NAT_DIVERTUDP|NAT_REDIRECT :
762 strcpy(t, "-DIV_RDR ");
766 sprintf(t, "-Type(%d) ", nl->nl_type);
771 proto = getlocalproto(nl->nl_p[0]);
773 family = vtof(nl->nl_v[0]);
776 sprintf(t, "%s,%s <- -> ", hostname(family, nl->nl_osrcip.i6),
777 portlocalname(res, proto, (u_int)nl->nl_osrcport));
779 sprintf(t, "%s,%s ", hostname(family, nl->nl_nsrcip.i6),
780 portlocalname(res, proto, (u_int)nl->nl_nsrcport));
782 sprintf(t, "[%s,%s] ", hostname(family, nl->nl_odstip.i6),
783 portlocalname(res, proto, (u_int)nl->nl_odstport));
785 sprintf(t, "%s,%s ", hostname(family, nl->nl_osrcip.i6),
786 portlocalname(res, proto, (u_int)nl->nl_osrcport));
788 sprintf(t, "%s,%s <- -> ", hostname(family, nl->nl_odstip.i6),
789 portlocalname(res, proto, (u_int)nl->nl_odstport));
791 sprintf(t, "%s,%s ", hostname(family, nl->nl_nsrcip.i6),
792 portlocalname(res, proto, (u_int)nl->nl_nsrcport));
794 sprintf(t, "%s,%s ", hostname(family, nl->nl_ndstip.i6),
795 portlocalname(res, proto, (u_int)nl->nl_ndstport));
799 strcpy(t, getlocalproto(nl->nl_p[0]));
802 if (nl->nl_action == NL_EXPIRE || nl->nl_action == NL_FLUSH) {
805 sprintf(t, " Pkts %" PRId64 "/%" PRId64 " Bytes %" PRId64 "/%"
808 sprintf(t, " Pkts %qd/%qd Bytes %qd/%qd",
811 sprintf(t, " Pkts %ld/%ld Bytes %ld/%ld",
813 nl->nl_pkts[0], nl->nl_pkts[1],
814 nl->nl_bytes[0], nl->nl_bytes[1]);
820 if (ipmonopts & IPMON_SYSLOG)
821 syslog(LOG_INFO, "%s", line);
822 else if (conf->log != NULL)
823 (void) fprintf(conf->log, "%s", line);
827 static void print_statelog(conf, buf, blen)
832 static u_32_t seqnum = 0;
833 int res, i, len, family;
840 ipl = (iplog_t *)buf;
841 if (ipl->ipl_seqnum != seqnum) {
842 if ((ipmonopts & IPMON_SYSLOG) != 0) {
844 "missed %u state log entries: %u %u",
845 ipl->ipl_seqnum - seqnum, seqnum,
848 (void) fprintf(conf->log,
849 "missed %u state log entries: %u %u\n",
850 ipl->ipl_seqnum - seqnum, seqnum,
854 seqnum = ipl->ipl_seqnum + ipl->ipl_count;
856 sl = (struct ipslog *)((char *)ipl + sizeof(*ipl));
857 res = (ipmonopts & IPMON_RESOLVE) ? 1 : 0;
858 tm = get_tm(ipl->ipl_sec);
860 if (!(ipmonopts & IPMON_SYSLOG)) {
861 (void) strftime(t, len, "%d/%m/%Y ", tm);
866 (void) strftime(t, len, "%T", tm);
868 sprintf(t, ".%-.6ld ", (long)ipl->ipl_usec);
871 family = vtof(sl->isl_v);
873 switch (sl->isl_type)
876 strcpy(t, "STATE:NEW ");
880 strcpy(t, "STATE:CLONED ");
884 if ((sl->isl_p == IPPROTO_TCP) &&
885 (sl->isl_state[0] > IPF_TCPS_ESTABLISHED ||
886 sl->isl_state[1] > IPF_TCPS_ESTABLISHED))
887 strcpy(t, "STATE:CLOSE ");
889 strcpy(t, "STATE:EXPIRE ");
893 strcpy(t, "STATE:FLUSH ");
896 case ISL_INTERMEDIATE :
897 strcpy(t, "STATE:INTERMEDIATE ");
901 strcpy(t, "STATE:REMOVE ");
905 strcpy(t, "STATE:KILLED ");
909 strcpy(t, "STATE:UNLOAD ");
913 sprintf(t, "Type: %d ", sl->isl_type);
918 proto = getlocalproto(sl->isl_p);
920 if (sl->isl_p == IPPROTO_TCP || sl->isl_p == IPPROTO_UDP) {
921 sprintf(t, "%s,%s -> ",
922 hostname(family, (u_32_t *)&sl->isl_src),
923 portlocalname(res, proto, (u_int)sl->isl_sport));
925 sprintf(t, "%s,%s PR %s",
926 hostname(family, (u_32_t *)&sl->isl_dst),
927 portlocalname(res, proto, (u_int)sl->isl_dport), proto);
928 } else if (sl->isl_p == IPPROTO_ICMP) {
929 sprintf(t, "%s -> ", hostname(family, (u_32_t *)&sl->isl_src));
931 sprintf(t, "%s PR icmp %d",
932 hostname(family, (u_32_t *)&sl->isl_dst),
934 } else if (sl->isl_p == IPPROTO_ICMPV6) {
935 sprintf(t, "%s -> ", hostname(family, (u_32_t *)&sl->isl_src));
937 sprintf(t, "%s PR icmpv6 %d",
938 hostname(family, (u_32_t *)&sl->isl_dst),
941 sprintf(t, "%s -> ", hostname(family, (u_32_t *)&sl->isl_src));
943 sprintf(t, "%s PR %s",
944 hostname(family, (u_32_t *)&sl->isl_dst), proto);
947 if (sl->isl_tag != FR_NOLOGTAG) {
948 sprintf(t, " tag %u", sl->isl_tag);
951 if (sl->isl_type != ISL_NEW) {
955 " Forward: Pkts in %" PRId64 " Bytes in %" PRId64
956 " Pkts out %" PRId64 " Bytes out %" PRId64
957 " Backward: Pkts in %" PRId64 " Bytes in %" PRId64
958 " Pkts out %" PRId64 " Bytes out %" PRId64,
960 " 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",
963 " 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",
965 sl->isl_pkts[0], sl->isl_bytes[0],
966 sl->isl_pkts[1], sl->isl_bytes[1],
967 sl->isl_pkts[2], sl->isl_bytes[2],
968 sl->isl_pkts[3], sl->isl_bytes[3]);
975 if (ipmonopts & IPMON_SYSLOG)
976 syslog(LOG_INFO, "%s", line);
977 else if (conf->log != NULL)
978 (void) fprintf(conf->log, "%s", line);
982 static void print_log(conf, log, buf, blen)
996 ipl = (iplog_t *)buf;
997 if ((u_long)ipl & (sizeof(long)-1)) {
1000 bp = (char *)malloc(blen);
1001 bcopy((char *)ipl, bp, blen);
1010 psize = ipl->ipl_dsize;
1014 if (conf->blog != NULL) {
1015 fwrite(buf, psize, 1, conf->blog);
1019 if (log->logtype == IPL_LOGIPF) {
1020 if (ipl->ipl_magic == IPL_MAGIC)
1021 print_ipflog(conf, buf, psize);
1023 } else if (log->logtype == IPL_LOGNAT) {
1024 if (ipl->ipl_magic == IPL_MAGIC_NAT)
1025 print_natlog(conf, buf, psize);
1027 } else if (log->logtype == IPL_LOGSTATE) {
1028 if (ipl->ipl_magic == IPL_MAGIC_STATE)
1029 print_statelog(conf, buf, psize);
1041 static void print_ipflog(conf, buf, blen)
1046 static u_32_t seqnum = 0;
1047 int i, f, lvl, res, len, off, plen, ipoff, defaction;
1059 struct ip6_ext *ehp;
1065 ipl = (iplog_t *)buf;
1066 if (ipl->ipl_seqnum != seqnum) {
1067 if ((ipmonopts & IPMON_SYSLOG) != 0) {
1069 "missed %u ipf log entries: %u %u",
1070 ipl->ipl_seqnum - seqnum, seqnum,
1073 (void) fprintf(conf->log,
1074 "missed %u ipf log entries: %u %u\n",
1075 ipl->ipl_seqnum - seqnum, seqnum,
1079 seqnum = ipl->ipl_seqnum + ipl->ipl_count;
1081 ipf = (ipflog_t *)((char *)buf + sizeof(*ipl));
1082 ip = (ip_t *)((char *)ipf + sizeof(*ipf));
1084 res = (ipmonopts & IPMON_RESOLVE) ? 1 : 0;
1087 tm = get_tm(ipl->ipl_sec);
1090 if (!(ipmonopts & IPMON_SYSLOG)) {
1091 (void) strftime(t, len, "%d/%m/%Y ", tm);
1096 (void) strftime(t, len, "%T", tm);
1098 sprintf(t, ".%-.6ld ", (long)ipl->ipl_usec);
1100 if (ipl->ipl_count > 1) {
1101 sprintf(t, "%dx ", ipl->ipl_count);
1105 char ifname[sizeof(ipf->fl_ifname) + 1];
1107 strncpy(ifname, ipf->fl_ifname, sizeof(ipf->fl_ifname));
1108 ifname[sizeof(ipf->fl_ifname)] = '\0';
1109 sprintf(t, "%s", ifname);
1111 # if defined(MENTAT)
1112 if (ISALPHA(*(t - 1))) {
1113 sprintf(t, "%d", ipf->fl_unit);
1118 if ((ipf->fl_group[0] == (char)~0) && (ipf->fl_group[1] == '\0'))
1120 else if (ipf->fl_group[0] == '\0')
1121 (void) strcpy(t, " @0:");
1123 sprintf(t, " @%s:", ipf->fl_group);
1125 if (ipf->fl_rule == 0xffffffff)
1128 sprintf(t, "%u ", ipf->fl_rule + 1);
1133 if (ipf->fl_lflags & FI_SHORT) {
1138 if (FR_ISPASS(ipf->fl_flags)) {
1139 if (ipf->fl_flags & FR_LOGP)
1143 } else if (FR_ISBLOCK(ipf->fl_flags)) {
1144 if (ipf->fl_flags & FR_LOGB)
1149 } else if ((ipf->fl_flags & FR_LOGMASK) == FR_LOG) {
1152 } else if (ipf->fl_flags & FF_LOGNOMATCH) {
1158 if (ipf->fl_loglevel != 0xffff)
1159 lvl = ipf->fl_loglevel;
1164 hl = IP_HL(ip) << 2;
1165 ipoff = ntohs(ip->ip_off);
1166 off = ipoff & IP_OFFMASK;
1167 p = (u_short)ip->ip_p;
1168 s = (u_32_t *)&ip->ip_src;
1169 d = (u_32_t *)&ip->ip_dst;
1170 plen = ntohs(ip->ip_len);
1173 if (f == AF_INET6) {
1178 p = (u_short)ip6->ip6_nxt;
1179 s = (u_32_t *)&ip6->ip6_src;
1180 d = (u_32_t *)&ip6->ip6_dst;
1181 plen = hl + ntohs(ip6->ip6_plen);
1183 ehp = (struct ip6_ext *)((char *)ip6 + hl);
1187 case IPPROTO_HOPOPTS :
1188 case IPPROTO_MOBILITY :
1189 case IPPROTO_DSTOPTS :
1190 case IPPROTO_ROUTING :
1193 ehl = 8 + (ehp->ip6e_len << 3);
1195 ehp = (struct ip6_ext *)((char *)ehp + ehl);
1197 case IPPROTO_FRAGMENT :
1198 hl += sizeof(struct ip6_frag);
1210 proto = getlocalproto(p);
1212 if ((p == IPPROTO_TCP || p == IPPROTO_UDP) && !off) {
1213 tp = (tcphdr_t *)((char *)ip + hl);
1214 if (!(ipf->fl_lflags & FI_SHORT)) {
1215 sprintf(t, "%s,%s -> ", hostname(f, s),
1216 portlocalname(res, proto, (u_int)tp->th_sport));
1218 sprintf(t, "%s,%s PR %s len %hu %hu",
1220 portlocalname(res, proto, (u_int)tp->th_dport),
1224 if (p == IPPROTO_TCP) {
1227 for (i = 0; tcpfl[i].value; i++)
1228 if (tp->th_flags & tcpfl[i].value)
1229 *t++ = tcpfl[i].flag;
1230 if (ipmonopts & IPMON_VERBOSE) {
1231 sprintf(t, " %lu %lu %hu",
1232 (u_long)(ntohl(tp->th_seq)),
1233 (u_long)(ntohl(tp->th_ack)),
1240 sprintf(t, "%s -> ", hostname(f, s));
1242 sprintf(t, "%s PR %s len %hu %hu",
1243 hostname(f, d), proto, hl, plen);
1245 #if defined(AF_INET6) && defined(IPPROTO_ICMPV6)
1246 } else if ((p == IPPROTO_ICMPV6) && !off && (f == AF_INET6)) {
1247 ic = (struct icmp *)((char *)ip + hl);
1248 sprintf(t, "%s -> ", hostname(f, s));
1250 sprintf(t, "%s PR icmpv6 len %hu %hu icmpv6 %s",
1251 hostname(f, d), hl, plen,
1252 icmpname6(ic->icmp_type, ic->icmp_code));
1254 } else if ((p == IPPROTO_ICMP) && !off && (f == AF_INET)) {
1255 ic = (struct icmp *)((char *)ip + hl);
1256 sprintf(t, "%s -> ", hostname(f, s));
1258 sprintf(t, "%s PR icmp len %hu %hu icmp %s",
1259 hostname(f, d), hl, plen,
1260 icmpname(ic->icmp_type, ic->icmp_code));
1261 if (ic->icmp_type == ICMP_UNREACH ||
1262 ic->icmp_type == ICMP_SOURCEQUENCH ||
1263 ic->icmp_type == ICMP_PARAMPROB ||
1264 ic->icmp_type == ICMP_REDIRECT ||
1265 ic->icmp_type == ICMP_TIMXCEED) {
1267 i = ntohs(ipc->ip_len);
1269 * XXX - try to guess endian of ip_len in ICMP
1274 ipoff = ntohs(ipc->ip_off);
1275 proto = getlocalproto(ipc->ip_p);
1277 if (!(ipoff & IP_OFFMASK) &&
1278 ((ipc->ip_p == IPPROTO_TCP) ||
1279 (ipc->ip_p == IPPROTO_UDP))) {
1280 tp = (tcphdr_t *)((char *)ipc + hl);
1282 sprintf(t, " for %s,%s -",
1283 HOSTNAMEV4(ipc->ip_src),
1284 portlocalname(res, proto,
1285 (u_int)tp->th_sport));
1287 sprintf(t, " %s,%s PR %s len %hu %hu",
1288 HOSTNAMEV4(ipc->ip_dst),
1289 portlocalname(res, proto,
1290 (u_int)tp->th_dport),
1291 proto, IP_HL(ipc) << 2, i);
1292 } else if (!(ipoff & IP_OFFMASK) &&
1293 (ipc->ip_p == IPPROTO_ICMP)) {
1294 icmp = (icmphdr_t *)((char *)ipc + hl);
1297 sprintf(t, " for %s -",
1298 HOSTNAMEV4(ipc->ip_src));
1301 " %s PR icmp len %hu %hu icmp %d/%d",
1302 HOSTNAMEV4(ipc->ip_dst),
1304 icmp->icmp_type, icmp->icmp_code);
1307 sprintf(t, " for %s -",
1308 HOSTNAMEV4(ipc->ip_src));
1310 sprintf(t, " %s PR %s len %hu (%hu)",
1311 HOSTNAMEV4(ipc->ip_dst), proto,
1312 IP_HL(ipc) << 2, i);
1314 if (ipoff & IP_OFFMASK) {
1315 sprintf(t, "(frag %d:%hu@%hu%s%s)",
1317 i - (IP_HL(ipc) << 2),
1318 (ipoff & IP_OFFMASK) << 3,
1319 ipoff & IP_MF ? "+" : "",
1320 ipoff & IP_DF ? "-" : "");
1326 sprintf(t, "%s -> ", hostname(f, s));
1328 sprintf(t, "%s PR %s len %hu (%hu)",
1329 hostname(f, d), proto, hl, plen);
1331 if (off & IP_OFFMASK)
1332 sprintf(t, " (frag %d:%hu@%hu%s%s)",
1334 plen - hl, (off & IP_OFFMASK) << 3,
1335 ipoff & IP_MF ? "+" : "",
1336 ipoff & IP_DF ? "-" : "");
1341 if (ipf->fl_flags & FR_KEEPSTATE) {
1342 (void) strcpy(t, " K-S");
1346 if (ipf->fl_flags & FR_KEEPFRAG) {
1347 (void) strcpy(t, " K-F");
1351 if (ipf->fl_dir == 0)
1353 else if (ipf->fl_dir == 1)
1356 if (ipf->fl_logtag != 0) {
1357 sprintf(t, " log-tag %d", ipf->fl_logtag);
1360 if (ipf->fl_nattag.ipt_num[0] != 0) {
1361 strcpy(t, " nat-tag ");
1363 strncpy(t, ipf->fl_nattag.ipt_tag, sizeof(ipf->fl_nattag));
1366 if ((ipf->fl_lflags & FI_LOWTTL) != 0) {
1367 strcpy(t, " low-ttl");
1370 if ((ipf->fl_lflags & FI_OOW) != 0) {
1374 if ((ipf->fl_lflags & FI_BAD) != 0) {
1378 if ((ipf->fl_lflags & FI_NATED) != 0) {
1382 if ((ipf->fl_lflags & FI_BADNAT) != 0) {
1383 strcpy(t, " bad-NAT");
1386 if ((ipf->fl_lflags & FI_BADSRC) != 0) {
1387 strcpy(t, " bad-src");
1390 if ((ipf->fl_lflags & FI_MULTICAST) != 0) {
1391 strcpy(t, " multicast");
1394 if ((ipf->fl_lflags & FI_BROADCAST) != 0) {
1395 strcpy(t, " broadcast");
1398 if ((ipf->fl_lflags & (FI_MULTICAST|FI_BROADCAST|FI_MBCAST)) ==
1400 strcpy(t, " mbcast");
1403 if (ipf->fl_breason != 0) {
1404 strcpy(t, " reason:");
1406 strcpy(t, reasons[ipf->fl_breason]);
1407 t += strlen(reasons[ipf->fl_breason]);
1412 if (conf->cfile != NULL)
1413 defaction = check_action(buf, line, ipmonopts, lvl);
1415 if (defaction == 0) {
1416 if (ipmonopts & IPMON_SYSLOG) {
1417 syslog(lvl, "%s", line);
1418 } else if (conf->log != NULL) {
1419 (void) fprintf(conf->log, "%s", line);
1422 if (ipmonopts & IPMON_HEXHDR) {
1423 dumphex(conf->log, ipmonopts, buf,
1424 sizeof(iplog_t) + sizeof(*ipf));
1426 if (ipmonopts & IPMON_HEXBODY) {
1427 dumphex(conf->log, ipmonopts, (char *)ip,
1428 ipf->fl_plen + ipf->fl_hlen);
1429 } else if ((ipmonopts & IPMON_LOGBODY) &&
1430 (ipf->fl_flags & FR_LOGBODY)) {
1431 dumphex(conf->log, ipmonopts, (char *)ip + ipf->fl_hlen,
1438 static void usage(prog)
1441 fprintf(stderr, "Usage: %s [ -abDFhnpstvxX ] [ -B <binary-logfile> ] [ -C <config-file> ]\n"
1442 "\t[ -f <device> ] [ -L <facility> ] [ -N <device> ]\n"
1443 "\t[ -o [NSI] ] [ -O [NSI] ] [ -P <pidfile> ] [ -S <device> ]\n"
1444 "\t[ <filename> ]\n", prog);
1449 static void write_pid(file)
1455 if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0644)) >= 0) {
1456 fp = fdopen(fd, "w");
1460 "unable to open/create pid file: %s\n", file);
1463 fprintf(fp, "%d", getpid());
1469 static void flushlogs(file, log)
1473 int fd, flushed = 0;
1475 if ((fd = open(file, O_RDWR)) == -1) {
1476 (void) fprintf(stderr, "%s: open: %s\n",
1477 file, STRERROR(errno));
1481 if (ioctl(fd, SIOCIPFFB, &flushed) == 0) {
1482 printf("%d bytes flushed from log buffer\n",
1486 ipferror(fd, "SIOCIPFFB");
1490 if (ipmonopts & IPMON_SYSLOG) {
1491 syslog(LOG_INFO, "%d bytes flushed from log\n",
1493 } else if ((log != stdout) && (log != NULL)) {
1494 fprintf(log, "%d bytes flushed from log\n", flushed);
1500 static void logopts(turnon, options)
1507 for (s = options; *s; s++)
1515 flags |= IPMON_STATE;
1518 flags |= IPMON_FILTER;
1521 fprintf(stderr, "Unknown log option %c\n", *s);
1529 ipmonopts &= ~(flags);
1532 static void initconfig(config_t *conf)
1536 memset(conf, 0, sizeof(*conf));
1541 for (i = 0; i < 3; i++) {
1542 conf->logsrc[i].fd = -1;
1543 conf->logsrc[i].logtype = -1;
1544 conf->logsrc[i].regular = -1;
1547 conf->logsrc[0].file = IPL_NAME;
1548 conf->logsrc[1].file = IPNAT_NAME;
1549 conf->logsrc[2].file = IPSTATE_NAME;
1551 add_doing(&executesaver);
1552 add_doing(&snmpv1saver);
1553 add_doing(&snmpv2saver);
1554 add_doing(&syslogsaver);
1555 add_doing(&filesaver);
1556 add_doing(¬hingsaver);
1560 int main(argc, argv)
1564 int doread, c, make_daemon = 0;
1568 prog = strrchr(argv[0], '/');
1574 initconfig(&config);
1576 while ((c = getopt(argc, argv,
1577 "?abB:C:Df:FhL:nN:o:O:pP:sS:tvxX")) != -1)
1581 ipmonopts |= IPMON_LOGALL;
1582 config.logsrc[0].logtype = IPL_LOGIPF;
1583 config.logsrc[1].logtype = IPL_LOGNAT;
1584 config.logsrc[2].logtype = IPL_LOGSTATE;
1587 ipmonopts |= IPMON_LOGBODY;
1590 config.bfile = optarg;
1591 config.blog = fopen(optarg, "a");
1594 config.cfile = optarg;
1599 case 'f' : case 'I' :
1600 ipmonopts |= IPMON_FILTER;
1601 config.logsrc[0].logtype = IPL_LOGIPF;
1602 config.logsrc[0].file = optarg;
1605 flushlogs(config.logsrc[0].file, config.log);
1606 flushlogs(config.logsrc[1].file, config.log);
1607 flushlogs(config.logsrc[2].file, config.log);
1610 logfac = fac_findname(optarg);
1613 "Unknown syslog facility '%s'\n",
1619 ipmonopts |= IPMON_RESOLVE;
1620 opts &= ~OPT_NORESOLVE;
1623 ipmonopts |= IPMON_NAT;
1624 config.logsrc[1].logtype = IPL_LOGNAT;
1625 config.logsrc[1].file = optarg;
1627 case 'o' : case 'O' :
1628 logopts(c == 'o', optarg);
1629 if (ipmonopts & IPMON_FILTER)
1630 config.logsrc[0].logtype = IPL_LOGIPF;
1631 if (ipmonopts & IPMON_NAT)
1632 config.logsrc[1].logtype = IPL_LOGNAT;
1633 if (ipmonopts & IPMON_STATE)
1634 config.logsrc[2].logtype = IPL_LOGSTATE;
1637 ipmonopts |= IPMON_PORTNUM;
1643 ipmonopts |= IPMON_SYSLOG;
1647 ipmonopts |= IPMON_STATE;
1648 config.logsrc[2].logtype = IPL_LOGSTATE;
1649 config.logsrc[2].file = optarg;
1652 ipmonopts |= IPMON_TAIL;
1655 ipmonopts |= IPMON_VERBOSE;
1658 ipmonopts |= IPMON_HEXBODY;
1661 ipmonopts |= IPMON_HEXHDR;
1669 if (ipmonopts & IPMON_SYSLOG)
1670 openlog(prog, LOG_NDELAY|LOG_PID, logfac);
1674 if (load_config(config.cfile) == -1) {
1680 * Default action is to only open the filter log file.
1682 if ((config.logsrc[0].logtype == -1) &&
1683 (config.logsrc[0].logtype == -1) &&
1684 (config.logsrc[0].logtype == -1))
1685 config.logsrc[0].logtype = IPL_LOGIPF;
1689 if (!(ipmonopts & IPMON_SYSLOG)) {
1690 config.file = argv[optind];
1691 config.log = config.file ? fopen(config.file, "a") : stdout;
1692 if (config.log == NULL) {
1693 (void) fprintf(stderr, "%s: fopen: %s\n",
1694 argv[optind], STRERROR(errno));
1698 setvbuf(config.log, NULL, _IONBF, 0);
1704 ((config.log != stdout) || (ipmonopts & IPMON_SYSLOG))) {
1706 daemon(0, !(ipmonopts & IPMON_SYSLOG));
1713 (void) fprintf(stderr, "%s: fork() failed: %s\n",
1714 argv[0], STRERROR(errno));
1724 if ((ipmonopts & IPMON_SYSLOG))
1732 signal(SIGHUP, handlehup);
1734 for (doread = 1; doread; )
1735 doread = read_loginfo(&config);
1744 static void openlogs(config_t *conf)
1750 for (i = 0; i < 3; i++) {
1751 l = &conf->logsrc[i];
1752 if (l->logtype == -1)
1754 if (!strcmp(l->file, "-"))
1757 if ((l->fd= open(l->file, O_RDONLY)) == -1) {
1758 (void) fprintf(stderr,
1759 "%s: open: %s\n", l->file,
1765 if (fstat(l->fd, &sb) == -1) {
1766 (void) fprintf(stderr, "%d: fstat: %s\n",
1767 l->fd, STRERROR(errno));
1772 l->regular = !S_ISCHR(sb.st_mode);
1774 l->size = sb.st_size;
1776 FD_SET(l->fd, &conf->fdmr);
1777 if (l->fd > conf->maxfd)
1778 conf->maxfd = l->fd;
1784 static int read_loginfo(config_t *conf)
1786 iplog_t buf[DEFAULT_IPFLOGSIZE/sizeof(iplog_t)+1];
1793 n = select(conf->maxfd + 1, &fdr, NULL, NULL, NULL);
1802 for (i = 0, nr = 0; i < 3; i++) {
1803 l = &conf->logsrc[i];
1805 if ((l->logtype == -1) || !FD_ISSET(l->fd, &fdr))
1810 tr = (lseek(l->fd, 0, SEEK_CUR) < l->size);
1811 if (!tr && !(ipmonopts & IPMON_TAIL))
1816 tr = read_log(l->fd, &n, (char *)buf, sizeof(buf));
1818 if (conf->file != NULL) {
1819 if (conf->log != NULL) {
1823 conf->log = fopen(conf->file, "a");
1826 if (conf->bfile != NULL) {
1827 if (conf->blog != NULL) {
1831 conf->blog = fopen(conf->bfile, "a");
1835 if (conf->cfile != NULL)
1836 load_config(conf->cfile);
1843 if (ipmonopts & IPMON_SYSLOG)
1844 syslog(LOG_CRIT, "read: %m\n");
1846 ipferror(l->fd, "read");
1850 if (ipmonopts & IPMON_SYSLOG)
1851 syslog(LOG_CRIT, "aborting logging\n");
1852 else if (conf->log != NULL)
1853 fprintf(conf->log, "aborting logging\n");
1860 print_log(conf, l, (char *)buf, n);
1861 if (!(ipmonopts & IPMON_SYSLOG))
1868 if (!nr && (ipmonopts & IPMON_TAIL))