]> The Tcpdump Group git mirrors - tcpdump/blob - print-pflog.c
From Jun Kuriyama:
[tcpdump] / print-pflog.c
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.15 2006-10-25 22:13:10 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 { 6, "6(bad-timestamp)" },
48 { 7, "7(congestion)" },
49 { 8, "8(ip-option)" },
50 { 9, "9(proto-cksum)" },
51 { 10, "10(state-mismatch)" },
52 { 11, "11(state-insert)" },
53 { 12, "12(state-limit)" },
54 { 13, "13(src-limit)" },
55 { 14, "14(synproxy)" },
56 { 0, NULL }
57 };
58
59 static struct tok pf_actions[] = {
60 { PF_PASS, "pass" },
61 { PF_DROP, "block" },
62 { PF_SCRUB, "scrub" },
63 { PF_NAT, "nat" },
64 { PF_NONAT, "nat" },
65 { PF_BINAT, "binat" },
66 { PF_NOBINAT, "binat" },
67 { PF_RDR, "rdr" },
68 { PF_NORDR, "rdr" },
69 { PF_SYNPROXY_DROP, "synproxy-drop" },
70 { 0, NULL }
71 };
72
73 static struct tok pf_directions[] = {
74 { PF_INOUT, "in/out" },
75 { PF_IN, "in" },
76 { PF_OUT, "out" },
77 { 0, NULL }
78 };
79
80 /* For reading capture files on other systems */
81 #define OPENBSD_AF_INET 2
82 #define OPENBSD_AF_INET6 24
83
84 static void
85 pflog_print(const struct pfloghdr *hdr)
86 {
87 u_int32_t rulenr, subrulenr;
88
89 rulenr = ntohl(hdr->rulenr);
90 subrulenr = ntohl(hdr->subrulenr);
91 if (subrulenr == (u_int32_t)-1)
92 printf("rule %u/", rulenr);
93 else
94 printf("rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr);
95
96 printf("%s: %s %s on %s: ",
97 tok2str(pf_reasons, "unkn(%u)", hdr->reason),
98 tok2str(pf_actions, "unkn(%u)", hdr->action),
99 tok2str(pf_directions, "unkn(%u)", hdr->dir),
100 hdr->ifname);
101 }
102
103 u_int
104 pflog_if_print(const struct pcap_pkthdr *h, register const u_char *p)
105 {
106 u_int length = h->len;
107 u_int hdrlen;
108 u_int caplen = h->caplen;
109 const struct pfloghdr *hdr;
110 u_int8_t af;
111
112 /* check length */
113 if (caplen < sizeof(u_int8_t)) {
114 printf("[|pflog]");
115 return (caplen);
116 }
117
118 #define MIN_PFLOG_HDRLEN 45
119 hdr = (struct pfloghdr *)p;
120 if (hdr->length < MIN_PFLOG_HDRLEN) {
121 printf("[pflog: invalid header length!]");
122 return (hdr->length); /* XXX: not really */
123 }
124 hdrlen = BPF_WORDALIGN(hdr->length);
125
126 if (caplen < hdrlen) {
127 printf("[|pflog]");
128 return (hdrlen); /* XXX: true? */
129 }
130
131 /* print what we know */
132 hdr = (struct pfloghdr *)p;
133 TCHECK(*hdr);
134 if (eflag)
135 pflog_print(hdr);
136
137 /* skip to the real packet */
138 af = hdr->af;
139 length -= hdrlen;
140 caplen -= hdrlen;
141 p += hdrlen;
142 switch (af) {
143
144 case AF_INET:
145 #if OPENBSD_AF_INET != AF_INET
146 case OPENBSD_AF_INET: /* XXX: read pcap files */
147 #endif
148 ip_print(gndo, p, length);
149 break;
150
151 #ifdef INET6
152 case AF_INET6:
153 #if OPENBSD_AF_INET6 != AF_INET6
154 case OPENBSD_AF_INET6: /* XXX: read pcap files */
155 #endif
156 ip6_print(p, length);
157 break;
158 #endif
159
160 default:
161 /* address family not handled, print raw packet */
162 if (!eflag)
163 pflog_print(hdr);
164 if (!suppress_default_print)
165 default_print(p, caplen);
166 }
167
168 return (hdrlen);
169 trunc:
170 printf("[|pflog]");
171 return (hdrlen);
172 }
173
174 /*
175 * Local Variables:
176 * c-style: whitesmith
177 * c-basic-offset: 8
178 * End:
179 */