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