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