xref: /freebsd/contrib/tcpdump/print-pflog.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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.7.2.4 2004/03/29 21:56:26 guy Exp $ (LBL)";
25 #endif
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <tcpdump-stdinc.h>
32 
33 #include <stdio.h>
34 #include <pcap.h>
35 
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "pf.h"
39 
40 static struct tok pf_reasons[] = {
41 	{ 0,	"0(match)" },
42 	{ 1,	"1(bad-offset)" },
43 	{ 2,	"2(fragment)" },
44 	{ 3,	"3(short)" },
45 	{ 4,	"4(normalize)" },
46 	{ 5,	"5(memory)" },
47 	{ 0,	NULL }
48 };
49 
50 static struct tok pf_actions[] = {
51 	{ PF_PASS,		"pass" },
52 	{ PF_DROP,		"block" },
53 	{ PF_SCRUB,		"scrub" },
54 	{ PF_NAT,		"nat" },
55 	{ PF_NONAT,		"nat" },
56 	{ PF_BINAT,		"binat" },
57 	{ PF_NOBINAT,		"binat" },
58 	{ PF_RDR,		"rdr" },
59 	{ PF_NORDR,		"rdr" },
60 	{ PF_SYNPROXY_DROP,	"synproxy-drop" },
61 	{ 0,			NULL }
62 };
63 
64 static struct tok pf_directions[] = {
65 	{ PF_INOUT,	"in/out" },
66 	{ PF_IN,	"in" },
67 	{ PF_OUT,	"out" },
68 	{ 0,		NULL }
69 };
70 
71 /* For reading capture files on other systems */
72 #define	OPENBSD_AF_INET		2
73 #define	OPENBSD_AF_INET6	24
74 
75 static void
76 pflog_print(const struct pfloghdr *hdr)
77 {
78 	if (ntohl(hdr->subrulenr) == (u_int32_t)-1)
79 		printf("rule %u/", ntohl(hdr->rulenr));
80 	else
81 		printf("rule %u.%s.%u/", ntohl(hdr->rulenr), hdr->ruleset,
82 		    ntohl(hdr->subrulenr));
83 
84 	printf("%s: %s %s on %s: ",
85 	    tok2str(pf_reasons, "unkn(%u)", hdr->reason),
86 	    tok2str(pf_actions, "unkn(%u)", hdr->action),
87 	    tok2str(pf_directions, "unkn(%u)", hdr->dir),
88 	    hdr->ifname);
89 }
90 
91 u_int
92 pflog_if_print(const struct pcap_pkthdr *h, register const u_char *p)
93 {
94 	u_int length = h->len;
95 	u_int hdrlen;
96 	u_int caplen = h->caplen;
97 	const struct pfloghdr *hdr;
98 	u_int8_t af;
99 
100 	/* check length */
101 	if (caplen < sizeof(u_int8_t)) {
102 		printf("[|pflog]");
103 		return (caplen);
104 	}
105 
106 #define MIN_PFLOG_HDRLEN	45
107 	hdr = (struct pfloghdr *)p;
108 	if (hdr->length < MIN_PFLOG_HDRLEN) {
109 		printf("[pflog: invalid header length!]");
110 		return (hdr->length);	/* XXX: not really */
111 	}
112 	hdrlen = BPF_WORDALIGN(hdr->length);
113 
114 	if (caplen < hdrlen) {
115 		printf("[|pflog]");
116 		return (hdrlen);	/* XXX: true? */
117 	}
118 
119 	/* print what we know */
120 	hdr = (struct pfloghdr *)p;
121 	TCHECK(*hdr);
122 	if (eflag)
123 		pflog_print(hdr);
124 
125 	/* skip to the real packet */
126 	af = hdr->af;
127 	length -= hdrlen;
128 	caplen -= hdrlen;
129 	p += hdrlen;
130 	switch (af) {
131 
132 		case AF_INET:
133 #if OPENBSD_AF_INET != AF_INET
134 		case OPENBSD_AF_INET:		/* XXX: read pcap files */
135 #endif
136 			ip_print(p, length);
137 			break;
138 
139 #ifdef INET6
140 		case AF_INET6:
141 #if OPENBSD_AF_INET6 != AF_INET6
142 		case OPENBSD_AF_INET6:		/* XXX: read pcap files */
143 #endif
144 			ip6_print(p, length);
145 			break;
146 #endif
147 
148 	default:
149 		/* address family not handled, print raw packet */
150 		if (!eflag)
151 			pflog_print(hdr);
152 		if (!xflag && !qflag)
153 			default_print(p, caplen);
154 	}
155 
156 	return (hdrlen);
157 trunc:
158 	printf("[|pflog]");
159 	return (hdrlen);
160 }
161