xref: /freebsd/contrib/tcpdump/print-pflog.c (revision 56cd7716b63d84190e6308d50717ec8bbbdfe1c0)
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 
223340d773SGleb Smirnoff /* \summary: OpenBSD packet filter log file printer */
233340d773SGleb Smirnoff 
245b0fe478SBruce M Simpson #ifdef HAVE_CONFIG_H
255b0fe478SBruce M Simpson #include "config.h"
265b0fe478SBruce M Simpson #endif
275b0fe478SBruce M Simpson 
28b5bfcb5dSMax Laier #ifndef HAVE_NET_PFVAR_H
29b5bfcb5dSMax Laier #error "No pf headers available"
30b5bfcb5dSMax Laier #endif
31b5bfcb5dSMax Laier #include <sys/types.h>
32b5bfcb5dSMax Laier #include <sys/socket.h>
33b5bfcb5dSMax Laier #include <net/if.h>
34b5bfcb5dSMax Laier #include <net/pfvar.h>
35b5bfcb5dSMax Laier #include <net/if_pflog.h>
36b5bfcb5dSMax Laier 
373340d773SGleb Smirnoff #include <netdissect-stdinc.h>
385b0fe478SBruce M Simpson 
393340d773SGleb Smirnoff #include "netdissect.h"
403c602fabSXin LI #include "extract.h"
415b0fe478SBruce M Simpson 
423c602fabSXin LI static const char tstr[] = "[|pflog]";
433c602fabSXin LI 
443c602fabSXin LI static const struct tok pf_reasons[] = {
455b0fe478SBruce M Simpson 	{ 0,	"0(match)" },
465b0fe478SBruce M Simpson 	{ 1,	"1(bad-offset)" },
475b0fe478SBruce M Simpson 	{ 2,	"2(fragment)" },
485b0fe478SBruce M Simpson 	{ 3,	"3(short)" },
495b0fe478SBruce M Simpson 	{ 4,	"4(normalize)" },
505b0fe478SBruce M Simpson 	{ 5,	"5(memory)" },
51b5bfcb5dSMax Laier 	{ 6,	"6(bad-timestamp)" },
52b5bfcb5dSMax Laier 	{ 7,	"7(congestion)" },
53b5bfcb5dSMax Laier 	{ 8,	"8(ip-option)" },
54b5bfcb5dSMax Laier 	{ 9,	"9(proto-cksum)" },
55b5bfcb5dSMax Laier 	{ 10,	"10(state-mismatch)" },
56b5bfcb5dSMax Laier 	{ 11,	"11(state-insert)" },
57b5bfcb5dSMax Laier 	{ 12,	"12(state-limit)" },
58b5bfcb5dSMax Laier 	{ 13,	"13(src-limit)" },
59b5bfcb5dSMax Laier 	{ 14,	"14(synproxy)" },
60*56cd7716SKajetan Staszkiewicz 	{ 15,	"15(map-failed)" },
615b0fe478SBruce M Simpson 	{ 0,	NULL }
625b0fe478SBruce M Simpson };
635b0fe478SBruce M Simpson 
643c602fabSXin LI static const struct tok pf_actions[] = {
655b0fe478SBruce M Simpson 	{ PF_PASS,		"pass" },
665b0fe478SBruce M Simpson 	{ PF_DROP,		"block" },
675b0fe478SBruce M Simpson 	{ PF_SCRUB,		"scrub" },
68*56cd7716SKajetan Staszkiewicz 	{ PF_NOSCRUB,		"scrub" },
695b0fe478SBruce M Simpson 	{ PF_NAT,		"nat" },
705b0fe478SBruce M Simpson 	{ PF_NONAT,		"nat" },
715b0fe478SBruce M Simpson 	{ PF_BINAT,		"binat" },
725b0fe478SBruce M Simpson 	{ PF_NOBINAT,		"binat" },
735b0fe478SBruce M Simpson 	{ PF_RDR,		"rdr" },
745b0fe478SBruce M Simpson 	{ PF_NORDR,		"rdr" },
755b0fe478SBruce M Simpson 	{ PF_SYNPROXY_DROP,	"synproxy-drop" },
76*56cd7716SKajetan Staszkiewicz 	{ PF_DEFER,		"pfsync-defer" },
77*56cd7716SKajetan Staszkiewicz 	{ PF_MATCH,		"match" },
785b0fe478SBruce M Simpson 	{ 0,			NULL }
795b0fe478SBruce M Simpson };
805b0fe478SBruce M Simpson 
813c602fabSXin LI static const struct tok pf_directions[] = {
825b0fe478SBruce M Simpson 	{ PF_INOUT,	"in/out" },
835b0fe478SBruce M Simpson 	{ PF_IN,	"in" },
845b0fe478SBruce M Simpson 	{ PF_OUT,	"out" },
855b0fe478SBruce M Simpson 	{ 0,		NULL }
865b0fe478SBruce M Simpson };
875b0fe478SBruce M Simpson 
885b0fe478SBruce M Simpson /* For reading capture files on other systems */
895b0fe478SBruce M Simpson #define	OPENBSD_AF_INET		2
905b0fe478SBruce M Simpson #define	OPENBSD_AF_INET6	24
915b0fe478SBruce M Simpson 
925b0fe478SBruce M Simpson static void
933c602fabSXin LI pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
945b0fe478SBruce M Simpson {
9576c5eeccSKristof Provost 	uint32_t rulenr, subrulenr, ridentifier;
961de50e9fSSam Leffler 
9727df3f5dSRui Paulo 	rulenr = EXTRACT_32BITS(&hdr->rulenr);
9827df3f5dSRui Paulo 	subrulenr = EXTRACT_32BITS(&hdr->subrulenr);
9976c5eeccSKristof Provost 	ridentifier = EXTRACT_32BITS(&hdr->ridentifier);
10076c5eeccSKristof Provost 
1013c602fabSXin LI 	if (subrulenr == (uint32_t)-1)
1023c602fabSXin LI 		ND_PRINT((ndo, "rule %u/", rulenr));
1035b0fe478SBruce M Simpson 	else
1043c602fabSXin LI 		ND_PRINT((ndo, "rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr));
1055b0fe478SBruce M Simpson 
106a00d5618SKristof Provost 	ND_PRINT((ndo, "%s", tok2str(pf_reasons, "unkn(%u)", hdr->reason)));
107a00d5618SKristof Provost 
108a00d5618SKristof Provost 	if (hdr->uid != UID_MAX)
109a00d5618SKristof Provost 		ND_PRINT((ndo, " [uid %u]", (unsigned)hdr->uid));
110a00d5618SKristof Provost 
11176c5eeccSKristof Provost 	if (ridentifier != 0)
11276c5eeccSKristof Provost 		ND_PRINT((ndo, " [ridentifier %u]", ridentifier));
11376c5eeccSKristof Provost 
114a00d5618SKristof Provost 	ND_PRINT((ndo, ": %s %s on %s: ",
1155b0fe478SBruce M Simpson 	    tok2str(pf_actions, "unkn(%u)", hdr->action),
1165b0fe478SBruce M Simpson 	    tok2str(pf_directions, "unkn(%u)", hdr->dir),
1173c602fabSXin LI 	    hdr->ifname));
1185b0fe478SBruce M Simpson }
1195b0fe478SBruce M Simpson 
1205b0fe478SBruce M Simpson u_int
1213c602fabSXin LI pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
1223c602fabSXin LI                register const u_char *p)
1235b0fe478SBruce M Simpson {
1245b0fe478SBruce M Simpson 	u_int length = h->len;
1255b0fe478SBruce M Simpson 	u_int hdrlen;
1265b0fe478SBruce M Simpson 	u_int caplen = h->caplen;
1275b0fe478SBruce M Simpson 	const struct pfloghdr *hdr;
1283c602fabSXin LI 	uint8_t af;
1295b0fe478SBruce M Simpson 
1305b0fe478SBruce M Simpson 	/* check length */
1313c602fabSXin LI 	if (caplen < sizeof(uint8_t)) {
1323c602fabSXin LI 		ND_PRINT((ndo, "%s", tstr));
1335b0fe478SBruce M Simpson 		return (caplen);
1345b0fe478SBruce M Simpson 	}
1355b0fe478SBruce M Simpson 
1365b0fe478SBruce M Simpson #define MIN_PFLOG_HDRLEN	45
1373340d773SGleb Smirnoff 	hdr = (const struct pfloghdr *)p;
1385b0fe478SBruce M Simpson 	if (hdr->length < MIN_PFLOG_HDRLEN) {
1393c602fabSXin LI 		ND_PRINT((ndo, "[pflog: invalid header length!]"));
1405b0fe478SBruce M Simpson 		return (hdr->length);	/* XXX: not really */
1415b0fe478SBruce M Simpson 	}
1425b0fe478SBruce M Simpson 	hdrlen = BPF_WORDALIGN(hdr->length);
1435b0fe478SBruce M Simpson 
1445b0fe478SBruce M Simpson 	if (caplen < hdrlen) {
1453c602fabSXin LI 		ND_PRINT((ndo, "%s", tstr));
1465b0fe478SBruce M Simpson 		return (hdrlen);	/* XXX: true? */
1475b0fe478SBruce M Simpson 	}
1485b0fe478SBruce M Simpson 
1495b0fe478SBruce M Simpson 	/* print what we know */
1503c602fabSXin LI 	ND_TCHECK(*hdr);
1513c602fabSXin LI 	if (ndo->ndo_eflag)
1523c602fabSXin LI 		pflog_print(ndo, hdr);
1535b0fe478SBruce M Simpson 
1545b0fe478SBruce M Simpson 	/* skip to the real packet */
1555b0fe478SBruce M Simpson 	af = hdr->af;
1565b0fe478SBruce M Simpson 	length -= hdrlen;
1575b0fe478SBruce M Simpson 	caplen -= hdrlen;
1585b0fe478SBruce M Simpson 	p += hdrlen;
1595b0fe478SBruce M Simpson 	switch (af) {
1605b0fe478SBruce M Simpson 
1615b0fe478SBruce M Simpson 		case AF_INET:
1625b0fe478SBruce M Simpson #if OPENBSD_AF_INET != AF_INET
1635b0fe478SBruce M Simpson 		case OPENBSD_AF_INET:		/* XXX: read pcap files */
1645b0fe478SBruce M Simpson #endif
1653c602fabSXin LI 		        ip_print(ndo, p, length);
1665b0fe478SBruce M Simpson 			break;
1675b0fe478SBruce M Simpson 
1688bdc5a62SPatrick Kelsey #if defined(AF_INET6) || defined(OPENBSD_AF_INET6)
1698bdc5a62SPatrick Kelsey #ifdef AF_INET6
1705b0fe478SBruce M Simpson 		case AF_INET6:
1718bdc5a62SPatrick Kelsey #endif /* AF_INET6 */
1728bdc5a62SPatrick Kelsey #if !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6
1735b0fe478SBruce M Simpson 		case OPENBSD_AF_INET6:		/* XXX: read pcap files */
1748bdc5a62SPatrick Kelsey #endif /* !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 */
1753c602fabSXin LI 			ip6_print(ndo, p, length);
1765b0fe478SBruce M Simpson 			break;
1778bdc5a62SPatrick Kelsey #endif /* defined(AF_INET6) || defined(OPENBSD_AF_INET6) */
1785b0fe478SBruce M Simpson 
1795b0fe478SBruce M Simpson 	default:
1805b0fe478SBruce M Simpson 		/* address family not handled, print raw packet */
1813c602fabSXin LI 		if (!ndo->ndo_eflag)
1823c602fabSXin LI 			pflog_print(ndo, hdr);
1833c602fabSXin LI 		if (!ndo->ndo_suppress_default_print)
1843c602fabSXin LI 			ND_DEFAULTPRINT(p, caplen);
1855b0fe478SBruce M Simpson 	}
1865b0fe478SBruce M Simpson 
1875b0fe478SBruce M Simpson 	return (hdrlen);
1885b0fe478SBruce M Simpson trunc:
1893c602fabSXin LI 	ND_PRINT((ndo, "%s", tstr));
1905b0fe478SBruce M Simpson 	return (hdrlen);
1915b0fe478SBruce M Simpson }
1921de50e9fSSam Leffler 
1931de50e9fSSam Leffler /*
1941de50e9fSSam Leffler  * Local Variables:
1951de50e9fSSam Leffler  * c-style: whitesmith
1961de50e9fSSam Leffler  * c-basic-offset: 8
1971de50e9fSSam Leffler  * End:
1981de50e9fSSam Leffler  */
199