xref: /freebsd/contrib/tcpdump/print-pflog.c (revision 4d846d26)
1 /*
2  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 /* \summary: OpenBSD packet filter log file printer */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #ifndef HAVE_NET_PFVAR_H
29 #error "No pf headers available"
30 #endif
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 #include <net/pfvar.h>
35 #include <net/if_pflog.h>
36 
37 #include <netdissect-stdinc.h>
38 
39 #include "netdissect.h"
40 #include "extract.h"
41 
42 static const char tstr[] = "[|pflog]";
43 
44 static const struct tok pf_reasons[] = {
45 	{ 0,	"0(match)" },
46 	{ 1,	"1(bad-offset)" },
47 	{ 2,	"2(fragment)" },
48 	{ 3,	"3(short)" },
49 	{ 4,	"4(normalize)" },
50 	{ 5,	"5(memory)" },
51 	{ 6,	"6(bad-timestamp)" },
52 	{ 7,	"7(congestion)" },
53 	{ 8,	"8(ip-option)" },
54 	{ 9,	"9(proto-cksum)" },
55 	{ 10,	"10(state-mismatch)" },
56 	{ 11,	"11(state-insert)" },
57 	{ 12,	"12(state-limit)" },
58 	{ 13,	"13(src-limit)" },
59 	{ 14,	"14(synproxy)" },
60 	{ 15,	"15(map-failed)" },
61 	{ 0,	NULL }
62 };
63 
64 static const struct tok pf_actions[] = {
65 	{ PF_PASS,		"pass" },
66 	{ PF_DROP,		"block" },
67 	{ PF_SCRUB,		"scrub" },
68 	{ PF_NOSCRUB,		"scrub" },
69 	{ PF_NAT,		"nat" },
70 	{ PF_NONAT,		"nat" },
71 	{ PF_BINAT,		"binat" },
72 	{ PF_NOBINAT,		"binat" },
73 	{ PF_RDR,		"rdr" },
74 	{ PF_NORDR,		"rdr" },
75 	{ PF_SYNPROXY_DROP,	"synproxy-drop" },
76 	{ PF_DEFER,		"pfsync-defer" },
77 	{ PF_MATCH,		"match" },
78 	{ 0,			NULL }
79 };
80 
81 static const struct tok pf_directions[] = {
82 	{ PF_INOUT,	"in/out" },
83 	{ PF_IN,	"in" },
84 	{ PF_OUT,	"out" },
85 	{ 0,		NULL }
86 };
87 
88 /* For reading capture files on other systems */
89 #define	OPENBSD_AF_INET		2
90 #define	OPENBSD_AF_INET6	24
91 
92 static void
93 pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
94 {
95 	uint32_t rulenr, subrulenr, ridentifier;
96 
97 	rulenr = EXTRACT_32BITS(&hdr->rulenr);
98 	subrulenr = EXTRACT_32BITS(&hdr->subrulenr);
99 	ridentifier = EXTRACT_32BITS(&hdr->ridentifier);
100 
101 	if (subrulenr == (uint32_t)-1)
102 		ND_PRINT((ndo, "rule %u/", rulenr));
103 	else
104 		ND_PRINT((ndo, "rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr));
105 
106 	ND_PRINT((ndo, "%s", tok2str(pf_reasons, "unkn(%u)", hdr->reason)));
107 
108 	if (hdr->uid != UID_MAX)
109 		ND_PRINT((ndo, " [uid %u]", (unsigned)hdr->uid));
110 
111 	if (ridentifier != 0)
112 		ND_PRINT((ndo, " [ridentifier %u]", ridentifier));
113 
114 	ND_PRINT((ndo, ": %s %s on %s: ",
115 	    tok2str(pf_actions, "unkn(%u)", hdr->action),
116 	    tok2str(pf_directions, "unkn(%u)", hdr->dir),
117 	    hdr->ifname));
118 }
119 
120 u_int
121 pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
122                register const u_char *p)
123 {
124 	u_int length = h->len;
125 	u_int hdrlen;
126 	u_int caplen = h->caplen;
127 	const struct pfloghdr *hdr;
128 	uint8_t af;
129 
130 	/* check length */
131 	if (caplen < sizeof(uint8_t)) {
132 		ND_PRINT((ndo, "%s", tstr));
133 		return (caplen);
134 	}
135 
136 #define MIN_PFLOG_HDRLEN	45
137 	hdr = (const struct pfloghdr *)p;
138 	if (hdr->length < MIN_PFLOG_HDRLEN) {
139 		ND_PRINT((ndo, "[pflog: invalid header length!]"));
140 		return (hdr->length);	/* XXX: not really */
141 	}
142 	hdrlen = BPF_WORDALIGN(hdr->length);
143 
144 	if (caplen < hdrlen) {
145 		ND_PRINT((ndo, "%s", tstr));
146 		return (hdrlen);	/* XXX: true? */
147 	}
148 
149 	/* print what we know */
150 	ND_TCHECK(*hdr);
151 	if (ndo->ndo_eflag)
152 		pflog_print(ndo, hdr);
153 
154 	/* skip to the real packet */
155 	af = hdr->af;
156 	length -= hdrlen;
157 	caplen -= hdrlen;
158 	p += hdrlen;
159 	switch (af) {
160 
161 		case AF_INET:
162 #if OPENBSD_AF_INET != AF_INET
163 		case OPENBSD_AF_INET:		/* XXX: read pcap files */
164 #endif
165 		        ip_print(ndo, p, length);
166 			break;
167 
168 #if defined(AF_INET6) || defined(OPENBSD_AF_INET6)
169 #ifdef AF_INET6
170 		case AF_INET6:
171 #endif /* AF_INET6 */
172 #if !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6
173 		case OPENBSD_AF_INET6:		/* XXX: read pcap files */
174 #endif /* !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 */
175 			ip6_print(ndo, p, length);
176 			break;
177 #endif /* defined(AF_INET6) || defined(OPENBSD_AF_INET6) */
178 
179 	default:
180 		/* address family not handled, print raw packet */
181 		if (!ndo->ndo_eflag)
182 			pflog_print(ndo, hdr);
183 		if (!ndo->ndo_suppress_default_print)
184 			ND_DEFAULTPRINT(p, caplen);
185 	}
186 
187 	return (hdrlen);
188 trunc:
189 	ND_PRINT((ndo, "%s", tstr));
190 	return (hdrlen);
191 }
192 
193 /*
194  * Local Variables:
195  * c-style: whitesmith
196  * c-basic-offset: 8
197  * End:
198  */
199