xref: /freebsd/contrib/tcpdump/print-pflog.c (revision 1de50e9f417616cf647a842762944a2301cb1415)
15b0fe478SBruce M Simpson /*
25b0fe478SBruce M Simpson  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
35b0fe478SBruce M Simpson  *	The Regents of the University of California.  All rights reserved.
45b0fe478SBruce M Simpson  *
55b0fe478SBruce M Simpson  * Redistribution and use in source and binary forms, with or without
65b0fe478SBruce M Simpson  * modification, are permitted provided that: (1) source code distributions
75b0fe478SBruce M Simpson  * retain the above copyright notice and this paragraph in its entirety, (2)
85b0fe478SBruce M Simpson  * distributions including binary code include the above copyright notice and
95b0fe478SBruce M Simpson  * this paragraph in its entirety in the documentation or other materials
105b0fe478SBruce M Simpson  * provided with the distribution, and (3) all advertising materials mentioning
115b0fe478SBruce M Simpson  * features or use of this software display the following acknowledgement:
125b0fe478SBruce M Simpson  * ``This product includes software developed by the University of California,
135b0fe478SBruce M Simpson  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
145b0fe478SBruce M Simpson  * the University nor the names of its contributors may be used to endorse
155b0fe478SBruce M Simpson  * or promote products derived from this software without specific prior
165b0fe478SBruce M Simpson  * written permission.
175b0fe478SBruce M Simpson  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
185b0fe478SBruce M Simpson  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
195b0fe478SBruce M Simpson  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
205b0fe478SBruce M Simpson  */
215b0fe478SBruce M Simpson 
225b0fe478SBruce M Simpson #ifndef lint
235b0fe478SBruce M Simpson static const char rcsid[] _U_ =
241de50e9fSSam Leffler     "@(#) $Header: /tcpdump/master/tcpdump/print-pflog.c,v 1.13 2005/04/06 21:32:41 mcr Exp $ (LBL)";
255b0fe478SBruce M Simpson #endif
265b0fe478SBruce M Simpson 
275b0fe478SBruce M Simpson #ifdef HAVE_CONFIG_H
285b0fe478SBruce M Simpson #include "config.h"
295b0fe478SBruce M Simpson #endif
305b0fe478SBruce M Simpson 
315b0fe478SBruce M Simpson #include <tcpdump-stdinc.h>
325b0fe478SBruce M Simpson 
335b0fe478SBruce M Simpson #include <stdio.h>
345b0fe478SBruce M Simpson #include <pcap.h>
355b0fe478SBruce M Simpson 
365b0fe478SBruce M Simpson #include "interface.h"
375b0fe478SBruce M Simpson #include "addrtoname.h"
385b0fe478SBruce M Simpson #include "pf.h"
395b0fe478SBruce M Simpson 
405b0fe478SBruce M Simpson static struct tok pf_reasons[] = {
415b0fe478SBruce M Simpson 	{ 0,	"0(match)" },
425b0fe478SBruce M Simpson 	{ 1,	"1(bad-offset)" },
435b0fe478SBruce M Simpson 	{ 2,	"2(fragment)" },
445b0fe478SBruce M Simpson 	{ 3,	"3(short)" },
455b0fe478SBruce M Simpson 	{ 4,	"4(normalize)" },
465b0fe478SBruce M Simpson 	{ 5,	"5(memory)" },
475b0fe478SBruce M Simpson 	{ 0,	NULL }
485b0fe478SBruce M Simpson };
495b0fe478SBruce M Simpson 
505b0fe478SBruce M Simpson static struct tok pf_actions[] = {
515b0fe478SBruce M Simpson 	{ PF_PASS,		"pass" },
525b0fe478SBruce M Simpson 	{ PF_DROP,		"block" },
535b0fe478SBruce M Simpson 	{ PF_SCRUB,		"scrub" },
545b0fe478SBruce M Simpson 	{ PF_NAT,		"nat" },
555b0fe478SBruce M Simpson 	{ PF_NONAT,		"nat" },
565b0fe478SBruce M Simpson 	{ PF_BINAT,		"binat" },
575b0fe478SBruce M Simpson 	{ PF_NOBINAT,		"binat" },
585b0fe478SBruce M Simpson 	{ PF_RDR,		"rdr" },
595b0fe478SBruce M Simpson 	{ PF_NORDR,		"rdr" },
605b0fe478SBruce M Simpson 	{ PF_SYNPROXY_DROP,	"synproxy-drop" },
615b0fe478SBruce M Simpson 	{ 0,			NULL }
625b0fe478SBruce M Simpson };
635b0fe478SBruce M Simpson 
645b0fe478SBruce M Simpson static struct tok pf_directions[] = {
655b0fe478SBruce M Simpson 	{ PF_INOUT,	"in/out" },
665b0fe478SBruce M Simpson 	{ PF_IN,	"in" },
675b0fe478SBruce M Simpson 	{ PF_OUT,	"out" },
685b0fe478SBruce M Simpson 	{ 0,		NULL }
695b0fe478SBruce M Simpson };
705b0fe478SBruce M Simpson 
715b0fe478SBruce M Simpson /* For reading capture files on other systems */
725b0fe478SBruce M Simpson #define	OPENBSD_AF_INET		2
735b0fe478SBruce M Simpson #define	OPENBSD_AF_INET6	24
745b0fe478SBruce M Simpson 
755b0fe478SBruce M Simpson static void
765b0fe478SBruce M Simpson pflog_print(const struct pfloghdr *hdr)
775b0fe478SBruce M Simpson {
781de50e9fSSam Leffler 	u_int32_t rulenr, subrulenr;
791de50e9fSSam Leffler 
801de50e9fSSam Leffler 	rulenr = ntohl(hdr->rulenr);
811de50e9fSSam Leffler 	subrulenr = ntohl(hdr->subrulenr);
821de50e9fSSam Leffler 	if (subrulenr == (u_int32_t)-1)
831de50e9fSSam Leffler 		printf("rule %u/", rulenr);
845b0fe478SBruce M Simpson 	else
851de50e9fSSam Leffler 		printf("rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr);
865b0fe478SBruce M Simpson 
875b0fe478SBruce M Simpson 	printf("%s: %s %s on %s: ",
885b0fe478SBruce M Simpson 	    tok2str(pf_reasons, "unkn(%u)", hdr->reason),
895b0fe478SBruce M Simpson 	    tok2str(pf_actions, "unkn(%u)", hdr->action),
905b0fe478SBruce M Simpson 	    tok2str(pf_directions, "unkn(%u)", hdr->dir),
915b0fe478SBruce M Simpson 	    hdr->ifname);
925b0fe478SBruce M Simpson }
935b0fe478SBruce M Simpson 
945b0fe478SBruce M Simpson u_int
955b0fe478SBruce M Simpson pflog_if_print(const struct pcap_pkthdr *h, register const u_char *p)
965b0fe478SBruce M Simpson {
975b0fe478SBruce M Simpson 	u_int length = h->len;
985b0fe478SBruce M Simpson 	u_int hdrlen;
995b0fe478SBruce M Simpson 	u_int caplen = h->caplen;
1005b0fe478SBruce M Simpson 	const struct pfloghdr *hdr;
1015b0fe478SBruce M Simpson 	u_int8_t af;
1025b0fe478SBruce M Simpson 
1035b0fe478SBruce M Simpson 	/* check length */
1045b0fe478SBruce M Simpson 	if (caplen < sizeof(u_int8_t)) {
1055b0fe478SBruce M Simpson 		printf("[|pflog]");
1065b0fe478SBruce M Simpson 		return (caplen);
1075b0fe478SBruce M Simpson 	}
1085b0fe478SBruce M Simpson 
1095b0fe478SBruce M Simpson #define MIN_PFLOG_HDRLEN	45
1105b0fe478SBruce M Simpson 	hdr = (struct pfloghdr *)p;
1115b0fe478SBruce M Simpson 	if (hdr->length < MIN_PFLOG_HDRLEN) {
1125b0fe478SBruce M Simpson 		printf("[pflog: invalid header length!]");
1135b0fe478SBruce M Simpson 		return (hdr->length);	/* XXX: not really */
1145b0fe478SBruce M Simpson 	}
1155b0fe478SBruce M Simpson 	hdrlen = BPF_WORDALIGN(hdr->length);
1165b0fe478SBruce M Simpson 
1175b0fe478SBruce M Simpson 	if (caplen < hdrlen) {
1185b0fe478SBruce M Simpson 		printf("[|pflog]");
1195b0fe478SBruce M Simpson 		return (hdrlen);	/* XXX: true? */
1205b0fe478SBruce M Simpson 	}
1215b0fe478SBruce M Simpson 
1225b0fe478SBruce M Simpson 	/* print what we know */
1235b0fe478SBruce M Simpson 	hdr = (struct pfloghdr *)p;
1245b0fe478SBruce M Simpson 	TCHECK(*hdr);
1255b0fe478SBruce M Simpson 	if (eflag)
1265b0fe478SBruce M Simpson 		pflog_print(hdr);
1275b0fe478SBruce M Simpson 
1285b0fe478SBruce M Simpson 	/* skip to the real packet */
1295b0fe478SBruce M Simpson 	af = hdr->af;
1305b0fe478SBruce M Simpson 	length -= hdrlen;
1315b0fe478SBruce M Simpson 	caplen -= hdrlen;
1325b0fe478SBruce M Simpson 	p += hdrlen;
1335b0fe478SBruce M Simpson 	switch (af) {
1345b0fe478SBruce M Simpson 
1355b0fe478SBruce M Simpson 		case AF_INET:
1365b0fe478SBruce M Simpson #if OPENBSD_AF_INET != AF_INET
1375b0fe478SBruce M Simpson 		case OPENBSD_AF_INET:		/* XXX: read pcap files */
1385b0fe478SBruce M Simpson #endif
1391de50e9fSSam Leffler 		        ip_print(gndo, p, length);
1405b0fe478SBruce M Simpson 			break;
1415b0fe478SBruce M Simpson 
1425b0fe478SBruce M Simpson #ifdef INET6
1435b0fe478SBruce M Simpson 		case AF_INET6:
1445b0fe478SBruce M Simpson #if OPENBSD_AF_INET6 != AF_INET6
1455b0fe478SBruce M Simpson 		case OPENBSD_AF_INET6:		/* XXX: read pcap files */
1465b0fe478SBruce M Simpson #endif
1475b0fe478SBruce M Simpson 			ip6_print(p, length);
1485b0fe478SBruce M Simpson 			break;
1495b0fe478SBruce M Simpson #endif
1505b0fe478SBruce M Simpson 
1515b0fe478SBruce M Simpson 	default:
1525b0fe478SBruce M Simpson 		/* address family not handled, print raw packet */
1535b0fe478SBruce M Simpson 		if (!eflag)
1545b0fe478SBruce M Simpson 			pflog_print(hdr);
1555b0fe478SBruce M Simpson 		if (!xflag && !qflag)
1565b0fe478SBruce M Simpson 			default_print(p, caplen);
1575b0fe478SBruce M Simpson 	}
1585b0fe478SBruce M Simpson 
1595b0fe478SBruce M Simpson 	return (hdrlen);
1605b0fe478SBruce M Simpson trunc:
1615b0fe478SBruce M Simpson 	printf("[|pflog]");
1625b0fe478SBruce M Simpson 	return (hdrlen);
1635b0fe478SBruce M Simpson }
1641de50e9fSSam Leffler 
1651de50e9fSSam Leffler /*
1661de50e9fSSam Leffler  * Local Variables:
1671de50e9fSSam Leffler  * c-style: whitesmith
1681de50e9fSSam Leffler  * c-basic-offset: 8
1691de50e9fSSam Leffler  * End:
1701de50e9fSSam Leffler  */
171