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