xref: /freebsd/contrib/tcpdump/print-pflog.c (revision 59e2ff550c448126b988150ce800cdf73bb5103e)
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 #define NETDISSECT_REWORKED
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #ifndef HAVE_NET_PFVAR_H
28 #error "No pf headers available"
29 #endif
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <net/if.h>
33 #include <net/pfvar.h>
34 #include <net/if_pflog.h>
35 
36 #include <tcpdump-stdinc.h>
37 
38 #include "interface.h"
39 #include "extract.h"
40 
41 static const char tstr[] = "[|pflog]";
42 
43 static const struct tok pf_reasons[] = {
44 	{ 0,	"0(match)" },
45 	{ 1,	"1(bad-offset)" },
46 	{ 2,	"2(fragment)" },
47 	{ 3,	"3(short)" },
48 	{ 4,	"4(normalize)" },
49 	{ 5,	"5(memory)" },
50 	{ 6,	"6(bad-timestamp)" },
51 	{ 7,	"7(congestion)" },
52 	{ 8,	"8(ip-option)" },
53 	{ 9,	"9(proto-cksum)" },
54 	{ 10,	"10(state-mismatch)" },
55 	{ 11,	"11(state-insert)" },
56 	{ 12,	"12(state-limit)" },
57 	{ 13,	"13(src-limit)" },
58 	{ 14,	"14(synproxy)" },
59 	{ 0,	NULL }
60 };
61 
62 static const struct tok pf_actions[] = {
63 	{ PF_PASS,		"pass" },
64 	{ PF_DROP,		"block" },
65 	{ PF_SCRUB,		"scrub" },
66 	{ PF_NAT,		"nat" },
67 	{ PF_NONAT,		"nat" },
68 	{ PF_BINAT,		"binat" },
69 	{ PF_NOBINAT,		"binat" },
70 	{ PF_RDR,		"rdr" },
71 	{ PF_NORDR,		"rdr" },
72 	{ PF_SYNPROXY_DROP,	"synproxy-drop" },
73 	{ 0,			NULL }
74 };
75 
76 static const struct tok pf_directions[] = {
77 	{ PF_INOUT,	"in/out" },
78 	{ PF_IN,	"in" },
79 	{ PF_OUT,	"out" },
80 	{ 0,		NULL }
81 };
82 
83 /* For reading capture files on other systems */
84 #define	OPENBSD_AF_INET		2
85 #define	OPENBSD_AF_INET6	24
86 
87 static void
88 pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
89 {
90 	uint32_t rulenr, subrulenr;
91 
92 	rulenr = EXTRACT_32BITS(&hdr->rulenr);
93 	subrulenr = EXTRACT_32BITS(&hdr->subrulenr);
94 	if (subrulenr == (uint32_t)-1)
95 		ND_PRINT((ndo, "rule %u/", rulenr));
96 	else
97 		ND_PRINT((ndo, "rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr));
98 
99 	ND_PRINT((ndo, "%s: %s %s on %s: ",
100 	    tok2str(pf_reasons, "unkn(%u)", hdr->reason),
101 	    tok2str(pf_actions, "unkn(%u)", hdr->action),
102 	    tok2str(pf_directions, "unkn(%u)", hdr->dir),
103 	    hdr->ifname));
104 }
105 
106 u_int
107 pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
108                register const u_char *p)
109 {
110 	u_int length = h->len;
111 	u_int hdrlen;
112 	u_int caplen = h->caplen;
113 	const struct pfloghdr *hdr;
114 	uint8_t af;
115 
116 	/* check length */
117 	if (caplen < sizeof(uint8_t)) {
118 		ND_PRINT((ndo, "%s", tstr));
119 		return (caplen);
120 	}
121 
122 #define MIN_PFLOG_HDRLEN	45
123 	hdr = (struct pfloghdr *)p;
124 	if (hdr->length < MIN_PFLOG_HDRLEN) {
125 		ND_PRINT((ndo, "[pflog: invalid header length!]"));
126 		return (hdr->length);	/* XXX: not really */
127 	}
128 	hdrlen = BPF_WORDALIGN(hdr->length);
129 
130 	if (caplen < hdrlen) {
131 		ND_PRINT((ndo, "%s", tstr));
132 		return (hdrlen);	/* XXX: true? */
133 	}
134 
135 	/* print what we know */
136 	hdr = (struct pfloghdr *)p;
137 	ND_TCHECK(*hdr);
138 	if (ndo->ndo_eflag)
139 		pflog_print(ndo, hdr);
140 
141 	/* skip to the real packet */
142 	af = hdr->af;
143 	length -= hdrlen;
144 	caplen -= hdrlen;
145 	p += hdrlen;
146 	switch (af) {
147 
148 		case AF_INET:
149 #if OPENBSD_AF_INET != AF_INET
150 		case OPENBSD_AF_INET:		/* XXX: read pcap files */
151 #endif
152 		        ip_print(ndo, p, length);
153 			break;
154 
155 #if defined(AF_INET6) || defined(OPENBSD_AF_INET6)
156 #ifdef AF_INET6
157 		case AF_INET6:
158 #endif /* AF_INET6 */
159 #if !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6
160 		case OPENBSD_AF_INET6:		/* XXX: read pcap files */
161 #endif /* !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 */
162 			ip6_print(ndo, p, length);
163 			break;
164 #endif /* defined(AF_INET6) || defined(OPENBSD_AF_INET6) */
165 
166 	default:
167 		/* address family not handled, print raw packet */
168 		if (!ndo->ndo_eflag)
169 			pflog_print(ndo, hdr);
170 		if (!ndo->ndo_suppress_default_print)
171 			ND_DEFAULTPRINT(p, caplen);
172 	}
173 
174 	return (hdrlen);
175 trunc:
176 	ND_PRINT((ndo, "%s", tstr));
177 	return (hdrlen);
178 }
179 
180 /*
181  * Local Variables:
182  * c-style: whitesmith
183  * c-basic-offset: 8
184  * End:
185  */
186