xref: /freebsd/contrib/tcpdump/print-pflog.c (revision d72f87c0fd1418bdb814594ea8fc76a202f7d5c6)
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 
22ee67461eSJoseph Mingrone /* \summary: *BSD/Darwin packet filter log file printer */
233340d773SGleb Smirnoff 
24ee67461eSJoseph Mingrone #include <config.h>
255b0fe478SBruce M Simpson 
26ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
275b0fe478SBruce M Simpson 
283340d773SGleb Smirnoff #include "netdissect.h"
293c602fabSXin LI #include "extract.h"
30ee67461eSJoseph Mingrone #include "af.h"
315b0fe478SBruce M Simpson 
32ee67461eSJoseph Mingrone #include "pflog.h"
333c602fabSXin LI 
343c602fabSXin LI static const struct tok pf_reasons[] = {
35ee67461eSJoseph Mingrone 	{ PFRES_MATCH,		"0(match)" },
36ee67461eSJoseph Mingrone 	{ PFRES_BADOFF,		"1(bad-offset)" },
37ee67461eSJoseph Mingrone 	{ PFRES_FRAG,		"2(fragment)" },
380a7e5f1fSJoseph Mingrone 	{ PFRES_SHORT,		"3(short)" },
39ee67461eSJoseph Mingrone 	{ PFRES_NORM,		"4(normalize)" },
40ee67461eSJoseph Mingrone 	{ PFRES_MEMORY,		"5(memory)" },
41ee67461eSJoseph Mingrone 	{ PFRES_TS,		"6(bad-timestamp)" },
42ee67461eSJoseph Mingrone 	{ PFRES_CONGEST,	"7(congestion)" },
43ee67461eSJoseph Mingrone 	{ PFRES_IPOPTIONS,	"8(ip-option)" },
44ee67461eSJoseph Mingrone 	{ PFRES_PROTCKSUM,	"9(proto-cksum)" },
45ee67461eSJoseph Mingrone 	{ PFRES_BADSTATE,	"10(state-mismatch)" },
46ee67461eSJoseph Mingrone 	{ PFRES_STATEINS,	"11(state-insert)" },
47ee67461eSJoseph Mingrone 	{ PFRES_MAXSTATES,	"12(state-limit)" },
48ee67461eSJoseph Mingrone 	{ PFRES_SRCLIMIT,	"13(src-limit)" },
49ee67461eSJoseph Mingrone 	{ PFRES_SYNPROXY,	"14(synproxy)" },
50ee67461eSJoseph Mingrone #if defined(__FreeBSD__)
51ee67461eSJoseph Mingrone 	{ PFRES_MAPFAILED,	"15(map-failed)" },
52ee67461eSJoseph Mingrone #elif defined(__NetBSD__)
53ee67461eSJoseph Mingrone 	{ PFRES_STATELOCKED,	"15(state-locked)" },
54ee67461eSJoseph Mingrone #elif defined(__OpenBSD__)
55ee67461eSJoseph Mingrone 	{ PFRES_TRANSLATE,	"15(translate)" },
56ee67461eSJoseph Mingrone 	{ PFRES_NOROUTE,	"16(no-route)" },
57ee67461eSJoseph Mingrone #elif defined(__APPLE__)
58ee67461eSJoseph Mingrone 	{ PFRES_DUMMYNET,	"15(dummynet)" },
59ee67461eSJoseph Mingrone #endif
605b0fe478SBruce M Simpson 	{ 0,	NULL }
615b0fe478SBruce M Simpson };
625b0fe478SBruce M Simpson 
633c602fabSXin LI static const struct tok pf_actions[] = {
645b0fe478SBruce M Simpson 	{ PF_PASS,		"pass" },
655b0fe478SBruce M Simpson 	{ PF_DROP,		"block" },
665b0fe478SBruce M Simpson 	{ PF_SCRUB,		"scrub" },
6756cd7716SKajetan Staszkiewicz 	{ PF_NOSCRUB,		"scrub" },
685b0fe478SBruce M Simpson 	{ PF_NAT,		"nat" },
69ee67461eSJoseph Mingrone 	{ PF_NONAT,		"nonat" },
705b0fe478SBruce M Simpson 	{ PF_BINAT,		"binat" },
71ee67461eSJoseph Mingrone 	{ PF_NOBINAT,		"nobinat" },
725b0fe478SBruce M Simpson 	{ PF_RDR,		"rdr" },
73ee67461eSJoseph Mingrone 	{ PF_NORDR,		"nordr" },
745b0fe478SBruce M Simpson 	{ PF_SYNPROXY_DROP,	"synproxy-drop" },
75ee67461eSJoseph Mingrone #if defined(__FreeBSD__)
76ee67461eSJoseph Mingrone 	{ PF_DEFER,		"defer" },
7756cd7716SKajetan Staszkiewicz 	{ PF_MATCH,		"match" },
78ee67461eSJoseph Mingrone #elif defined(__OpenBSD__)
79ee67461eSJoseph Mingrone 	{ PF_DEFER,		"defer" },
80ee67461eSJoseph Mingrone 	{ PF_MATCH,		"match" },
81ee67461eSJoseph Mingrone 	{ PF_DIVERT,		"divert" },
82ee67461eSJoseph Mingrone 	{ PF_RT,		"rt" },
83ee67461eSJoseph Mingrone 	{ PF_AFRT,		"afrt" },
84ee67461eSJoseph Mingrone #elif defined(__APPLE__)
85ee67461eSJoseph Mingrone 	{ PF_DUMMYNET,		"dummynet" },
86ee67461eSJoseph Mingrone 	{ PF_NODUMMYNET,	"nodummynet" },
87ee67461eSJoseph Mingrone 	{ PF_NAT64,		"nat64" },
88ee67461eSJoseph Mingrone 	{ PF_NONAT64,		"nonat64" },
89ee67461eSJoseph Mingrone #endif
905b0fe478SBruce M Simpson 	{ 0,			NULL }
915b0fe478SBruce M Simpson };
925b0fe478SBruce M Simpson 
933c602fabSXin LI static const struct tok pf_directions[] = {
945b0fe478SBruce M Simpson 	{ PF_INOUT,	"in/out" },
955b0fe478SBruce M Simpson 	{ PF_IN,	"in" },
965b0fe478SBruce M Simpson 	{ PF_OUT,	"out" },
97ee67461eSJoseph Mingrone #if defined(__OpenBSD__)
98ee67461eSJoseph Mingrone 	{ PF_FWD,	"fwd" },
99ee67461eSJoseph Mingrone #endif
1005b0fe478SBruce M Simpson 	{ 0,		NULL }
1015b0fe478SBruce M Simpson };
1025b0fe478SBruce M Simpson 
1035b0fe478SBruce M Simpson static void
pflog_print(netdissect_options * ndo,const struct pfloghdr * hdr)1043c602fabSXin LI pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
1055b0fe478SBruce M Simpson {
10676c5eeccSKristof Provost 	uint32_t rulenr, subrulenr, ridentifier;
1071de50e9fSSam Leffler 
108ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "pflog";
1090a7e5f1fSJoseph Mingrone 	rulenr = GET_BE_U_4(hdr->rulenr);
1100a7e5f1fSJoseph Mingrone 	subrulenr = GET_BE_U_4(hdr->subrulenr);
1110a7e5f1fSJoseph Mingrone 	ridentifier = GET_BE_U_4(hdr->ridentifier);
1123c602fabSXin LI 	if (subrulenr == (uint32_t)-1)
113ee67461eSJoseph Mingrone 		ND_PRINT("rule %u/", rulenr);
114ee67461eSJoseph Mingrone 	else {
115ee67461eSJoseph Mingrone 		ND_PRINT("rule %u.", rulenr);
116ee67461eSJoseph Mingrone 		nd_printjnp(ndo, (const u_char*)hdr->ruleset, PFLOG_RULESET_NAME_SIZE);
117ee67461eSJoseph Mingrone 		ND_PRINT(".%u/", subrulenr);
1185b0fe478SBruce M Simpson 	}
1195b0fe478SBruce M Simpson 
1200a7e5f1fSJoseph Mingrone 	ND_PRINT("%s", tok2str(pf_reasons, "unkn(%u)", GET_U_1(hdr->reason)));
121ee67461eSJoseph Mingrone 
122*d72f87c0SEric A. Borisch 	if (GET_HE_U_4(hdr->uid) != UID_MAX)
123*d72f87c0SEric A. Borisch 		ND_PRINT(" [uid %u]", GET_HE_U_4(hdr->uid));
124ee67461eSJoseph Mingrone 
125ee67461eSJoseph Mingrone 	if (ridentifier != 0)
126ee67461eSJoseph Mingrone 		ND_PRINT(" [ridentifier %u]", ridentifier);
127ee67461eSJoseph Mingrone 
128ee67461eSJoseph Mingrone 	ND_PRINT(": %s %s on ",
1290a7e5f1fSJoseph Mingrone 	    tok2str(pf_actions, "unkn(%u)", GET_U_1(hdr->action)),
1300a7e5f1fSJoseph Mingrone 	    tok2str(pf_directions, "unkn(%u)", GET_U_1(hdr->dir)));
131ee67461eSJoseph Mingrone 	nd_printjnp(ndo, (const u_char*)hdr->ifname, PFLOG_IFNAMSIZ);
132ee67461eSJoseph Mingrone 	ND_PRINT(": ");
133ee67461eSJoseph Mingrone }
134ee67461eSJoseph Mingrone 
135ee67461eSJoseph Mingrone void
pflog_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)1363c602fabSXin LI pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
137ee67461eSJoseph Mingrone                const u_char *p)
1385b0fe478SBruce M Simpson {
1395b0fe478SBruce M Simpson 	u_int length = h->len;
1405b0fe478SBruce M Simpson 	u_int hdrlen;
1415b0fe478SBruce M Simpson 	u_int caplen = h->caplen;
1425b0fe478SBruce M Simpson 	const struct pfloghdr *hdr;
1433c602fabSXin LI 	uint8_t af;
1445b0fe478SBruce M Simpson 
145ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "pflog";
1465b0fe478SBruce M Simpson 	/* check length */
1473c602fabSXin LI 	if (caplen < sizeof(uint8_t)) {
148ee67461eSJoseph Mingrone 		nd_print_trunc(ndo);
149ee67461eSJoseph Mingrone 		ndo->ndo_ll_hdr_len += h->caplen;
150ee67461eSJoseph Mingrone 		return;
1515b0fe478SBruce M Simpson 	}
1525b0fe478SBruce M Simpson 
1533340d773SGleb Smirnoff 	hdr = (const struct pfloghdr *)p;
1540a7e5f1fSJoseph Mingrone 	hdrlen = GET_U_1(hdr->length);
1550a7e5f1fSJoseph Mingrone 	if (hdrlen < MIN_PFLOG_HDRLEN) {
156ee67461eSJoseph Mingrone 		ND_PRINT("[pflog: invalid header length!]");
1570a7e5f1fSJoseph Mingrone 		ndo->ndo_ll_hdr_len += hdrlen;	/* XXX: not really */
158ee67461eSJoseph Mingrone 		return;
1595b0fe478SBruce M Simpson 	}
1600a7e5f1fSJoseph Mingrone 	hdrlen = roundup2(hdrlen, 4);
1615b0fe478SBruce M Simpson 
1625b0fe478SBruce M Simpson 	if (caplen < hdrlen) {
163ee67461eSJoseph Mingrone 		nd_print_trunc(ndo);
164ee67461eSJoseph Mingrone 		ndo->ndo_ll_hdr_len += hdrlen;	/* XXX: true? */
165ee67461eSJoseph Mingrone 		return;
1665b0fe478SBruce M Simpson 	}
1675b0fe478SBruce M Simpson 
1685b0fe478SBruce M Simpson 	/* print what we know */
169ee67461eSJoseph Mingrone 	ND_TCHECK_SIZE(hdr);
1703c602fabSXin LI 	if (ndo->ndo_eflag)
1713c602fabSXin LI 		pflog_print(ndo, hdr);
1725b0fe478SBruce M Simpson 
1735b0fe478SBruce M Simpson 	/* skip to the real packet */
1740a7e5f1fSJoseph Mingrone 	af = GET_U_1(hdr->af);
1755b0fe478SBruce M Simpson 	length -= hdrlen;
1765b0fe478SBruce M Simpson 	caplen -= hdrlen;
1775b0fe478SBruce M Simpson 	p += hdrlen;
1785b0fe478SBruce M Simpson 	switch (af) {
1795b0fe478SBruce M Simpson 
180ee67461eSJoseph Mingrone 		/*
181ee67461eSJoseph Mingrone 		 * If there's a system that doesn't use the AF_INET
182ee67461eSJoseph Mingrone 		 * from 4.2BSD, feel free to add its value to af.h
183ee67461eSJoseph Mingrone 		 * and use it here.
184ee67461eSJoseph Mingrone 		 *
185ee67461eSJoseph Mingrone 		 * Hopefully, there isn't.
186ee67461eSJoseph Mingrone 		 */
187ee67461eSJoseph Mingrone 		case BSD_AFNUM_INET:
1883c602fabSXin LI 		        ip_print(ndo, p, length);
1895b0fe478SBruce M Simpson 			break;
1905b0fe478SBruce M Simpson 
191ee67461eSJoseph Mingrone 		/*
192ee67461eSJoseph Mingrone 		 * Try all AF_INET6 values for all systems with pflog,
193ee67461eSJoseph Mingrone 		 * including Darwin.
194ee67461eSJoseph Mingrone 		 */
195ee67461eSJoseph Mingrone 		case BSD_AFNUM_INET6_BSD:
196ee67461eSJoseph Mingrone 		case BSD_AFNUM_INET6_FREEBSD:
197ee67461eSJoseph Mingrone 		case BSD_AFNUM_INET6_DARWIN:
1983c602fabSXin LI 			ip6_print(ndo, p, length);
1995b0fe478SBruce M Simpson 			break;
2005b0fe478SBruce M Simpson 
2015b0fe478SBruce M Simpson 	default:
2025b0fe478SBruce M Simpson 		/* address family not handled, print raw packet */
2033c602fabSXin LI 		if (!ndo->ndo_eflag)
2043c602fabSXin LI 			pflog_print(ndo, hdr);
2053c602fabSXin LI 		if (!ndo->ndo_suppress_default_print)
2063c602fabSXin LI 			ND_DEFAULTPRINT(p, caplen);
2075b0fe478SBruce M Simpson 	}
2085b0fe478SBruce M Simpson 
209ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += hdrlen;
210ee67461eSJoseph Mingrone 	return;
2115b0fe478SBruce M Simpson trunc:
212ee67461eSJoseph Mingrone 	nd_print_trunc(ndo);
213ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += hdrlen;
2145b0fe478SBruce M Simpson }
215